Skip to content

Commit

Permalink
feat: support default email feature via portal config
Browse files Browse the repository at this point in the history
  • Loading branch information
nisiyong authored and leesing0107 committed Oct 30, 2021
1 parent 78fc113 commit 2097464
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 14 deletions.
4 changes: 4 additions & 0 deletions apollo-portal/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
</dependency>
<!-- end of JDK 1.8+ -->
<!-- JDK 11+ -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,28 @@ public String consumerTokenSalt() {
return getValue("consumer.token.salt", "apollo-portal");
}

public boolean isEmailEnabled() {
return getBooleanProperty("email.enabled", false);
}

public String emailConfigHost() {
return getValue("email.config.host", "");
}

public String emailConfigUser() {
return getValue("email.config.user", "");
}

public String emailConfigPassword() {
return getValue("email.config.password", "");
}

public String emailSender() {
return getValue("email.sender");
String value = getValue("email.sender", "");
if (Strings.isNullOrEmpty(value)) {
value = emailConfigUser();
}
return value;
}

public String emailTemplateFramework() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public List<String> getRecipients() {
return recipients;
}

public String getRecipientsString() {
return String.join(",", recipients);
}

public void setRecipients(List<String> recipients) {
this.recipients = recipients;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,101 @@
package com.ctrip.framework.apollo.portal.spi.defaultimpl;

import com.ctrip.framework.apollo.portal.component.config.PortalConfig;
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.SMTPTransport;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.annotation.Resource;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DefaultEmailService implements EmailService{
public class DefaultEmailService implements EmailService {

private final Logger logger = LoggerFactory.getLogger(DefaultEmailService.class);

@Resource
private PortalConfig portalConfig;

@Override
public void send(Email email){
//do nothing
public void send(Email email) {
if (!portalConfig.isEmailEnabled()) {
return;
}

SMTPTransport t = null;
try {
Properties prop = System.getProperties();
Session session = Session.getInstance(prop, null);

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())));

String host = portalConfig.emailConfigHost();
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());
} catch (Exception e) {
logger.error("send email failed.", e);
Tracer.logError("send email failed.", e);
} finally {
if (t != null) {
try {
t.close();
} catch (Exception e) {
// nothing
}
}
}
}

static class HTMLDataSource implements DataSource {

private String html;

HTMLDataSource(String htmlString) {
html = htmlString;
}

@Override
public InputStream getInputStream() throws IOException {
if (html == null) {
throw new IOException("html message is null!");
}
return new ByteArrayInputStream(html.getBytes());
}

@Override
public OutputStream getOutputStream() throws IOException {
throw new IOException("This DataHandler cannot write HTML");
}

@Override
public String getContentType() {
return "text/html";
}

@Override
public String getName() {
return "HTMLDataSource";
}
}
}
26 changes: 16 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@
<platform-bom.version>Cairo-SR4</platform-bom.version>
<spring-boot.version>2.0.5.RELEASE</spring-boot.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
<jaxb.version>2.3.0</jaxb.version>
<javax.activation.version>1.1.1</javax.activation.version>
<javassist.version>3.23.1-GA</javassist.version>
<jaxb.version>2.3.0</jaxb.version>
<javax.activation.version>1.1.1</javax.activation.version>
<javax.mail.version>1.6.2</javax.mail.version>
<javassist.version>3.23.1-GA</javassist.version>
<!-- Plugins Version -->
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
Expand Down Expand Up @@ -349,13 +350,18 @@
<artifactId>activation</artifactId>
<version>${javax.activation.version}</version>
</dependency>
<!-- JDK 11+ -->
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>${javassist.version}</version>
</dependency>
<!-- end of JDK 11+ -->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>${javax.mail.version}</version>
</dependency>
<!-- JDK 11+ -->
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>${javassist.version}</version>
</dependency>
<!-- end of JDK 11+ -->
</dependencies>
</dependencyManagement>

Expand Down

0 comments on commit 2097464

Please sign in to comment.