引入阿里云邮件推送
This commit is contained in:
@@ -2,10 +2,8 @@ package com.njcn.msgpush.module.push.client.sender.impl.email;
|
|||||||
|
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
|
||||||
import com.aliyun.dm20151123.Client;
|
import com.aliyun.dm20151123.Client;
|
||||||
import com.aliyun.dm20151123.models.SingleSendMailRequest;
|
import com.aliyun.dm20151123.models.*;
|
||||||
import com.aliyun.dm20151123.models.SingleSendMailResponse;
|
|
||||||
import com.aliyun.teaopenapi.models.Config;
|
import com.aliyun.teaopenapi.models.Config;
|
||||||
import com.aliyun.teautil.models.RuntimeOptions;
|
import com.aliyun.teautil.models.RuntimeOptions;
|
||||||
import com.njcn.msgpush.module.push.client.sender.EmailSender;
|
import com.njcn.msgpush.module.push.client.sender.EmailSender;
|
||||||
@@ -18,6 +16,8 @@ import org.springframework.http.HttpStatus;
|
|||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
@@ -25,9 +25,10 @@ import java.util.concurrent.TimeUnit;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class AliyunEmailSender implements EmailSender {
|
public class AliyunEmailSender implements EmailSender {
|
||||||
|
|
||||||
private static final String ACCOUNT_NAME = "accountName";
|
private static final String ACCOUNT_NAME = "njcn@shining-electric.cn";
|
||||||
private static final String REPLY_TO_ADDRESS = "replyToAddress";
|
private static final Integer ADDRESS_TYPE = 1;
|
||||||
private static final String FROM_ALIAS = "fromAlias";
|
private static final Boolean REPLY_TO_ADDRESS = false;
|
||||||
|
private static final String FROM_ALIAS = "南京灿能";
|
||||||
|
|
||||||
private final Sender sender;
|
private final Sender sender;
|
||||||
|
|
||||||
@@ -60,17 +61,15 @@ public class AliyunEmailSender implements EmailSender {
|
|||||||
RuntimeOptions runtimeOptions = new RuntimeOptions();
|
RuntimeOptions runtimeOptions = new RuntimeOptions();
|
||||||
runtimeOptions.autoretry = true;
|
runtimeOptions.autoretry = true;
|
||||||
|
|
||||||
// JSONObject jsonObject = JSON.parseObject(message.getExtraInfo());
|
|
||||||
JSONObject jsonObject = null;
|
|
||||||
SingleSendMailRequest request = new SingleSendMailRequest()
|
SingleSendMailRequest request = new SingleSendMailRequest()
|
||||||
.setAccountName(jsonObject.getString(ACCOUNT_NAME))
|
.setAccountName(ACCOUNT_NAME)
|
||||||
.setAddressType(1)
|
.setAddressType(ADDRESS_TYPE)
|
||||||
.setReplyToAddress(jsonObject.getBooleanValue(REPLY_TO_ADDRESS))
|
.setReplyToAddress(REPLY_TO_ADDRESS)
|
||||||
.setToAddress(message.getReceiver())
|
.setToAddress(message.getReceiver())
|
||||||
.setSubject(message.getTitle())
|
.setSubject(message.getTitle())
|
||||||
.setHtmlBody(message.getContent())
|
.setHtmlBody(message.getContent())
|
||||||
.setTextBody("")
|
.setTextBody("")
|
||||||
.setFromAlias(jsonObject.getString(FROM_ALIAS));
|
.setFromAlias(FROM_ALIAS);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
LocalDateTime now = LocalDateTime.now();
|
LocalDateTime now = LocalDateTime.now();
|
||||||
@@ -82,6 +81,8 @@ public class AliyunEmailSender implements EmailSender {
|
|||||||
message.setCostTime(costTime);
|
message.setCostTime(costTime);
|
||||||
|
|
||||||
if (HttpStatus.OK.value() == response.getStatusCode()) {
|
if (HttpStatus.OK.value() == response.getStatusCode()) {
|
||||||
|
// 电信短信同步返回成功同样只代表已受理,最终状态以后续回执为准。
|
||||||
|
this.getDownInfo(message);
|
||||||
// 邮件接口同步返回成功时,当前平台直接认定本次发送成功。
|
// 邮件接口同步返回成功时,当前平台直接认定本次发送成功。
|
||||||
return SendResult.success(now, costTime, null);
|
return SendResult.success(now, costTime, null);
|
||||||
}
|
}
|
||||||
@@ -108,4 +109,52 @@ public class AliyunEmailSender implements EmailSender {
|
|||||||
return this.sender.buildTimeoutResult();
|
return this.sender.buildTimeoutResult();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void getDownInfo(MessageRecordDO message) {
|
||||||
|
// 回执查询延后执行,给第三方落库和状态变更留出时间。
|
||||||
|
this.sender.MSG_CALLBACK_THREAD_POOL_SCHEDULER.schedule(() -> {
|
||||||
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||||
|
SenderStatisticsDetailByParamRequest request = new SenderStatisticsDetailByParamRequest()
|
||||||
|
.setToAddress(message.getReceiver())
|
||||||
|
.setLength(1);
|
||||||
|
try {
|
||||||
|
SenderStatisticsDetailByParamResponse detail = this.emailClient.senderStatisticsDetailByParam(request);
|
||||||
|
System.out.println("@@@=" + JSON.toJSONString(detail));
|
||||||
|
|
||||||
|
if (detail.statusCode == 200) {
|
||||||
|
List<SenderStatisticsDetailByParamResponseBody.SenderStatisticsDetailByParamResponseBodyDataMailDetail> mailDetailList = detail.body.getData().mailDetail;
|
||||||
|
|
||||||
|
if (mailDetailList.size() > 0) {
|
||||||
|
SenderStatisticsDetailByParamResponseBody.SenderStatisticsDetailByParamResponseBodyDataMailDetail mailDetail = mailDetailList.get(0);
|
||||||
|
if (mailDetail.getStatus() == 0) {
|
||||||
|
// 回执确认成功后,复用统一成功落库逻辑。
|
||||||
|
this.sender.applyCallbackResult(message, SendResult.success(message.getSendTime(), message.getCostTime(), message.getThirdPartyId()));
|
||||||
|
} else {
|
||||||
|
SendResult failedResult = this.sender.buildFailureResult(
|
||||||
|
message,
|
||||||
|
mailDetail.getErrorClassification(),
|
||||||
|
mailDetail.getErrorClassification(),
|
||||||
|
"THIRD_PARTY_CALLBACK_FAILED",
|
||||||
|
"邮件回执返回失败",
|
||||||
|
false
|
||||||
|
);
|
||||||
|
this.sender.applyCallbackResult(message, failedResult);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
SendResult failedResult = this.sender.buildFailureResult(
|
||||||
|
message,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
"THIRD_PARTY_CALLBACK_FAILED",
|
||||||
|
"邮件回执返回失败",
|
||||||
|
false
|
||||||
|
);
|
||||||
|
this.sender.applyCallbackResult(message, failedResult);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("电信短信回执查询失败", e);
|
||||||
|
}
|
||||||
|
}, 30, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -214,22 +214,6 @@ public class TelecomSmsSender implements SmsSender {
|
|||||||
// 回执确认成功后,复用统一成功落库逻辑。
|
// 回执确认成功后,复用统一成功落库逻辑。
|
||||||
this.sender.applyCallbackResult(message, SendResult.success(message.getSendTime(), message.getCostTime(), message.getThirdPartyId()));
|
this.sender.applyCallbackResult(message, SendResult.success(message.getSendTime(), message.getCostTime(), message.getThirdPartyId()));
|
||||||
}
|
}
|
||||||
// double random = Math.random();
|
|
||||||
// System.out.println(random + " aaaa");
|
|
||||||
// if (random > 0.5) {
|
|
||||||
// SendResult failedResult = this.sender.buildFailureResult(
|
|
||||||
// message,
|
|
||||||
// detailRes.getStat(),
|
|
||||||
// null,
|
|
||||||
// "THIRD_PARTY_CALLBACK_FAILED",
|
|
||||||
// "短信回执返回失败",
|
|
||||||
// true
|
|
||||||
// );
|
|
||||||
// this.sender.applyCallbackResult(message, failedResult);
|
|
||||||
// } else {
|
|
||||||
// // 回执确认成功后,复用统一成功落库逻辑。
|
|
||||||
// this.sender.applyCallbackResult(message, SendResult.success(message.getSendTime(), message.getCostTime(), message.getThirdPartyId()));
|
|
||||||
// }
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("电信短信回执查询失败", e);
|
log.error("电信短信回执查询失败", e);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user