package cn.itcast.shop.utils;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/*
* 发送邮件的工具类
* */
public class MailUtils {
/*
* 发送邮件的方法
* @param to 收件人
* @param code 激活码
*
* */
public static void sendMail(String to,String code){
/*
* 创建session对象
* 创建一个代表邮件的对象Message
* 发送邮件Transpot
*
* */
//1 获得连接对象
Properties props=new Properties();
props.setProperty("mail.host", "localhost");
Session session =Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("service@shop.com","111");
}
});
//2创建邮件对象
Message message=new MimeMessage(session);
try {
message.setFrom(new InternetAddress("service@shop.com"));
message.setRecipient(RecipientType.TO, new InternetAddress(to));
message.setSubject("来自传智播客的购物系统");
message.setContent("<h1>来之传智商城的购物系统,点下面的连接完成激活</h1> <h3> <a href='http://172.17.215.235:8080/shop/user_active.action?code="+code+"'>http://172.17.215.235:8080/shop/user_active.action?code="+code+"</a></h3>", "text/html;charset=UTF-8");
Transport.send(message);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
sendMail("aaa@shop.com", "11111111111111111");
}
}
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/*
* 发送邮件的工具类
* */
public class MailUtils {
/*
* 发送邮件的方法
* @param to 收件人
* @param code 激活码
*
* */
public static void sendMail(String to,String code){
/*
* 创建session对象
* 创建一个代表邮件的对象Message
* 发送邮件Transpot
*
* */
//1 获得连接对象
Properties props=new Properties();
props.setProperty("mail.host", "localhost");
Session session =Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("service@shop.com","111");
}
});
//2创建邮件对象
Message message=new MimeMessage(session);
try {
message.setFrom(new InternetAddress("service@shop.com"));
message.setRecipient(RecipientType.TO, new InternetAddress(to));
message.setSubject("来自传智播客的购物系统");
message.setContent("<h1>来之传智商城的购物系统,点下面的连接完成激活</h1> <h3> <a href='http://172.17.215.235:8080/shop/user_active.action?code="+code+"'>http://172.17.215.235:8080/shop/user_active.action?code="+code+"</a></h3>", "text/html;charset=UTF-8");
Transport.send(message);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
sendMail("aaa@shop.com", "11111111111111111");
}
}