From 374045ebbeaa725ec2d030f5474067e37f568637 Mon Sep 17 00:00:00 2001
From: wayne <wayne8692wayne8692@gmail.com>
Date: 星期四, 23 十二月 2021 10:18:12 +0800
Subject: [PATCH] [add][todo 132507] 新增發送email service

---
 pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java |   81 ++++++++++++++++++++++++++++++++--------
 1 files changed, 65 insertions(+), 16 deletions(-)

diff --git a/pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java b/pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java
index 5280732..6bac287 100644
--- a/pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java
+++ b/pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java
@@ -1,46 +1,44 @@
 package com.pollex.pam.service;
 
-import com.fasterxml.jackson.core.JsonProcessingException;
 import com.pollex.pam.config.ApplicationProperties;
-import com.pollex.pam.service.dto.SMSDetail;
-import com.pollex.pam.service.dto.SendSMSRequest;
-import com.pollex.pam.service.dto.SendSMSResponse;
+import com.pollex.pam.config.ApplicationProperties.Email;
+import com.pollex.pam.config.ApplicationProperties.SMS;
+import com.pollex.pam.service.dto.*;
 import com.pollex.pam.service.util.HttpRequestUtil;
+import com.pollex.pam.web.rest.errors.SendEmailFailException;
+import com.pollex.pam.web.rest.errors.SendSMSFailException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
 
 import java.nio.charset.StandardCharsets;
-import java.security.KeyManagementException;
-import java.security.KeyStoreException;
-import java.security.NoSuchAlgorithmException;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
 import java.util.Base64;
 import java.util.Base64.Encoder;
 import java.util.Collections;
+import java.util.List;
 import java.util.UUID;
 
 @Service
 public class SendMsgService {
 
-    private final static Logger log = LoggerFactory.getLogger(SendMsgService.class);
+    private static final Logger log = LoggerFactory.getLogger(SendMsgService.class);
     private final Encoder encoder = Base64.getEncoder();
 
     @Autowired
     ApplicationProperties applicationProperties;
 
-    public SendSMSResponse sendMsgBySMS(String subject, String toMobile, String content) throws Exception{
-        final String SMS_URL = applicationProperties.getSms().getUrl();
+    public void sendMsgBySMS(String subject, String toMobile, String content) throws SendSMSFailException{
+        SMS smsProperties = applicationProperties.getSms();
 
         SendSMSRequest sendSMSRequest = new SendSMSRequest();
         sendSMSRequest.setpKey(UUID.randomUUID().toString());
-        sendSMSRequest.setSourceCode(applicationProperties.getSms().getSourceCode());
-        sendSMSRequest.setSender(applicationProperties.getSms().getSender());
-        sendSMSRequest.setMsgTypeSet(applicationProperties.getSms().getSmsType());
+        sendSMSRequest.setSourceCode(smsProperties.getSourceCode());
+        sendSMSRequest.setSender(smsProperties.getSender());
+        sendSMSRequest.setMsgTypeSet(smsProperties.getSmsType());
         sendSMSRequest.setSendTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:00")));
         sendSMSRequest.setSubject(subject);
         sendSMSRequest.setActivityId("");
@@ -51,9 +49,60 @@
 
         sendSMSRequest.setDetail(Collections.singletonList(smsDetail));
 
-        ResponseEntity<SendSMSResponse> responseEntity = HttpRequestUtil.postWithJson(SMS_URL, sendSMSRequest, SendSMSResponse.class);
-        return responseEntity.getBody();
+        try {
+            ResponseEntity<SendSMSResponse> responseEntity = HttpRequestUtil.postWithJson(smsProperties.getUrl(), sendSMSRequest, SendSMSResponse.class);
+
+            log.debug("response status code = {}", responseEntity.getStatusCode());
+            log.debug("smsResponse = {}", responseEntity.getBody());
+
+            // todo ����閬�����隤方������葫��
+        }
+        catch (Exception e) {
+            log.debug("send sms failed!", e);
+            throw new SendSMSFailException();
+        }
     }
 
+    public void sendMsgByEmail(String from, String to, String subject, String content, boolean htmlFormat) throws SendEmailFailException{
+        sendMsgByEmail(from, to, subject, content, htmlFormat, Collections.emptyList(), Collections.emptyList());
+    }
 
+    public void sendMsgByEmail(
+        String fromAddress, String toAddress, String subject, String content, boolean htmlFormat, List<String> toCCAddress,
+        List<String> attachments) throws SendEmailFailException
+    {
+        SendMailRequest sendMailRequest = new SendMailRequest();
+        sendMailRequest.setSendMailAddresses(Collections.singletonList(toAddress));
+        sendMailRequest.setFrom(fromAddress);
+        sendMailRequest.setContent(content);
+        sendMailRequest.setSubject(subject);
+        sendMailRequest.setSendCCMailAddresses(toCCAddress);
+        sendMailRequest.setAttachments(attachments);
+        sendMailRequest.setHtmlFormat(htmlFormat);
+        sendMailRequest.setFunctionId(applicationProperties.getEmail().getFunctionId());
+
+        sendMsgByEmail(sendMailRequest);
+    }
+
+    public void sendMsgByEmail(SendMailRequest sendMailRequest) throws SendEmailFailException{
+        try {
+            ResponseEntity<SendMailResponse> responseEntity =
+                HttpRequestUtil.postWithJson( applicationProperties.getEmail().getUrl(), sendMailRequest, SendMailResponse.class);
+
+            SendMailResponse sendMailResponse = responseEntity.getBody();
+            log.debug("response status code = {}", responseEntity.getStatusCode());
+            log.debug("emailResponse = {}", responseEntity.getBody());
+
+            if(sendMailResponse == null || sendMailResponse.getData() == null || "ADDED".equalsIgnoreCase(sendMailResponse.getData().getMessageStatus())) {
+                throw new SendEmailFailException();
+            }
+        }
+        catch (SendEmailFailException e) {
+            throw e;
+        }
+        catch (Exception e) {
+            log.debug("send email failed!", e);
+            throw new SendEmailFailException();
+        }
+    }
 }

--
Gitblit v1.8.0