|
@@ -13,6 +13,8 @@ import com.storlead.sales.mail.common.entity.SmtpPopSettingsEntity;
|
|
|
import com.storlead.sales.mail.common.enums.MailSceneEnum;
|
|
import com.storlead.sales.mail.common.enums.MailSceneEnum;
|
|
|
import com.storlead.sales.mail.common.enums.MailSendStatus;
|
|
import com.storlead.sales.mail.common.enums.MailSendStatus;
|
|
|
import com.storlead.sales.mail.common.properties.MailFileProperties;
|
|
import com.storlead.sales.mail.common.properties.MailFileProperties;
|
|
|
|
|
+import com.storlead.sales.mail.common.pojo.EmailAddressValidateResult;
|
|
|
|
|
+import com.storlead.sales.mail.common.service.EmailAddressValidateService;
|
|
|
import com.storlead.sales.mail.common.service.MailAccountBizService;
|
|
import com.storlead.sales.mail.common.service.MailAccountBizService;
|
|
|
import com.storlead.sales.mail.common.service.MailTempAttachmentService;
|
|
import com.storlead.sales.mail.common.service.MailTempAttachmentService;
|
|
|
import com.storlead.sales.mail.common.transport.MailTransportSupport;
|
|
import com.storlead.sales.mail.common.transport.MailTransportSupport;
|
|
@@ -28,11 +30,14 @@ import com.storlead.sales.mail.mkt.pojo.MktMailQueryDTO;
|
|
|
import com.storlead.sales.mail.mkt.pojo.MktOpenStatsVO;
|
|
import com.storlead.sales.mail.mkt.pojo.MktOpenStatsVO;
|
|
|
import com.storlead.sales.mail.mkt.pojo.MktSendMailDTO;
|
|
import com.storlead.sales.mail.mkt.pojo.MktSendMailDTO;
|
|
|
import com.storlead.sales.mail.mkt.pojo.MktSendMailResult;
|
|
import com.storlead.sales.mail.mkt.pojo.MktSendMailResult;
|
|
|
|
|
+import com.storlead.sales.mail.mkt.config.MktMailSendProperties;
|
|
|
import com.storlead.sales.mail.mkt.service.MktMailAttachmentService;
|
|
import com.storlead.sales.mail.mkt.service.MktMailAttachmentService;
|
|
|
import com.storlead.sales.mail.mkt.service.MktMailBlacklistService;
|
|
import com.storlead.sales.mail.mkt.service.MktMailBlacklistService;
|
|
|
import com.storlead.sales.mail.mkt.service.MktMailMessageService;
|
|
import com.storlead.sales.mail.mkt.service.MktMailMessageService;
|
|
|
import com.storlead.sales.mail.mkt.service.MktMailReplyRecordService;
|
|
import com.storlead.sales.mail.mkt.service.MktMailReplyRecordService;
|
|
|
import com.storlead.sales.mail.mkt.service.MktMailStatusService;
|
|
import com.storlead.sales.mail.mkt.service.MktMailStatusService;
|
|
|
|
|
+import com.storlead.sales.mail.mkt.service.MktSendAccountSelector;
|
|
|
|
|
+import com.storlead.sales.mail.mkt.service.MktSendQuotaService;
|
|
|
import com.storlead.sales.mail.mkt.support.MktMailHeaders;
|
|
import com.storlead.sales.mail.mkt.support.MktMailHeaders;
|
|
|
import com.storlead.sales.mail.mkt.util.MktMailSender;
|
|
import com.storlead.sales.mail.mkt.util.MktMailSender;
|
|
|
import lombok.extern.log4j.Log4j2;
|
|
import lombok.extern.log4j.Log4j2;
|
|
@@ -63,8 +68,10 @@ import java.util.ArrayList;
|
|
|
import java.util.Collection;
|
|
import java.util.Collection;
|
|
|
import java.util.Collections;
|
|
import java.util.Collections;
|
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
|
|
|
+import java.util.HashSet;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
import java.util.Objects;
|
|
import java.util.Objects;
|
|
|
|
|
+import java.util.Set;
|
|
|
import java.util.UUID;
|
|
import java.util.UUID;
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
@@ -106,6 +113,18 @@ public class MktMailMessageServiceImpl extends MyBaseServiceImpl<MktMailMessageM
|
|
|
@Resource
|
|
@Resource
|
|
|
private MktMailBlacklistService mktMailBlacklistService;
|
|
private MktMailBlacklistService mktMailBlacklistService;
|
|
|
|
|
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private EmailAddressValidateService emailAddressValidateService;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private MktSendQuotaService mktSendQuotaService;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private MktSendAccountSelector mktSendAccountSelector;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private MktMailSendProperties mktMailSendProperties;
|
|
|
|
|
+
|
|
|
@Resource
|
|
@Resource
|
|
|
private MailFileProperties mailFileProperties;
|
|
private MailFileProperties mailFileProperties;
|
|
|
|
|
|
|
@@ -171,21 +190,37 @@ public class MktMailMessageServiceImpl extends MyBaseServiceImpl<MktMailMessageM
|
|
|
if (!StringUtils.hasText(dto.getSubject())) {
|
|
if (!StringUtils.hasText(dto.getSubject())) {
|
|
|
throw new WebException("主题不能为空");
|
|
throw new WebException("主题不能为空");
|
|
|
}
|
|
}
|
|
|
|
|
+// // 发信前校验主收件人:格式 + 域名收信能力,降低退信
|
|
|
|
|
+// EmailAddressValidateResult recipientCheck = emailAddressValidateService.validate(dto.getRecipient());
|
|
|
|
|
+// if (!recipientCheck.isValid()) {
|
|
|
|
|
+// throw new WebException(StringUtils.hasText(recipientCheck.getReason())
|
|
|
|
|
+// ? recipientCheck.getReason() : "收件邮箱无效");
|
|
|
|
|
+// }
|
|
|
|
|
+ dto.setRecipient(dto.getRecipient());
|
|
|
|
|
+ // 抄送/密送去掉明显无效地址
|
|
|
|
|
+ if (!CollectionUtils.isEmpty(dto.getRecipientCcList())) {
|
|
|
|
|
+ dto.setRecipientCcList(retainValidEmails(dto.getRecipientCcList()));
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!CollectionUtils.isEmpty(dto.getRecipientBccList())) {
|
|
|
|
|
+ dto.setRecipientBccList(retainValidEmails(dto.getRecipientBccList()));
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- SmtpPopSettingsEntity account = mailAccountBizService.requireAccount(dto.getAccountId(), MailSceneEnum.MKT);
|
|
|
|
|
|
|
+ // preferred:调用方指定的首选发信号;实际发出账号可能因轮询切换而不同
|
|
|
|
|
+ SmtpPopSettingsEntity preferred = mailAccountBizService.requireAccount(dto.getAccountId(), MailSceneEnum.MKT);
|
|
|
|
|
|
|
|
MktMailMessageEntity entity = resolveWritableEntity(dto.getId(), operatorId);
|
|
MktMailMessageEntity entity = resolveWritableEntity(dto.getId(), operatorId);
|
|
|
- fillMessageFields(entity, dto, account, operatorId);
|
|
|
|
|
- ensureTrackToken(entity, dto.getIsTrack());
|
|
|
|
|
- assertTrackReady(entity);
|
|
|
|
|
- // 主收件人在黑名单则整封不发;抄送/密送里的黑名单地址剔除后再发
|
|
|
|
|
- applyRecipientBlacklist(entity, false);
|
|
|
|
|
-
|
|
|
|
|
|
|
+ /*
|
|
|
|
|
+ * 延时发送:产品约定不计入池级日限额、不参与账号轮询;
|
|
|
|
|
+ * 仅落库 status=DELAY,到期任务另议(本需求不改造延时链路)。
|
|
|
|
|
+ */
|
|
|
boolean delayed = dto.getDelaySendTime() != null && dto.getDelaySendTime().after(new Date());
|
|
boolean delayed = dto.getDelaySendTime() != null && dto.getDelaySendTime().after(new Date());
|
|
|
- entity.setFolder(MktMailFolderEnum.SENTING.code);
|
|
|
|
|
- entity.setInOutMark(2);
|
|
|
|
|
if (delayed) {
|
|
if (delayed) {
|
|
|
- // 延时尚未投递,正文需先落库供到期任务使用
|
|
|
|
|
|
|
+ fillMessageFields(entity, dto, preferred, operatorId);
|
|
|
|
|
+ ensureTrackToken(entity, dto.getIsTrack());
|
|
|
|
|
+ assertTrackReady(entity);
|
|
|
|
|
+ applyRecipientBlacklist(entity, false);
|
|
|
|
|
+ entity.setFolder(MktMailFolderEnum.SENTING.code);
|
|
|
|
|
+ entity.setInOutMark(2);
|
|
|
entity.setStatus(STATUS_DELAY);
|
|
entity.setStatus(STATUS_DELAY);
|
|
|
entity.setDelaySendTime(dto.getDelaySendTime());
|
|
entity.setDelaySendTime(dto.getDelaySendTime());
|
|
|
this.saveOrUpdate(entity);
|
|
this.saveOrUpdate(entity);
|
|
@@ -195,24 +230,85 @@ public class MktMailMessageServiceImpl extends MyBaseServiceImpl<MktMailMessageM
|
|
|
return toSendResult(null, entity);
|
|
return toSendResult(null, entity);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- entity.setStatus(STATUS_SENDING);
|
|
|
|
|
- entity.setDelaySendTime(null);
|
|
|
|
|
- this.saveOrUpdate(entity);
|
|
|
|
|
- mktMailStatusService.ensureStatus(entity);
|
|
|
|
|
- bindTempAttachments(entity.getId(), dto.getFileIds(), operatorId);
|
|
|
|
|
- copyFormalToTemp(entity.getId(), dto.getOldFileIds(), operatorId);
|
|
|
|
|
-
|
|
|
|
|
- SmtpPopSettingsEntity decrypted = decryptAccount(account);
|
|
|
|
|
|
|
+ /*
|
|
|
|
|
+ * 即时发送主流程(全站日限额 Redis + 账号轮询):
|
|
|
|
|
+ * 1) tryReserve:Redis 剩余额度 -1,为 0 则今日额度用完
|
|
|
|
|
+ * 2) selectNext 选健康号 → 落库 SENTING → bindReservation(mailId)
|
|
|
|
|
+ * 3) SMTP 成功 confirm(同步 DB remaining)/ 失败 rollback(Redis+1)
|
|
|
|
|
+ * 4) 同步可换号重试;异步只发一枪
|
|
|
|
|
+ */
|
|
|
|
|
+ Set<Long> excluded = new HashSet<>();
|
|
|
|
|
+ MailSendStatus lastStatus = MailSendStatus.FAILED;
|
|
|
|
|
+ int maxAttempts = 1 + Math.max(0, mktMailSendProperties.getSendAccountRetry());
|
|
|
boolean async = Boolean.TRUE.equals(dto.getAsync());
|
|
boolean async = Boolean.TRUE.equals(dto.getAsync());
|
|
|
- MailSendStatus status = dispatchSmtpSend(entity.getId(), decrypted, async, entity.getContent());
|
|
|
|
|
|
|
+
|
|
|
|
|
+ for (int attempt = 0; attempt < maxAttempts; attempt++) {
|
|
|
|
|
+ // 每轮单独预占:上一轮失败时 Sender 已 rollback,需重新占额再试
|
|
|
|
|
+ if (!mktSendQuotaService.tryReserve()) {
|
|
|
|
|
+ throw new WebException("营销邮件今日发送额度已满");
|
|
|
|
|
+ }
|
|
|
|
|
+ // reserved=true:已 DECR 但尚未 bind,异常时需 rollbackByMailId 无法命中,用临时标记回补
|
|
|
|
|
+ boolean reserved = true;
|
|
|
|
|
+ Long reservedMailId = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ SmtpPopSettingsEntity account = mktSendAccountSelector.selectNext(preferred, excluded);
|
|
|
|
|
+ if (account == null) {
|
|
|
|
|
+ throw new WebException("暂无可用的营销发信邮箱");
|
|
|
|
|
+ }
|
|
|
|
|
+ fillMessageFields(entity, dto, account, operatorId);
|
|
|
|
|
+ ensureTrackToken(entity, dto.getIsTrack());
|
|
|
|
|
+ assertTrackReady(entity);
|
|
|
|
|
+ applyRecipientBlacklist(entity, false);
|
|
|
|
|
+
|
|
|
|
|
+ entity.setFolder(MktMailFolderEnum.SENTING.code);
|
|
|
|
|
+ entity.setInOutMark(2);
|
|
|
|
|
+ entity.setStatus(STATUS_SENDING);
|
|
|
|
|
+ entity.setDelaySendTime(null);
|
|
|
|
|
+ entity.setFailReason(null);
|
|
|
|
|
+ this.saveOrUpdate(entity);
|
|
|
|
|
+ mktMailStatusService.ensureStatus(entity);
|
|
|
|
|
+ if (attempt == 0) {
|
|
|
|
|
+ bindTempAttachments(entity.getId(), dto.getFileIds(), operatorId);
|
|
|
|
|
+ copyFormalToTemp(entity.getId(), dto.getOldFileIds(), operatorId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ mktSendQuotaService.bindReservation(entity.getId());
|
|
|
|
|
+ reservedMailId = entity.getId();
|
|
|
|
|
+ reserved = false;
|
|
|
|
|
+
|
|
|
|
|
+ SmtpPopSettingsEntity decrypted = decryptAccount(account);
|
|
|
|
|
+ if (async) {
|
|
|
|
|
+ dispatchSmtpSend(entity.getId(), decrypted, true, entity.getContent());
|
|
|
|
|
+ MktMailMessageEntity latestAsync = this.getById(entity.getId());
|
|
|
|
|
+ return toSendResult(null, latestAsync == null ? entity : latestAsync);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ lastStatus = dispatchSmtpSend(entity.getId(), decrypted, false, entity.getContent());
|
|
|
|
|
+ if (lastStatus == MailSendStatus.SUCCESS || lastStatus == MailSendStatus.PARTIAL_SUCCESS) {
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ excluded.add(account.getId());
|
|
|
|
|
+ log.warn("mkt send attempt fail, mailId={}, accountId={}, status={}, tryNext={}",
|
|
|
|
|
+ entity.getId(), account.getId(), lastStatus, attempt + 1 < maxAttempts);
|
|
|
|
|
+ } catch (RuntimeException ex) {
|
|
|
|
|
+ if (reserved) {
|
|
|
|
|
+ // 已 DECR 未 bind:直接 Redis +1
|
|
|
|
|
+ mktSendQuotaService.rollbackUnbound();
|
|
|
|
|
+ } else if (reservedMailId != null) {
|
|
|
|
|
+ mktSendQuotaService.rollbackByMailId(reservedMailId);
|
|
|
|
|
+ }
|
|
|
|
|
+ throw ex;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (reserved) {
|
|
|
|
|
+ mktSendQuotaService.rollbackUnbound();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
MktMailMessageEntity latest = this.getById(entity.getId());
|
|
MktMailMessageEntity latest = this.getById(entity.getId());
|
|
|
if (latest == null) {
|
|
if (latest == null) {
|
|
|
latest = entity;
|
|
latest = entity;
|
|
|
}
|
|
}
|
|
|
- if (async) {
|
|
|
|
|
- return toSendResult(null, latest);
|
|
|
|
|
- }
|
|
|
|
|
- return toSendResult(status, latest);
|
|
|
|
|
|
|
+ return toSendResult(lastStatus, latest);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -226,6 +322,9 @@ public class MktMailMessageServiceImpl extends MyBaseServiceImpl<MktMailMessageM
|
|
|
mktMailBlacklistService.assertRecipientsNotBlacklisted(to);
|
|
mktMailBlacklistService.assertRecipientsNotBlacklisted(to);
|
|
|
String cc = emptyIfNull(mktMailBlacklistService.retainDeliverable(entity.getRecipientCc()));
|
|
String cc = emptyIfNull(mktMailBlacklistService.retainDeliverable(entity.getRecipientCc()));
|
|
|
String bcc = emptyIfNull(mktMailBlacklistService.retainDeliverable(entity.getRecipientBcc()));
|
|
String bcc = emptyIfNull(mktMailBlacklistService.retainDeliverable(entity.getRecipientBcc()));
|
|
|
|
|
+ // 再过滤格式/域名无效地址,进一步降低退信
|
|
|
|
|
+ cc = emptyIfNull(emailAddressValidateService.retainDeliverable(cc));
|
|
|
|
|
+ bcc = emptyIfNull(emailAddressValidateService.retainDeliverable(bcc));
|
|
|
String oldCc = emptyIfNull(entity.getRecipientCc());
|
|
String oldCc = emptyIfNull(entity.getRecipientCc());
|
|
|
String oldBcc = emptyIfNull(entity.getRecipientBcc());
|
|
String oldBcc = emptyIfNull(entity.getRecipientBcc());
|
|
|
entity.setRecipientCc(StringUtils.hasText(cc) ? cc : "");
|
|
entity.setRecipientCc(StringUtils.hasText(cc) ? cc : "");
|
|
@@ -240,6 +339,26 @@ public class MktMailMessageServiceImpl extends MyBaseServiceImpl<MktMailMessageM
|
|
|
this.update(update);
|
|
this.update(update);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private List<String> retainValidEmails(List<String> emails) {
|
|
|
|
|
+ if (CollectionUtils.isEmpty(emails)) {
|
|
|
|
|
+ return emails;
|
|
|
|
|
+ }
|
|
|
|
|
+ List<String> kept = new ArrayList<>();
|
|
|
|
|
+ for (String email : emails) {
|
|
|
|
|
+ if (!StringUtils.hasText(email)) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ emailAddressValidateService.assertDeliverable(email.trim());
|
|
|
|
|
+ kept.add(email.trim());
|
|
|
|
|
+ } catch (WebException ex) {
|
|
|
|
|
+ log.info("mkt drop invalid cc/bcc before send, email={}, reason={}",
|
|
|
|
|
+ email, ex.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return kept;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private static String emptyIfNull(String value) {
|
|
private static String emptyIfNull(String value) {
|
|
|
return value == null ? "" : value;
|
|
return value == null ? "" : value;
|
|
|
}
|
|
}
|
|
@@ -650,6 +769,9 @@ public class MktMailMessageServiceImpl extends MyBaseServiceImpl<MktMailMessageM
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * SMTP 准备阶段失败(建连前异常等):落失败态并释放已预占池额度。
|
|
|
|
|
+ */
|
|
|
private void markSendingFailed(Long mailId, String reason) {
|
|
private void markSendingFailed(Long mailId, String reason) {
|
|
|
LambdaUpdateWrapper<MktMailMessageEntity> fail = new LambdaUpdateWrapper<>();
|
|
LambdaUpdateWrapper<MktMailMessageEntity> fail = new LambdaUpdateWrapper<>();
|
|
|
fail.eq(MktMailMessageEntity::getId, mailId);
|
|
fail.eq(MktMailMessageEntity::getId, mailId);
|
|
@@ -658,6 +780,12 @@ public class MktMailMessageServiceImpl extends MyBaseServiceImpl<MktMailMessageM
|
|
|
fail.set(MktMailMessageEntity::getFailReason, reason);
|
|
fail.set(MktMailMessageEntity::getFailReason, reason);
|
|
|
this.update(fail);
|
|
this.update(fail);
|
|
|
mktMailStatusService.markSendStatus(mailId, STATUS_FAIL);
|
|
mktMailStatusService.markSendStatus(mailId, STATUS_FAIL);
|
|
|
|
|
+ // 准备失败不会进 Sender 回调,需在此主动回滚预占
|
|
|
|
|
+ try {
|
|
|
|
|
+ mktSendQuotaService.rollbackByMailId(mailId);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("mkt quota rollback on prepare fail error, mailId={}", mailId, e);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private MimeMessage buildMimeMessage(Session session,
|
|
private MimeMessage buildMimeMessage(Session session,
|