packagecom.microClass.util;importcom.sun.mail.util.MailSSLSocketFactory;import javax.mail.*;importjavax.mail.internet.InternetAddress;importjavax.mail.internet.MimeMessage;importjava.security.GeneralSecurityException;importjava.util.Properties;/*** Created by ause on 2017-08-25.*/
public classMailUtil {//public final static int ERA = 0;//设置邮件服务器主机名
public final static String HOST = "smtp.qq.com";//发送邮件协议名称
public final static String PROTOCOL = "smtp";//发送人的邮箱地址
public final static String SEND_ADDRESS = "1593139675@qq.com";//授权码Authorization code//ndtqsiupcmxcbabf
public final static String AUTHORIZATION_CODE = "ndtqsiupcmxcbabf";public static void send(String title, String body, String receiveAddress) throwsMessagingException, GeneralSecurityException {
Properties props= newProperties();//开启debug调试
props.setProperty("mail.debug", "true");//发送服务器需要身份验证
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.host", HOST);
props.setProperty("mail.transport.protocol", PROTOCOL);
MailSSLSocketFactory sf= newMailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.socketFactory", sf);
Session session=Session.getInstance(props);
Message msg= newMimeMessage(session);
msg.setSubject(title);
msg.setText(body);
msg.setFrom(newInternetAddress(SEND_ADDRESS));
Transport transport=session.getTransport();
transport.connect(HOST, SEND_ADDRESS, AUTHORIZATION_CODE);
Address[] addresses= {newInternetAddress(receiveAddress)};
transport.sendMessage(msg, addresses);
transport.close();
}public static void registerMailValidate(String receiveAddress,String code) throwsGeneralSecurityException, MessagingException {
String title="微辅导注册邮件认证";//String body="你正在注册为微辅导用户:请点击连接地址完成注册:http://127.0.0.1:8080/register/mailPage?code="+code;
String str=" 认证";
send(title, str, receiveAddress);
}public static voidmain(String[] args) {try{
registerMailValidate("851298348@qq.com","9999");
}catch(GeneralSecurityException e) {
e.printStackTrace();
}catch(MessagingException e) {
e.printStackTrace();
}
}
}