package com.nanometer.smartlab.util;
|
|
import com.nanometer.smartlab.exception.BusinessException;
|
import com.nanometer.smartlab.exception.ExceptionEnumCode;
|
import org.apache.log4j.Logger;
|
|
import javax.mail.Authenticator;
|
import javax.mail.Message;
|
import javax.mail.PasswordAuthentication;
|
import javax.mail.Session;
|
import javax.mail.Transport;
|
import javax.mail.internet.InternetAddress;
|
import javax.mail.internet.MimeMessage;
|
import java.text.MessageFormat;
|
import java.text.SimpleDateFormat;
|
import java.util.Date;
|
import java.util.List;
|
import java.util.Properties;
|
import com.sun.mail.util.MailSSLSocketFactory;
|
|
public class EmailSend {
|
|
private static Logger logger = Logger.getLogger(EmailSend.class);
|
|
public static void send(String to, String applyCode) {
|
|
Transport transport = null;
|
try {
|
|
EmailConfigService cs = SpringUtil.getBean("emailConfigService", EmailConfigService.class);
|
String smtp = cs.getSmtpHost();
|
String user = cs.getSmtpUser();
|
String password = cs.getSmtpPwd();
|
String from = cs.getSmtpFrom();
|
String subject = cs.getSmtpSubject();
|
String content = MessageFormat.format(cs.getSmtpContent(), applyCode);
|
|
Properties props = new Properties();
|
props.put("mail.smtp.host", smtp);
|
props.put("mail.smtp.auth", "true");
|
Session ssn = Session.getInstance(props, new Authenticator() {
|
@Override
|
protected PasswordAuthentication getPasswordAuthentication() {
|
return new PasswordAuthentication(user, password);
|
}
|
});
|
// 由邮件会话新建一个消息对象
|
MimeMessage message = new MimeMessage(ssn);
|
// 发件人的邮件地址
|
InternetAddress fromAddress = new InternetAddress(from);
|
// 设置发件人
|
message.setFrom(fromAddress);
|
// 收件人的邮件地址
|
InternetAddress toAddress = new InternetAddress(to);
|
// 设置收件人
|
message.addRecipient(Message.RecipientType.TO, toAddress);
|
// 设置标题
|
message.setSubject(subject);
|
// 设置内容
|
message.setText(content);
|
// 设置发信时间
|
message.setSentDate(new Date());
|
|
transport = ssn.getTransport("smtp");
|
transport.connect(smtp, user, password);
|
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
|
} catch (Exception ex) {
|
logger.error(ex.getMessage(), ex);
|
throw new BusinessException(ExceptionEnumCode.MAIL_SEND_FAIL, "邮件发送失败。", ex);
|
} finally {
|
if (transport != null) {
|
try {
|
transport.close();
|
} catch (Exception ex) {
|
|
}
|
}
|
}
|
}
|
|
|
public static void sendArrivalEmail(List<String> revicerList, String msg, String title){
|
EmailConfigService cs = SpringUtil.getBean("emailConfigService", EmailConfigService.class);
|
String sender = cs.getZkySmtpUser();/*发送人*/
|
String password = cs.getZkySmtpPwd();
|
|
String ZkySmtp = "smtp.cstnet.cn"; /*中科院邮件服务器*/
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
Properties prop = new Properties();
|
prop.setProperty("mail.host", ZkySmtp);
|
prop.setProperty("mail.transport.protocol", "smtp"); // 邮件发送协议
|
prop.setProperty("mail.smtp.auth", "true"); // 需要验证用户名密码
|
prop.setProperty("mail.smtp.port", "25");
|
|
MailSSLSocketFactory mailSSLSocketFactory = null;
|
try{
|
mailSSLSocketFactory = new MailSSLSocketFactory();
|
mailSSLSocketFactory.setTrustAllHosts(true);
|
}catch (Exception e){
|
logger.error("邮件开启 MailSSLSocketFactory 失败:"+e.getMessage());
|
}
|
|
if (mailSSLSocketFactory != null) {
|
|
prop.put("mail.smtp.ssl.enable", false);
|
prop.put("mail.smtp.ssl.socketFactory", mailSSLSocketFactory);
|
//创建定义整个应用程序所需的环境信息的 Session 对象
|
Session session = Session.getDefaultInstance(prop, new Authenticator() {
|
public PasswordAuthentication getPasswordAuthentication() {
|
//发件人邮件用户名、授权码
|
return new PasswordAuthentication(sender, password);
|
}
|
});
|
|
for (String revicer : revicerList) {
|
try {
|
//2、通过session得到transport对象
|
Transport ts = session.getTransport();
|
//3、使用邮箱的用户名和授权码连上邮件服务器
|
ts.connect(sender, password);
|
try {
|
//4、创建邮件
|
//创建邮件对象
|
MimeMessage message = new MimeMessage(session);
|
//指明邮件的发件人
|
message.setFrom(new InternetAddress(sender));
|
//指明邮件的收件人
|
message.setRecipient(Message.RecipientType.TO, new InternetAddress(revicer));
|
//邮件的标题
|
message.setSubject(sdf.format(new Date())+title);
|
//邮件的文本内容
|
message.setContent(msg, "text/html;charset=UTF-8");
|
//5、发送邮件
|
ts.sendMessage(message, message.getAllRecipients());
|
} catch (Exception e) {
|
logger.error("邮件发送" + "失败:" + e.getMessage());
|
}
|
ts.close();
|
} catch (Exception e) {
|
logger.error("邮件发送失败:" + e.getMessage());
|
}
|
}
|
}
|
logger.error("邮件发送完成" + "时间:" + sdf.format(new Date()));
|
}
|
}
|