This commit is contained in:
xiangwbs
2017-11-21 22:35:51 +08:00
parent 2495cb5f65
commit f4023eef3c
8 changed files with 47 additions and 67 deletions
@@ -2,7 +2,6 @@ package com.xwbing.util;
import com.xwbing.domain.entity.model.EmailModel;
import com.xwbing.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import javax.mail.*;
@@ -29,7 +28,7 @@ public class EmailUtil {
String serverHost = emailModel.getServerHost();// 设置发送邮件的邮件服务器的属性(这里使用网易的smtp服务器)
if (StringUtils.isEmpty(serverHost)) {
result = false;
throw new BusinessException("发送邮件主机不能为空");
throw new RuntimeException("发送邮件主机不能为空");
}
props.setProperty("mail.transport.protocol",
emailModel.getProtocol());// 使用的协议(JavaMail规范要求)
@@ -48,13 +47,13 @@ public class EmailUtil {
String fromEmail = emailModel.getFromEmail();
if (StringUtils.isEmpty(fromEmail)) {
result = false;
throw new BusinessException("发送邮箱不能为空");
throw new RuntimeException("发送邮箱不能为空");
}
message.setFrom(new InternetAddress(emailModel.getFromEmail()));
// To: 收件人
if (StringUtils.isEmpty(emailModel.getToEmail())) {
result = false;
throw new BusinessException("接收邮箱不能为空");
throw new RuntimeException("接收邮箱不能为空");
}
InternetAddress[] toEmailList = InternetAddress.parse(emailModel
.getToEmail());// toEmail格式为"aaa,bbb,..."或"aaa"
@@ -70,7 +69,7 @@ public class EmailUtil {
// Subject: 邮件主题
if (StringUtils.isEmpty(emailModel.getSubject())) {
result = false;
throw new BusinessException("邮件主题不能为空");
throw new RuntimeException("邮件主题不能为空");
}
message.setSubject(emailModel.getSubject());
// 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
@@ -88,7 +87,7 @@ public class EmailUtil {
// 5. 使用 邮箱账号 和 密码 连接邮件服务器
if (StringUtils.isEmpty(emailModel.getPassword())) {
result = false;
throw new BusinessException("发送邮箱密码不能为空");
throw new RuntimeException("发送邮箱密码不能为空");
}
transport.connect(serverHost, fromEmail, emailModel.getPassword());// 这里认证的邮箱必须与
// message中的发件人邮箱一致,否则报错