Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support send email with SMTPSSLTransport #5018

Closed
wants to merge 2 commits into from
Closed
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
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 @@
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 @@
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 @@
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like unnecessary to declare a new variable tSsl. Could we consider streamlining the code even further, as suggested below?

String protocol = useSSl ? "smtps" : "smtp";
t = (SMTPTransport) session.getTransport(protocol);

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 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 {
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 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
Loading