Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,7 @@ public SenderEmail getSenderEmail() {
@Override
public ResponseResult checkEmail(SenderEmail senderEmail) {
ResponseResult result = new ResponseResult();
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
if (senderEmail.isSsl()) {
props.put("mail.smtp.starttls.enable", "true");
}
props.put("mail.smtp.host", senderEmail.getHost());
props.put("mail.smtp.port", senderEmail.getPort());

Properties props = buildEmailProperties(senderEmail);
Session session = Session.getInstance(props);
try {
Transport transport = session.getTransport("smtp");
Expand All @@ -247,6 +240,17 @@ public ResponseResult checkEmail(SenderEmail senderEmail) {
return result;
}

static Properties buildEmailProperties(SenderEmail senderEmail) {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", senderEmail.getHost());
props.put("mail.smtp.port", senderEmail.getPort().toString());
if (senderEmail.isSsl()) {
props.put("mail.smtp.ssl.enable", "true");
}
return props;
}

@Override
public boolean updateEmail(SenderEmail senderEmail) {
List<Setting> settings = SenderEmail.toSettings(senderEmail);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.streampark.console.core.service.impl;

import org.apache.streampark.console.core.bean.SenderEmail;

import org.junit.jupiter.api.Test;

import java.util.Properties;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

class SettingServiceImplTest {

@Test
void buildEmailPropertiesUsesSslOnConnectWhenSslEnabled() {
SenderEmail senderEmail = senderEmail(true);

Properties properties = SettingServiceImpl.buildEmailProperties(senderEmail);

assertEquals("true", properties.getProperty("mail.smtp.auth"));
assertEquals("smtp.126.com", properties.getProperty("mail.smtp.host"));
assertEquals("994", properties.getProperty("mail.smtp.port"));
assertEquals("true", properties.getProperty("mail.smtp.ssl.enable"));
assertNull(properties.getProperty("mail.smtp.starttls.enable"));
}

@Test
void buildEmailPropertiesKeepsPlainSmtpWhenSslDisabled() {
SenderEmail senderEmail = senderEmail(false);

Properties properties = SettingServiceImpl.buildEmailProperties(senderEmail);

assertEquals("true", properties.getProperty("mail.smtp.auth"));
assertEquals("smtp.126.com", properties.getProperty("mail.smtp.host"));
assertEquals("994", properties.getProperty("mail.smtp.port"));
assertNull(properties.getProperty("mail.smtp.ssl.enable"));
assertNull(properties.getProperty("mail.smtp.starttls.enable"));
}

private static SenderEmail senderEmail(boolean ssl) {
SenderEmail senderEmail = new SenderEmail();
senderEmail.setHost("smtp.126.com");
senderEmail.setPort(994);
senderEmail.setSsl(ssl);
return senderEmail;
}
}
Loading