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
|
@Builder.Default
|
||||||
private String isAuth = "true";
|
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 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 {
|
||||||
|
|||||||
@ -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,14 +362,69 @@ 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);
|
||||||
|
|
||||||
Multipart multipart = new MimeMultipart("alternative");
|
Multipart multipart = new MimeMultipart("alternative");
|
||||||
//读取模板并进行变量替换
|
// 读取模板并进行变量替换
|
||||||
List<String> strings = HtmlUtil.replacePlaceholder(html, parameter);
|
List<String> strings = HtmlUtil.replacePlaceholder(html, parameter);
|
||||||
//拼合HTML数据
|
// 拼合HTML数据
|
||||||
String htmlData = HtmlUtil.pieceHtml(strings);
|
String htmlData = HtmlUtil.pieceHtml(strings);
|
||||||
if (!body.isEmpty()) {
|
if (!body.isEmpty()) {
|
||||||
// 创建文本正文部分
|
// 创建文本正文部分
|
||||||
@ -376,19 +432,17 @@ public class MailService implements MailClient {
|
|||||||
textPart.setText(body);
|
textPart.setText(body);
|
||||||
multipart.addBodyPart(textPart);
|
multipart.addBodyPart(textPart);
|
||||||
}
|
}
|
||||||
//添加附件
|
// 添加附件
|
||||||
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 {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user