This is an automated email from the ASF dual-hosted git repository.

caishunfeng pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/dolphinscheduler.git


The following commit(s) were added to refs/heads/dev by this push:
     new 69e744961b [Fix][Alert] Fix when auth is false and user || pwd null 
can't send email excection (#13761)
69e744961b is described below

commit 69e744961b489c9553e656b8d1f6247b46884e05
Author: 旺阳 <[email protected]>
AuthorDate: Tue Mar 21 13:59:01 2023 +0800

    [Fix][Alert] Fix when auth is false and user || pwd null can't send email 
excection (#13761)
    
    * fix send email error
    
    * fix spotless
    
    * fix-sonar
---
 .../plugin/alert/email/MailSender.java             | 11 +++++---
 .../plugin/alert/email/MailUtilsTest.java          | 30 ++++++++++++++++++++++
 2 files changed, 37 insertions(+), 4 deletions(-)

diff --git 
a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-email/src/main/java/org/apache/dolphinscheduler/plugin/alert/email/MailSender.java
 
b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-email/src/main/java/org/apache/dolphinscheduler/plugin/alert/email/MailSender.java
index 218b1cf16f..f323219dab 100644
--- 
a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-email/src/main/java/org/apache/dolphinscheduler/plugin/alert/email/MailSender.java
+++ 
b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-email/src/main/java/org/apache/dolphinscheduler/plugin/alert/email/MailSender.java
@@ -22,6 +22,7 @@ import static java.util.Objects.requireNonNull;
 import org.apache.dolphinscheduler.alert.api.AlertConstants;
 import org.apache.dolphinscheduler.alert.api.AlertResult;
 import org.apache.dolphinscheduler.alert.api.ShowType;
+import org.apache.dolphinscheduler.common.constants.Constants;
 import 
org.apache.dolphinscheduler.plugin.alert.email.exception.AlertEmailException;
 import org.apache.dolphinscheduler.plugin.alert.email.template.AlertTemplate;
 import 
org.apache.dolphinscheduler.plugin.alert.email.template.DefaultHTMLTemplate;
@@ -103,12 +104,14 @@ public final class MailSender {
         requireNonNull(mailSenderEmail, MailParamsConstants.NAME_MAIL_SENDER + 
mustNotNull);
 
         enableSmtpAuth = config.get(MailParamsConstants.NAME_MAIL_SMTP_AUTH);
-
         mailUser = config.get(MailParamsConstants.NAME_MAIL_USER);
-        requireNonNull(mailUser, MailParamsConstants.NAME_MAIL_USER + 
mustNotNull);
-
         mailPasswd = config.get(MailParamsConstants.NAME_MAIL_PASSWD);
-        requireNonNull(mailPasswd, MailParamsConstants.NAME_MAIL_PASSWD + 
mustNotNull);
+
+        // Needs to check user && password only if enableSmtpAuth is true
+        if (Constants.STRING_TRUE.equals(enableSmtpAuth)) {
+            requireNonNull(mailUser, MailParamsConstants.NAME_MAIL_USER + 
mustNotNull);
+            requireNonNull(mailPasswd, MailParamsConstants.NAME_MAIL_PASSWD + 
mustNotNull);
+        }
 
         mailUseStartTLS = 
config.get(MailParamsConstants.NAME_MAIL_SMTP_STARTTLS_ENABLE);
         requireNonNull(mailUseStartTLS, 
MailParamsConstants.NAME_MAIL_SMTP_STARTTLS_ENABLE + mustNotNull);
diff --git 
a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-email/src/test/java/org/apache/dolphinscheduler/plugin/alert/email/MailUtilsTest.java
 
b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-email/src/test/java/org/apache/dolphinscheduler/plugin/alert/email/MailUtilsTest.java
index bc6e63d8af..f562999be0 100644
--- 
a/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-email/src/test/java/org/apache/dolphinscheduler/plugin/alert/email/MailUtilsTest.java
+++ 
b/dolphinscheduler-alert/dolphinscheduler-alert-plugins/dolphinscheduler-alert-email/src/test/java/org/apache/dolphinscheduler/plugin/alert/email/MailUtilsTest.java
@@ -29,6 +29,7 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 
+import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.Disabled;
 import org.junit.jupiter.api.Test;
@@ -81,6 +82,35 @@ public class MailUtilsTest {
                 content);
     }
 
+    @Test
+    void testAuthCheck() {
+        String title = "Auth Exception";
+        String content = list2String();
+
+        // test auth false and user && pwd null will pass
+        emailConfig.put(MailParamsConstants.NAME_MAIL_SMTP_AUTH, "false");
+        emailConfig.put(MailParamsConstants.NAME_MAIL_USER, null);
+        emailConfig.put(MailParamsConstants.NAME_MAIL_PASSWD, null);
+        mailSender = new MailSender(emailConfig);
+        mailSender.sendMails(title, content);
+
+        try {
+            // test auth true and user null will throw exception
+            emailConfig.put(MailParamsConstants.NAME_MAIL_SMTP_AUTH, "true");
+            emailConfig.put(MailParamsConstants.NAME_MAIL_USER, null);
+            mailSender = new MailSender(emailConfig);
+            mailSender.sendMails(title, content);
+        } catch (Exception e) {
+            
Assertions.assertTrue(e.getMessage().contains(MailParamsConstants.NAME_MAIL_USER));
+        }
+
+        // test auth true and user && pwd not null will pass
+        emailConfig.put(MailParamsConstants.NAME_MAIL_USER, "user");
+        emailConfig.put(MailParamsConstants.NAME_MAIL_PASSWD, "passwd");
+        mailSender = new MailSender(emailConfig);
+        mailSender.sendMails(title, content);
+    }
+
     public String list2String() {
 
         LinkedHashMap<String, Object> map1 = new LinkedHashMap<>();

Reply via email to