merge
This commit is contained in:
@@ -3,6 +3,8 @@ package com.xwbing.util;
|
||||
|
||||
import com.xwbing.domain.entity.model.EmailModel;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.mail.*;
|
||||
import javax.mail.internet.*;
|
||||
@@ -14,6 +16,8 @@ import java.util.Properties;
|
||||
* 说明: 邮箱工具类
|
||||
*/
|
||||
public class EmailUtil {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(EmailUtil.class);
|
||||
|
||||
/**
|
||||
* 发送邮件
|
||||
*
|
||||
@@ -22,16 +26,13 @@ public class EmailUtil {
|
||||
*/
|
||||
public static boolean sendTextEmail(EmailModel emailModel) {
|
||||
try {
|
||||
boolean result = true;
|
||||
// 1. 创建参数配置, 用于连接邮件服务器的参数配置
|
||||
Properties props = new Properties();// 用于连接邮件服务器的参数配置(发送邮件时才需要用到)
|
||||
String serverHost = emailModel.getServerHost();// 设置发送邮件的邮件服务器的属性(这里使用网易的smtp服务器)
|
||||
if (StringUtils.isEmpty(serverHost)) {
|
||||
result = false;
|
||||
throw new RuntimeException("发送邮件主机不能为空");
|
||||
}
|
||||
props.setProperty("mail.transport.protocol",
|
||||
emailModel.getProtocol());// 使用的协议(JavaMail规范要求)
|
||||
props.setProperty("mail.transport.protocol", emailModel.getProtocol());// 使用的协议(JavaMail规范要求)
|
||||
props.setProperty("mail.smtp.host", serverHost); // 发件人的邮箱的
|
||||
// SMTP服务器地址
|
||||
if (emailModel.getServerPort() != null) {
|
||||
@@ -46,17 +47,14 @@ public class EmailUtil {
|
||||
// From: 发件人
|
||||
String fromEmail = emailModel.getFromEmail();
|
||||
if (StringUtils.isEmpty(fromEmail)) {
|
||||
result = false;
|
||||
throw new RuntimeException("发送邮箱不能为空");
|
||||
}
|
||||
message.setFrom(new InternetAddress(emailModel.getFromEmail()));
|
||||
// To: 收件人
|
||||
if (StringUtils.isEmpty(emailModel.getToEmail())) {
|
||||
result = false;
|
||||
throw new RuntimeException("接收邮箱不能为空");
|
||||
}
|
||||
InternetAddress[] toEmailList = InternetAddress.parse(emailModel
|
||||
.getToEmail());// toEmail格式为"aaa,bbb,..."或"aaa"
|
||||
InternetAddress[] toEmailList = InternetAddress.parse(emailModel.getToEmail());// toEmail格式为"aaa,bbb,..."或"aaa"
|
||||
message.setRecipients(Message.RecipientType.TO, toEmailList);
|
||||
/*
|
||||
* message.addRecipient(MimeMessage.RecipientType.TO,new
|
||||
@@ -68,7 +66,6 @@ public class EmailUtil {
|
||||
*/
|
||||
// Subject: 邮件主题
|
||||
if (StringUtils.isEmpty(emailModel.getSubject())) {
|
||||
result = false;
|
||||
throw new RuntimeException("邮件主题不能为空");
|
||||
}
|
||||
message.setSubject(emailModel.getSubject());
|
||||
@@ -86,7 +83,6 @@ public class EmailUtil {
|
||||
Transport transport = session.getTransport("smtp");
|
||||
// 5. 使用 邮箱账号 和 密码 连接邮件服务器
|
||||
if (StringUtils.isEmpty(emailModel.getPassword())) {
|
||||
result = false;
|
||||
throw new RuntimeException("发送邮箱密码不能为空");
|
||||
}
|
||||
transport.connect(serverHost, fromEmail, emailModel.getPassword());// 这里认证的邮箱必须与
|
||||
@@ -96,13 +92,10 @@ public class EmailUtil {
|
||||
transport.sendMessage(message, message.getAllRecipients());
|
||||
// 7. 关闭连接
|
||||
transport.close();
|
||||
return result;
|
||||
} catch (AddressException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
return true;
|
||||
} catch (MessagingException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
LOGGER.error(e.getMessage());
|
||||
throw new RuntimeException("发送邮件失败");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user