发送过程
代码工具类
package untils;
import com.sun.mail.util.MailSSLSocketFactory;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
/**
* Created by King on 2017/6/27.
*/
public class MailUntils {
/**
* 发送邮件 amlowhazdeygbgec
*@param to 收件人qq邮箱
*@param code 激活码
*/
public static void sendMail(String to,String code){
Properties properties = new Properties();
properties.setProperty("mail.smtp.host", "smtp.qq.com");
properties.setProperty("mail.smtp.auth", "true");
Session session = Session.getInstance(properties, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("328679279@qq.com", XXXXXXXXXXXXXXXX);
}
});
try {
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.ssl.socketFactory", sf);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("328679279@qq.com"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("账号激活");
String content = "<html><head></head><body><h1>这是一封激活邮件,激活请点击以下链接</h1><h3><a href='http://localhost:8080/register?code="
+ code + "'>http://localhost:8080/register?code=" + code
+ "</href></h3></body></html>";
message.setContent(content, "text/html;charset=UTF-8");
Transport.send(message);
} catch (Exception e) {
e.printStackTrace();
}
}
}