/**
* 参数1 邮箱账户 参数2 接受邮箱地址 参数3 邮件内容 参数4 邮件主题 参数5 抄送
*/
private static Boolean sendMessage(EmailAccount account, String toEmail, String mailContext, String subject,String ccEmail){
long begin = System.currentTimeMillis();
try {
//邮件属性
Properties props = setProperties(account);
// 获得邮件会话对象
Session session = Session.getInstance(props);
//设置邮件扩充,标题、发信人、正文
MimeMessage mimeMsg = setMIME(mailContext, subject, account, session, toEmail, ccEmail);
//连接服务器发送邮件
Transport transport = session.getTransport("smtp");
if(account.getPort() == null || account.getPort() == 0) {
//默认端口25
transport.connect(account.getAddress(), account.getUsername(), account.getPassword());
}else {
//指定端口,587或其他可用端口
transport.connect(account.getAddress(),account.getPort(), account.getUsername(), account.getPassword());
}
transport.sendMessage(mimeMsg, mimeMsg.getAllRecipients());
transport.close();
LOGGER.info("sendMessage success,toEmail="+toEmail+",time="+(System.currentTimeMillis()-begin));
return true;
} catch (Exception e) {
LOGGER.info("sendMessage fail!!!,emailAccount=" + account.getUsername() + ",toEmail=" + toEmail + "," +
"time=" + (System.currentTimeMillis() - begin), e);
// 无效地址认为发送成功
if (StringUtils.isBlank(e.getMessage())) {
return false;
}
return e.getMessage().contains("Invalid Addresses");
}
}
/**
* 设置邮件属性
*/
private static Properties setProperties(EmailAccount account) {
Properties props = new Properties();
props.put("mail.smtp.host", account.getAddress()); // 设置SMTP主机
props.put("mail.smtp.quitwait", "false");
props.put("mail.smtp.connectiontimeout", TIME_OUT);
props.put("mail.smtp.timeout", TIME_OUT);
props.put("mail.smtp.auth", "true");
return props;
}
/**
* 发送邮箱账户
*/
public class EmailAccount {
private String address;//发件箱服务器地址
private Integer port ;//邮件服务器端口
private String username;//发件箱用户名
private String password;//发件箱密码
private String alias;//发件人别名
//省略set get 方法
}
/**
* 设置邮件扩充,标题、发信人、正文
*/
private static MimeMessage setMIME(String mailContext, String subject,
EmailAccount account, Session session, String toEmail, String ccEmail) throws Exception,
AddressException, UnsupportedEncodingException {
// 创建MIME邮件对象
MimeMessage mimeMsg = new MimeMessage(session);
//标题
mimeMsg.setSubject(subject);
//发件人
if (StringUtils.isBlank(account.getAlias())) {
mimeMsg.setFrom(new InternetAddress(account.getUsername()));
} else {//带有别名的发件人
mimeMsg.setFrom(new InternetAddress(account.getUsername(), account.getAlias()));
}
// Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象
Multipart mp = new MimeMultipart();
//邮件正文
BodyPart bp = new MimeBodyPart();
//居中展示邮件内容
mailContext = "<div align=center>" + mailContext + "</div>";
bp.setContent(mailContext, "text/html;charset=utf-8");
mp.addBodyPart(bp);
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
//设置抄送
if (StringUtils.isNotBlank(ccEmail) && ccEmail.indexOf("@") != -1) {
mimeMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccEmail));
}
return mimeMsg;
}