Skip to content

Commit

Permalink
support send email with SMTPSSLTransport
Browse files Browse the repository at this point in the history
  • Loading branch information
victor.quan committed Nov 15, 2023
1 parent 6812a1a commit d3d338e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
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,6 +50,50 @@ 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
if (useSsl) {
sendWithSsl(email);

Check warning on line 55 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#L55

Added line #L55 was not covered by tests
} else {
sendWithoutSsl(email);

Check warning on line 57 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#L57

Added line #L57 was not covered by tests
}
}

Check warning on line 59 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#L59

Added line #L59 was not covered by tests

private void sendWithSsl(Email email) {
SMTPSSLTransport t = null;

Check warning on line 62 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#L62

Added line #L62 was not covered by tests
try {
Properties prop = System.getProperties();
Session session = Session.getInstance(prop, null);

Check warning on line 65 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#L64-L65

Added lines #L64 - L65 were not covered by tests

Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(email.getSenderEmailAddress()));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email.getRecipientsString(), false));
msg.setSubject(email.getSubject());
msg.setDataHandler(new DataHandler(new HTMLDataSource(email.getBody())));

Check warning on line 71 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#L67-L71

Added lines #L67 - L71 were not covered by tests

String host = portalConfig.emailConfigHost();
String user = portalConfig.emailConfigUser();
String password = portalConfig.emailConfigPassword();

Check warning on line 75 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#L73-L75

Added lines #L73 - L75 were not covered by tests

t = (SMTPSSLTransport) session.getTransport("smtps");
t.connect(host, user, password);
msg.saveChanges();
t.sendMessage(msg, msg.getAllRecipients());
logger.debug("email response: {}", t.getLastServerResponse());
} catch (Exception e) {
logger.error("send email failed.", e);
Tracer.logError("send email failed.", e);

Check warning on line 84 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#L77-L84

Added lines #L77 - L84 were not covered by tests
} finally {
if (t != null) {
try {
t.close();
} catch (Exception e) {

Check warning on line 89 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#L88-L89

Added lines #L88 - L89 were not covered by tests
// nothing
}

Check warning on line 91 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#L91

Added line #L91 was not covered by tests
}
}
}

Check warning on line 94 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#L94

Added line #L94 was not covered by tests

private void sendWithoutSsl(Email email) {
SMTPTransport t = null;
try {
Properties prop = System.getProperties();
Expand Down

0 comments on commit d3d338e

Please sign in to comment.