mirror of
https://gitee.com/dromara/sms4j.git
synced 2025-12-07 01:18:33 +08:00
feat:邮件发送失败重试功能完成
This commit is contained in:
parent
ea9bd0f626
commit
c08af08f53
@ -52,4 +52,16 @@ public class MailSmtpConfig {
|
||||
* */
|
||||
@Builder.Default
|
||||
private String isAuth = "true";
|
||||
|
||||
/**
|
||||
* 重试间隔(单位:秒),默认为5秒
|
||||
*/
|
||||
@Builder.Default
|
||||
private int retryInterval = 5;
|
||||
|
||||
/**
|
||||
* 重试次数,默认为1次
|
||||
*/
|
||||
@Builder.Default
|
||||
private int maxRetries = 1;
|
||||
}
|
||||
|
||||
@ -27,6 +27,10 @@ public class MailBuild {
|
||||
|
||||
private Blacklist blacklist;
|
||||
|
||||
private int retryInterval;
|
||||
|
||||
private int maxRetries;
|
||||
|
||||
private MailBuild(MailSmtpConfig config) throws MessagingException {
|
||||
Properties props = new Properties();
|
||||
props.put("mail.smtp.host", config.getSmtpServer());
|
||||
@ -42,6 +46,8 @@ public class MailBuild {
|
||||
this.message = new MimeMessage(session);
|
||||
this.message.setFrom(new InternetAddress(config.getFromAddress()));
|
||||
this.config = config;
|
||||
this.retryInterval = config.getRetryInterval();
|
||||
this.maxRetries = config.getMaxRetries();
|
||||
}
|
||||
|
||||
private MailBuild(MailSmtpConfig config,Blacklist blacklist)throws MessagingException{
|
||||
@ -61,6 +67,8 @@ public class MailBuild {
|
||||
this.message.setFrom(new InternetAddress(config.getFromAddress()));
|
||||
this.config = config;
|
||||
this.blacklist = blacklist;
|
||||
this.retryInterval = config.getRetryInterval();
|
||||
this.maxRetries = config.getMaxRetries();
|
||||
}
|
||||
|
||||
public static MailClient build(MailSmtpConfig config) throws MessagingException {
|
||||
|
||||
@ -32,6 +32,7 @@ import java.io.InputStream;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class MailService implements MailClient {
|
||||
|
||||
@ -361,36 +362,89 @@ public class MailService implements MailClient {
|
||||
List<String> cc,
|
||||
List<String> bcc) {
|
||||
try {
|
||||
Message message = mailBuild.getMessage();
|
||||
message.setRecipients(Message.RecipientType.TO, mailBuild.eliminate(mailAddress));
|
||||
message.setSubject(title);
|
||||
|
||||
Multipart multipart = new MimeMultipart("alternative");
|
||||
//读取模板并进行变量替换
|
||||
List<String> strings = HtmlUtil.replacePlaceholder(html, parameter);
|
||||
//拼合HTML数据
|
||||
String htmlData = HtmlUtil.pieceHtml(strings);
|
||||
if (!body.isEmpty()) {
|
||||
// 创建文本正文部分
|
||||
MimeBodyPart textPart = new MimeBodyPart();
|
||||
textPart.setText(body);
|
||||
multipart.addBodyPart(textPart);
|
||||
}
|
||||
//添加附件
|
||||
if (files != null && files.size() != 0) {
|
||||
forFiles(multipart, files);
|
||||
}
|
||||
MimeBodyPart htmlPart = new MimeBodyPart();
|
||||
htmlPart.setContent(htmlData, "text/html;charset=UTF-8");
|
||||
addCC(cc, bcc, message);
|
||||
multipart.addBodyPart(htmlPart);
|
||||
message.setContent(multipart);
|
||||
Message message = messageBuild(mailAddress, title, body, html, parameter, files, cc, bcc);
|
||||
Transport.send(message);
|
||||
} catch (MessagingException e) {
|
||||
throw new MailException(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.setRecipients(Message.RecipientType.TO, mailBuild.eliminate(mailAddress));
|
||||
message.setSubject(title);
|
||||
|
||||
Multipart multipart = new MimeMultipart("alternative");
|
||||
// 读取模板并进行变量替换
|
||||
List<String> strings = HtmlUtil.replacePlaceholder(html, parameter);
|
||||
// 拼合HTML数据
|
||||
String htmlData = HtmlUtil.pieceHtml(strings);
|
||||
if (!body.isEmpty()) {
|
||||
// 创建文本正文部分
|
||||
MimeBodyPart textPart = new MimeBodyPart();
|
||||
textPart.setText(body);
|
||||
multipart.addBodyPart(textPart);
|
||||
}
|
||||
// 添加附件
|
||||
if (files != null && files.size() != 0) {
|
||||
forFiles(multipart, files);
|
||||
}
|
||||
|
||||
MimeBodyPart htmlPart = new MimeBodyPart();
|
||||
htmlPart.setContent(htmlData, "text/html;charset=UTF-8");
|
||||
addCC(cc, bcc, message);
|
||||
multipart.addBodyPart(htmlPart);
|
||||
message.setContent(multipart);
|
||||
return message;
|
||||
}
|
||||
|
||||
private void addCC(List<String> cc, List<String> bcc, Message message) throws MessagingException {
|
||||
if (cc != null && cc.size() > 0) {
|
||||
message.addRecipients(Message.RecipientType.CC, mailBuild.eliminate(cc));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user