packagetextAnalysis.util;importtextAnalysis.dto.MailInfo;import javax.mail.*;importjavax.mail.internet.InternetAddress;importjavax.mail.internet.MimeBodyPart;importjavax.mail.internet.MimeMessage;importjavax.mail.internet.MimeMultipart;importjava.util.Date;importjava.util.Properties;/*** Created by 草帽boy on 2016/12/8.*/
public classMailSendUtil {private final static String host = "smtp.163.com"; //163的服务器private final static String formName = "YYYXXX@163.com";//你的邮箱private final static String password = "***********"; //授权码private final static String replayAddress = "YYYXXX@163.com"; //你的邮箱public static void sendHtmlMail(MailInfo info)throwsException{
info.setHost(host);
info.setFormName(formName);
info.setFormPassword(password);//网易邮箱的授权码~不一定是密码
info.setReplayAddress(replayAddress);
Message message=getMessage(info);//MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart mainPart = newMimeMultipart();//创建一个包含HTML内容的MimeBodyPart
BodyPart html = newMimeBodyPart();//设置HTML内容
html.setContent(info.getContent(), "text/html; charset=utf-8");
mainPart.addBodyPart(html);//将MiniMultipart对象设置为邮件内容
message.setContent(mainPart);
Transport.send(message);
}public static void sendTextMail(MailInfo info) throwsException {
info.setHost(host);
info.setFormName(formName);
info.setFormPassword(password);//网易邮箱的授权码~不一定是密码
info.setReplayAddress(replayAddress);
Message message=getMessage(info);//消息发送的内容
message.setText(info.getContent());
Transport.send(message);
}private static Message getMessage(MailInfo info) throwsException{final Properties p =System.getProperties() ;
p.setProperty("mail.smtp.host", info.getHost());
p.setProperty("mail.smtp.auth", "true");
p.setProperty("mail.smtp.user", info.getFormName());
p.setProperty("mail.smtp.pass", info.getFormPassword());//根据邮件会话属性和密码验证器构造一个发送邮件的session
Session session = Session.getInstance(p, newAuthenticator(){protectedPasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(p.getProperty("mail.smtp.user"),p.getProperty("mail.smtp.pass"));
}
});
session.setDebug(true);
Message message= newMimeMessage(session);//消息发送的主题
message.setSubject(info.getSubject());//接受消息的人
message.setReplyTo(InternetAddress.parse(info.getReplayAddress()));//消息的发送者
message.setFrom(new InternetAddress(p.getProperty("mail.smtp.user"),"余高峰"));//创建邮件的接收者地址,并设置到邮件消息中
message.setRecipient(Message.RecipientType.TO,newInternetAddress(info.getToAddress()));//消息发送的时间
message.setSentDate(newDate());returnmessage ;
}
}