From fab723694ee9ca11a054e65aea7ec8a2f5057fe8 Mon Sep 17 00:00:00 2001
From: wayne <wayne8692wayne8692@gmail.com>
Date: 星期六, 22 一月 2022 10:54:33 +0800
Subject: [PATCH] [update] 測試gmail發送

---
 pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java |   29 ++++++++++++++++++++++++-----
 1 files changed, 24 insertions(+), 5 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 05d8d73..15a72fc 100644
--- a/pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java
+++ b/pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java
@@ -3,6 +3,7 @@
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.pollex.pam.config.ApplicationProperties;
 import com.pollex.pam.config.ApplicationProperties.SMS;
+import com.pollex.pam.config.Constants;
 import com.pollex.pam.repository.ConsultantRepository;
 import com.pollex.pam.service.dto.*;
 import com.pollex.pam.service.util.HttpRequestUtil;
@@ -11,9 +12,12 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.env.Environment;
+import org.springframework.core.env.Profiles;
 import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
 import org.thymeleaf.spring5.SpringTemplateEngine;
+import tech.jhipster.config.JHipsterConstants;
 
 import java.nio.charset.StandardCharsets;
 import java.time.LocalDateTime;
@@ -38,12 +42,18 @@
     @Autowired
     SpringTemplateEngine springTemplateEngine;
 
+    @Autowired
+    Environment environment;
+
+    @Autowired
+    MailService mailService;
+
     public SendSMSResponse sendMsgBySMS(String toMobile, String content) throws SendSMSFailException {
     	if(!applicationProperties.isSendNotifyMsg()) {
 //    		return getMockSMSResponse();
     		return null;
     	}
-    	
+
     	SMS smsProperties = applicationProperties.getSms();
 
         SendSMSRequest sendSMSRequest = new SendSMSRequest();
@@ -94,7 +104,7 @@
     public String sendMsgByEmail(String toAddress, String subject, String content, boolean htmlFormat, List<String> toCCAddress,
         List<String> attachments) throws SendEmailFailException {
     	String fromAddress = applicationProperties.getEmail().getSenderEmail();
-         
+
         SendMailRequest sendMailRequest = new SendMailRequest();
         sendMailRequest.setSendMailAddresses(Collections.singletonList(toAddress));
         sendMailRequest.setFrom(fromAddress);
@@ -109,9 +119,18 @@
     }
 
     public String sendMsgByEmail(SendMailRequest sendMailRequest) throws SendEmailFailException{
-    	if(!applicationProperties.isSendNotifyMsg()) {
-    		return null;
-    	}
+        if(!applicationProperties.isSendNotifyMsg()) {
+            if(environment.acceptsProfiles(Profiles.of(Constants.SPRING_PROFILE_POLLEX_DEVELOPMENT))) {
+                String subject = sendMailRequest.getSubject();
+                String content = sendMailRequest.getContent();
+                boolean isHtml = sendMailRequest.isHtmlFormat();
+
+                sendMailRequest.getSendMailAddresses().forEach(receiver -> mailService.sendEmail(receiver, subject, content, false, isHtml));
+            }
+            
+            return null;
+        }
+
     	try {
             ResponseEntity<String> responseEntity =
                 HttpRequestUtil.postWithJson( applicationProperties.getEmail().getUrl(), sendMailRequest, String.class);

--
Gitblit v1.8.0