Skip to content

Commit

Permalink
support send email with SMTPSSLTransport
Browse files Browse the repository at this point in the history
Signed-off-by: victorquan <[email protected]>
  • Loading branch information
dongqiaoershao committed Nov 28, 2023
1 parent 6812a1a commit 8fe3b1c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Apollo 2.2.0
* [Fix the issue that namespace content being cleared when identical content is pasted into the namespace](https://github.com/apolloconfig/apollo/pull/4922)
* [feat(openapi): allow user create app via openapi](https://github.com/apolloconfig/apollo/pull/4954)
* [Support grayscale feature for non-properties namespaces](https://github.com/apolloconfig/apollo/pull/4952)
* [Support send email with SMTPSSLTransport](https://github.com/apolloconfig/apollo/pull/5018)

------------------
All issues and pull requests are [here](https://github.com/apolloconfig/apollo/milestone/13?closed=1)
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ public String emailConfigHost() {
return getValue("email.config.host", "");
}

public Boolean emailConfigUseSsl() {
return getBooleanProperty("email.config.useSsl", false);

Check warning on line 227 in apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/config/PortalConfig.java

View check run for this annotation

Codecov / codecov/patch

apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/config/PortalConfig.java#L227

Added line #L227 was not covered by tests
}

public String emailConfigUser() {
return getValue("email.config.user", "");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.ctrip.framework.apollo.portal.entity.bo.Email;
import com.ctrip.framework.apollo.portal.spi.EmailService;
import com.ctrip.framework.apollo.tracer.Tracer;
import com.sun.mail.smtp.SMTPSSLTransport;
import com.sun.mail.smtp.SMTPTransport;
import java.io.ByteArrayInputStream;
import java.io.IOException;
Expand Down Expand Up @@ -49,7 +50,10 @@ public void send(Email email) {
return;
}

Boolean useSsl = portalConfig.emailConfigUseSsl();

Check warning on line 53 in apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultEmailService.java

View check run for this annotation

Codecov / codecov/patch

apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultEmailService.java#L53

Added line #L53 was not covered by tests

SMTPTransport t = null;
SMTPSSLTransport tSsl = null;

Check warning on line 56 in apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultEmailService.java

View check run for this annotation

Codecov / codecov/patch

apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultEmailService.java#L56

Added line #L56 was not covered by tests
try {
Properties prop = System.getProperties();
Session session = Session.getInstance(prop, null);
Expand All @@ -64,21 +68,32 @@ public void send(Email email) {
String user = portalConfig.emailConfigUser();
String password = portalConfig.emailConfigPassword();

t = (SMTPTransport) session.getTransport("smtp");
t.connect(host, user, password);
msg.saveChanges();
t.sendMessage(msg, msg.getAllRecipients());
logger.debug("email response: {}", t.getLastServerResponse());
if (useSsl) {
t = (SMTPTransport) session.getTransport("smtp");
t.connect(host, user, password);
msg.saveChanges();
t.sendMessage(msg, msg.getAllRecipients());
logger.debug("email response: {}", t.getLastServerResponse());

Check warning on line 76 in apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultEmailService.java

View check run for this annotation

Codecov / codecov/patch

apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultEmailService.java#L72-L76

Added lines #L72 - L76 were not covered by tests
} else {
tSsl = (SMTPSSLTransport) session.getTransport("smtps");
tSsl.connect(host, user, password);
msg.saveChanges();
tSsl.sendMessage(msg, msg.getAllRecipients());
logger.debug("email response: {}", tSsl.getLastServerResponse());

Check warning on line 82 in apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultEmailService.java

View check run for this annotation

Codecov / codecov/patch

apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultEmailService.java#L78-L82

Added lines #L78 - L82 were not covered by tests
}
} catch (Exception e) {
logger.error("send email failed.", e);
Tracer.logError("send email failed.", e);
} finally {
if (t != null) {
try {
try {
if (t != null) {
t.close();
} catch (Exception e) {
// nothing
}
if (tSsl != null) {
tSsl.close();

Check warning on line 93 in apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultEmailService.java

View check run for this annotation

Codecov / codecov/patch

apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultEmailService.java#L93

Added line #L93 was not covered by tests
}
} catch (Exception e) {

Check warning on line 95 in apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultEmailService.java

View check run for this annotation

Codecov / codecov/patch

apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultEmailService.java#L95

Added line #L95 was not covered by tests
// nothing
}
}
}
Expand Down

0 comments on commit 8fe3b1c

Please sign in to comment.