mirror of
https://gitee.com/dromara/sms4j.git
synced 2025-12-07 01:18:33 +08:00
feat:邮件重试功能完善;并添加相关日志
This commit is contained in:
parent
c08af08f53
commit
70c49db323
@ -33,9 +33,11 @@ import java.util.Collections;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
public class MailService implements MailClient {
|
public class MailService implements MailClient {
|
||||||
|
|
||||||
|
private static Logger logger = Logger.getLogger("mailLog");
|
||||||
private MailBuild mailBuild;
|
private MailBuild mailBuild;
|
||||||
|
|
||||||
private MailService(MailBuild mailBuild) {
|
private MailService(MailBuild mailBuild) {
|
||||||
@ -61,26 +63,6 @@ public class MailService implements MailClient {
|
|||||||
sendEmail(Collections.singletonList(mailAddress), title, body, files);
|
sendEmail(Collections.singletonList(mailAddress), title, body, files);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void sendEmail(String mailAddress, String title, String body, String zipName, Map<String, String> files) {
|
|
||||||
try {
|
|
||||||
Message message = mailBuild.getMessage();
|
|
||||||
message.setRecipients(Message.RecipientType.TO, mailBuild.eliminate(Convert.toList(String.class, mailAddress)));
|
|
||||||
message.setSubject(title);
|
|
||||||
if (StrUtil.isNotBlank(body)) {
|
|
||||||
message.setText(body);
|
|
||||||
}
|
|
||||||
if (files != null && files.size() != 0) {
|
|
||||||
Multipart multipart = new MimeMultipart();
|
|
||||||
zipFiles(multipart, zipName, files);
|
|
||||||
message.setContent(multipart);
|
|
||||||
}
|
|
||||||
Transport.send(message);
|
|
||||||
} catch (MessagingException | IOException e) {
|
|
||||||
throw new MailException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendEmail(List<String> mailAddress, String title, String body, Map<String, String> files) {
|
public void sendEmail(List<String> mailAddress, String title, String body, Map<String, String> files) {
|
||||||
sendEmail(mailAddress, title, body, null, null, files);
|
sendEmail(mailAddress, title, body, null, null, files);
|
||||||
@ -91,27 +73,6 @@ public class MailService implements MailClient {
|
|||||||
sendEmail(Collections.singletonList(mailAddress), title, body, cc, bcc, files);
|
sendEmail(Collections.singletonList(mailAddress), title, body, cc, bcc, files);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void sendEmail(List<String> mailAddress, String title, String body, List<String> cc, List<String> bcc, Map<String, String> files) {
|
|
||||||
try {
|
|
||||||
Message message = mailBuild.getMessage();
|
|
||||||
message.setRecipients(Message.RecipientType.TO, mailBuild.eliminate(mailAddress));
|
|
||||||
message.setSubject(title);
|
|
||||||
if (StrUtil.isNotBlank(body)) {
|
|
||||||
message.setText(body);
|
|
||||||
}
|
|
||||||
if (files != null && files.size() != 0) {
|
|
||||||
Multipart multipart = new MimeMultipart();
|
|
||||||
forFiles(multipart, files);
|
|
||||||
message.setContent(multipart);
|
|
||||||
}
|
|
||||||
addCC(cc, bcc, message);
|
|
||||||
Transport.send(message);
|
|
||||||
} catch (MessagingException e) {
|
|
||||||
throw new MailException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendEmail(MailMessage mailMessage) {
|
public void sendEmail(MailMessage mailMessage) {
|
||||||
sendEmail(mailMessage.getMailAddress(),
|
sendEmail(mailMessage.getMailAddress(),
|
||||||
@ -187,38 +148,6 @@ public class MailService implements MailClient {
|
|||||||
sendHtml(Collections.singletonList(mailAddress), title, body, htmlName, parameter, files);
|
sendHtml(Collections.singletonList(mailAddress), title, body, htmlName, parameter, files);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void sendHtml(String mailAddress, String title, String body, String htmlName, Map<String, String> parameter, String zipName, Map<String, String> files) {
|
|
||||||
try {
|
|
||||||
Message message = mailBuild.getMessage();
|
|
||||||
message.setRecipients(Message.RecipientType.TO, mailBuild.eliminate(Convert.toList(String.class, mailAddress)));
|
|
||||||
message.setSubject(title);
|
|
||||||
|
|
||||||
Multipart multipart = new MimeMultipart("alternative");
|
|
||||||
//读取模板并进行变量替换
|
|
||||||
List<String> strings = HtmlUtil.replacePlaceholder(HtmlUtil.readHtml(htmlName), parameter);
|
|
||||||
//拼合HTML数据
|
|
||||||
String htmlData = HtmlUtil.pieceHtml(strings);
|
|
||||||
if (StrUtil.isNotBlank(body)) {
|
|
||||||
// 创建文本正文部分
|
|
||||||
MimeBodyPart textPart = new MimeBodyPart();
|
|
||||||
textPart.setText(body);
|
|
||||||
multipart.addBodyPart(textPart);
|
|
||||||
}
|
|
||||||
//添加附件
|
|
||||||
if (MapUtil.isNotEmpty(files)) {
|
|
||||||
zipFiles(multipart, zipName, files);
|
|
||||||
}
|
|
||||||
MimeBodyPart htmlPart = new MimeBodyPart();
|
|
||||||
htmlPart.setContent(htmlData, "text/html;charset=UTF-8");
|
|
||||||
multipart.addBodyPart(htmlPart);
|
|
||||||
message.setContent(multipart);
|
|
||||||
Transport.send(message);
|
|
||||||
} catch (MessagingException | IOException e) {
|
|
||||||
throw new MailException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendHtml(String mailAddress, String title, String body, String htmlName, Parameter parameter, String zipName, Map<String, String> files) {
|
public void sendHtml(String mailAddress, String title, String body, String htmlName, Parameter parameter, String zipName, Map<String, String> files) {
|
||||||
sendHtml(mailAddress, title, body, htmlName, ReflectUtil.getValues(parameter), zipName, files);
|
sendHtml(mailAddress, title, body, htmlName, ReflectUtil.getValues(parameter), zipName, files);
|
||||||
@ -364,8 +293,11 @@ public class MailService implements MailClient {
|
|||||||
try {
|
try {
|
||||||
Message message = messageBuild(mailAddress, title, body, html, parameter, files, cc, bcc);
|
Message message = messageBuild(mailAddress, title, body, html, parameter, files, cc, bcc);
|
||||||
Transport.send(message);
|
Transport.send(message);
|
||||||
|
logger.info("邮件发送成功!^_^");
|
||||||
} catch (MessagingException e) {
|
} catch (MessagingException e) {
|
||||||
ReSend(mailAddress,
|
// 防止 maxRetries 数值小于0带来的其他问题
|
||||||
|
if (mailBuild.getMaxRetries() > 0){
|
||||||
|
ReSendList(mailAddress,
|
||||||
title,
|
title,
|
||||||
body,
|
body,
|
||||||
html,
|
html,
|
||||||
@ -373,10 +305,62 @@ public class MailService implements MailClient {
|
|||||||
files,
|
files,
|
||||||
cc,
|
cc,
|
||||||
bcc);
|
bcc);
|
||||||
|
} else {
|
||||||
|
logger.warning(e.getMessage());
|
||||||
|
throw new MailException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ReSend(List<String> mailAddress,
|
@Override
|
||||||
|
public void sendEmail(List<String> mailAddress, String title, String body, List<String> cc, List<String> bcc, Map<String, String> files) {
|
||||||
|
try {
|
||||||
|
Message message = messageBuild(mailAddress, title, body, cc, bcc, files);
|
||||||
|
Transport.send(message);
|
||||||
|
logger.info("邮件发送成功!^_^");
|
||||||
|
} catch (MessagingException e) {
|
||||||
|
if (mailBuild.getMaxRetries() > 0) {
|
||||||
|
ReSendList(mailAddress, title, body, cc, bcc, files);
|
||||||
|
} else {
|
||||||
|
logger.warning(e.getMessage());
|
||||||
|
throw new MailException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendEmail(String mailAddress, String title, String body, String zipName, Map<String, String> files) {
|
||||||
|
try {
|
||||||
|
Message message = messageBuild(mailAddress, title, body, zipName, files);
|
||||||
|
Transport.send(message);
|
||||||
|
logger.info("邮件发送成功!^_^");
|
||||||
|
} catch (MessagingException | IOException e) {
|
||||||
|
if (mailBuild.getMaxRetries() > 1) {
|
||||||
|
ReSend(mailAddress, title, body, zipName, files);
|
||||||
|
} else {
|
||||||
|
logger.warning(e.getMessage());
|
||||||
|
throw new MailException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendHtml(String mailAddress, String title, String body, String htmlName, Map<String, String> parameter, String zipName, Map<String, String> files) {
|
||||||
|
try {
|
||||||
|
Message message = messageBuild(mailAddress, title, body, htmlName, parameter, zipName, files);
|
||||||
|
Transport.send(message);
|
||||||
|
logger.info("邮件发送成功!^_^");
|
||||||
|
} catch (MessagingException | IOException e) {
|
||||||
|
if (mailBuild.getMaxRetries() > 1) {
|
||||||
|
ReSend(mailAddress, title, body, htmlName, parameter, zipName, files);
|
||||||
|
} else {
|
||||||
|
logger.warning(e.getMessage());
|
||||||
|
throw new MailException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ReSendList(List<String> mailAddress,
|
||||||
String title,
|
String title,
|
||||||
String body,
|
String body,
|
||||||
List<String> html,
|
List<String> html,
|
||||||
@ -385,12 +369,18 @@ public class MailService implements MailClient {
|
|||||||
List<String> cc,
|
List<String> cc,
|
||||||
List<String> bcc) {
|
List<String> bcc) {
|
||||||
int maxRetries = mailBuild.getMaxRetries();
|
int maxRetries = mailBuild.getMaxRetries();
|
||||||
int retryCount = 0;
|
int retryCount = 1; // 初始值为1;则while循环中少发送一次,最后一次发送在判断 retryCount >= maxRetries 这里。
|
||||||
boolean retryOnFailure = true;
|
boolean retryOnFailure = true;
|
||||||
|
|
||||||
while (retryOnFailure && retryCount < maxRetries) {
|
while (retryOnFailure && retryCount < maxRetries) {
|
||||||
try {
|
try {
|
||||||
Message message = messageBuild(mailAddress, title, body, html, parameter, files, cc, bcc);
|
logger.warning("邮件第 {" + retryCount + "} 次重新发送");
|
||||||
|
Message message;
|
||||||
|
if (html != null || parameter != null){
|
||||||
|
message = messageBuild(mailAddress, title, body, html, parameter, files, cc, bcc);
|
||||||
|
} else {
|
||||||
|
message = messageBuild(mailAddress, title, body, cc, bcc, files);
|
||||||
|
}
|
||||||
Transport.send(message);
|
Transport.send(message);
|
||||||
retryOnFailure = false; // 发送成功,停止重试
|
retryOnFailure = false; // 发送成功,停止重试
|
||||||
} catch (MessagingException e) {
|
} catch (MessagingException e) {
|
||||||
@ -404,11 +394,91 @@ public class MailService implements MailClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (retryCount >= maxRetries) {
|
if (retryCount >= maxRetries && retryOnFailure) {
|
||||||
throw new MailException(new MailException());
|
try {
|
||||||
|
Message message;
|
||||||
|
if (html != null || parameter != null){
|
||||||
|
message = messageBuild(mailAddress, title, body, html, parameter, files, cc, bcc);
|
||||||
|
} else {
|
||||||
|
message = messageBuild(mailAddress, title, body, cc, bcc, files);
|
||||||
|
}
|
||||||
|
Transport.send(message);
|
||||||
|
} catch (MessagingException e) {
|
||||||
|
throw new MailException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void ReSendList(List<String> mailAddress,
|
||||||
|
String title,
|
||||||
|
String body,
|
||||||
|
List<String> cc,
|
||||||
|
List<String> bcc,
|
||||||
|
Map<String, String> files) {
|
||||||
|
ReSendList(mailAddress, title, body, null, null, files, cc, bcc);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ReSend(String mailAddress,
|
||||||
|
String title,
|
||||||
|
String body,
|
||||||
|
String htmlName,
|
||||||
|
Map<String, String> parameter,
|
||||||
|
String zipName,
|
||||||
|
Map<String, String> files) {
|
||||||
|
int maxRetries = mailBuild.getMaxRetries();
|
||||||
|
int retryCount = 1; // 初始值为1;则while循环中少发送一次,最后一次发送在判断 retryCount >= maxRetries 这里。
|
||||||
|
boolean retryOnFailure = true;
|
||||||
|
|
||||||
|
while (retryOnFailure && retryCount < maxRetries) {
|
||||||
|
try {
|
||||||
|
logger.warning("邮件第 {" + retryCount + "} 次重新发送");
|
||||||
|
if (htmlName != null || parameter != null){
|
||||||
|
Message message = messageBuild(mailAddress, title, body, htmlName, parameter, zipName, files);
|
||||||
|
Transport.send(message);
|
||||||
|
} else {
|
||||||
|
Message message = messageBuild(mailAddress, title, body, zipName, files);
|
||||||
|
Transport.send(message);
|
||||||
|
}
|
||||||
|
retryOnFailure = false; // 发送成功,停止重试
|
||||||
|
} catch (MessagingException e) {
|
||||||
|
retryCount++;
|
||||||
|
try {
|
||||||
|
// 间隔秒数
|
||||||
|
TimeUnit.SECONDS.sleep(mailBuild.getRetryInterval());
|
||||||
|
} catch (InterruptedException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (retryCount >= maxRetries && retryOnFailure) {
|
||||||
|
try {
|
||||||
|
if (htmlName != null || parameter != null){
|
||||||
|
Message message = messageBuild(mailAddress, title, body, htmlName, parameter, zipName, files);
|
||||||
|
Transport.send(message);
|
||||||
|
} else {
|
||||||
|
Message message = messageBuild(mailAddress, title, body, zipName, files);
|
||||||
|
Transport.send(message);
|
||||||
|
}
|
||||||
|
} catch (MessagingException e) {
|
||||||
|
throw new MailException(e);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ReSend(String mailAddress,
|
||||||
|
String title,
|
||||||
|
String body,
|
||||||
|
String zipName,
|
||||||
|
Map<String, String> files) {
|
||||||
|
ReSend(mailAddress, title, body, null, null, zipName, files);
|
||||||
|
}
|
||||||
|
|
||||||
|
// messageBuild 方法重载
|
||||||
private Message messageBuild(List<String> mailAddress,
|
private Message messageBuild(List<String> mailAddress,
|
||||||
String title,
|
String title,
|
||||||
String body,
|
String body,
|
||||||
@ -445,6 +515,79 @@ public class MailService implements MailClient {
|
|||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Message messageBuild(List<String> mailAddress,
|
||||||
|
String title,
|
||||||
|
String body,
|
||||||
|
List<String> cc,
|
||||||
|
List<String> bcc,
|
||||||
|
Map<String, String> files) throws MessagingException {
|
||||||
|
Message message = mailBuild.getMessage();
|
||||||
|
message.setRecipients(Message.RecipientType.TO, mailBuild.eliminate(mailAddress));
|
||||||
|
message.setSubject(title);
|
||||||
|
if (StrUtil.isNotBlank(body)) {
|
||||||
|
message.setText(body);
|
||||||
|
}
|
||||||
|
if (files != null && files.size() != 0) {
|
||||||
|
Multipart multipart = new MimeMultipart();
|
||||||
|
forFiles(multipart, files);
|
||||||
|
message.setContent(multipart);
|
||||||
|
}
|
||||||
|
addCC(cc, bcc, message);
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Message messageBuild(String mailAddress,
|
||||||
|
String title,
|
||||||
|
String body,
|
||||||
|
String zipName,
|
||||||
|
Map<String, String> files) throws MessagingException, IOException {
|
||||||
|
Message message = mailBuild.getMessage();
|
||||||
|
message.setRecipients(Message.RecipientType.TO, mailBuild.eliminate(Convert.toList(String.class, mailAddress)));
|
||||||
|
message.setSubject(title);
|
||||||
|
if (StrUtil.isNotBlank(body)) {
|
||||||
|
message.setText(body);
|
||||||
|
}
|
||||||
|
if (files != null && files.size() != 0) {
|
||||||
|
Multipart multipart = new MimeMultipart();
|
||||||
|
zipFiles(multipart, zipName, files);
|
||||||
|
message.setContent(multipart);
|
||||||
|
}
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Message messageBuild(String mailAddress,
|
||||||
|
String title,
|
||||||
|
String body,
|
||||||
|
String htmlName,
|
||||||
|
Map<String, String> parameter,
|
||||||
|
String zipName,
|
||||||
|
Map<String, String> files) throws MessagingException, IOException {
|
||||||
|
Message message = mailBuild.getMessage();
|
||||||
|
message.setRecipients(Message.RecipientType.TO, mailBuild.eliminate(Convert.toList(String.class, mailAddress)));
|
||||||
|
message.setSubject(title);
|
||||||
|
|
||||||
|
Multipart multipart = new MimeMultipart("alternative");
|
||||||
|
//读取模板并进行变量替换
|
||||||
|
List<String> strings = HtmlUtil.replacePlaceholder(HtmlUtil.readHtml(htmlName), parameter);
|
||||||
|
//拼合HTML数据
|
||||||
|
String htmlData = HtmlUtil.pieceHtml(strings);
|
||||||
|
if (StrUtil.isNotBlank(body)) {
|
||||||
|
// 创建文本正文部分
|
||||||
|
MimeBodyPart textPart = new MimeBodyPart();
|
||||||
|
textPart.setText(body);
|
||||||
|
multipart.addBodyPart(textPart);
|
||||||
|
}
|
||||||
|
//添加附件
|
||||||
|
if (MapUtil.isNotEmpty(files)) {
|
||||||
|
zipFiles(multipart, zipName, files);
|
||||||
|
}
|
||||||
|
MimeBodyPart htmlPart = new MimeBodyPart();
|
||||||
|
htmlPart.setContent(htmlData, "text/html;charset=UTF-8");
|
||||||
|
multipart.addBodyPart(htmlPart);
|
||||||
|
message.setContent(multipart);
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
private void addCC(List<String> cc, List<String> bcc, Message message) throws MessagingException {
|
private void addCC(List<String> cc, List<String> bcc, Message message) throws MessagingException {
|
||||||
if (cc != null && cc.size() > 0) {
|
if (cc != null && cc.size() > 0) {
|
||||||
message.addRecipients(Message.RecipientType.CC, mailBuild.eliminate(cc));
|
message.addRecipients(Message.RecipientType.CC, mailBuild.eliminate(cc));
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user