feat:邮件发送失败重试功能完成

This commit is contained in:
tiejiaxiaobao 2023-07-18 22:02:03 +08:00
parent ea9bd0f626
commit c08af08f53
3 changed files with 99 additions and 25 deletions

View File

@ -52,4 +52,16 @@ public class MailSmtpConfig {
* */ * */
@Builder.Default @Builder.Default
private String isAuth = "true"; private String isAuth = "true";
/**
* 重试间隔单位默认为5秒
*/
@Builder.Default
private int retryInterval = 5;
/**
* 重试次数默认为1次
*/
@Builder.Default
private int maxRetries = 1;
} }

View File

@ -27,6 +27,10 @@ public class MailBuild {
private Blacklist blacklist; private Blacklist blacklist;
private int retryInterval;
private int maxRetries;
private MailBuild(MailSmtpConfig config) throws MessagingException { private MailBuild(MailSmtpConfig config) throws MessagingException {
Properties props = new Properties(); Properties props = new Properties();
props.put("mail.smtp.host", config.getSmtpServer()); props.put("mail.smtp.host", config.getSmtpServer());
@ -42,6 +46,8 @@ public class MailBuild {
this.message = new MimeMessage(session); this.message = new MimeMessage(session);
this.message.setFrom(new InternetAddress(config.getFromAddress())); this.message.setFrom(new InternetAddress(config.getFromAddress()));
this.config = config; this.config = config;
this.retryInterval = config.getRetryInterval();
this.maxRetries = config.getMaxRetries();
} }
private MailBuild(MailSmtpConfig config,Blacklist blacklist)throws MessagingException{ private MailBuild(MailSmtpConfig config,Blacklist blacklist)throws MessagingException{
@ -61,6 +67,8 @@ public class MailBuild {
this.message.setFrom(new InternetAddress(config.getFromAddress())); this.message.setFrom(new InternetAddress(config.getFromAddress()));
this.config = config; this.config = config;
this.blacklist = blacklist; this.blacklist = blacklist;
this.retryInterval = config.getRetryInterval();
this.maxRetries = config.getMaxRetries();
} }
public static MailClient build(MailSmtpConfig config) throws MessagingException { public static MailClient build(MailSmtpConfig config) throws MessagingException {

View File

@ -32,6 +32,7 @@ import java.io.InputStream;
import java.util.Collections; 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;
public class MailService implements MailClient { public class MailService implements MailClient {
@ -361,6 +362,61 @@ public class MailService implements MailClient {
List<String> cc, List<String> cc,
List<String> bcc) { List<String> bcc) {
try { try {
Message message = messageBuild(mailAddress, title, body, html, parameter, files, cc, bcc);
Transport.send(message);
} catch (MessagingException e) {
ReSend(mailAddress,
title,
body,
html,
parameter,
files,
cc,
bcc);
}
}
private void ReSend(List<String> mailAddress,
String title,
String body,
List<String> html,
Map<String, String> parameter,
Map<String, String> files,
List<String> cc,
List<String> bcc) {
int maxRetries = mailBuild.getMaxRetries();
int retryCount = 0;
boolean retryOnFailure = true;
while (retryOnFailure && retryCount < maxRetries) {
try {
Message message = messageBuild(mailAddress, title, body, html, parameter, files, cc, bcc);
Transport.send(message);
retryOnFailure = false; // 发送成功停止重试
} catch (MessagingException e) {
retryCount++;
try {
// 间隔秒数
TimeUnit.SECONDS.sleep(mailBuild.getRetryInterval());
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
if (retryCount >= maxRetries) {
throw new MailException(new MailException());
}
}
private Message messageBuild(List<String> mailAddress,
String title,
String body,
List<String> html,
Map<String, String> parameter,
Map<String, String> files,
List<String> cc,
List<String> bcc) throws MessagingException {
Message message = mailBuild.getMessage(); Message message = mailBuild.getMessage();
message.setRecipients(Message.RecipientType.TO, mailBuild.eliminate(mailAddress)); message.setRecipients(Message.RecipientType.TO, mailBuild.eliminate(mailAddress));
message.setSubject(title); message.setSubject(title);
@ -380,15 +436,13 @@ public class MailService implements MailClient {
if (files != null && files.size() != 0) { if (files != null && files.size() != 0) {
forFiles(multipart, files); forFiles(multipart, files);
} }
MimeBodyPart htmlPart = new MimeBodyPart(); MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(htmlData, "text/html;charset=UTF-8"); htmlPart.setContent(htmlData, "text/html;charset=UTF-8");
addCC(cc, bcc, message); addCC(cc, bcc, message);
multipart.addBodyPart(htmlPart); multipart.addBodyPart(htmlPart);
message.setContent(multipart); message.setContent(multipart);
Transport.send(message); return message;
} catch (MessagingException e) {
throw new MailException(e);
}
} }
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 {