diff --git a/apollo-portal/pom.xml b/apollo-portal/pom.xml
index f124fdcd2c6..21aa038cede 100644
--- a/apollo-portal/pom.xml
+++ b/apollo-portal/pom.xml
@@ -50,6 +50,10 @@
javax.activation
activation
+
+ com.sun.mail
+ javax.mail
+
diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/config/PortalConfig.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/config/PortalConfig.java
index 3f99ab077a7..008e27929ba 100644
--- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/config/PortalConfig.java
+++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/config/PortalConfig.java
@@ -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() {
diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/entity/bo/Email.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/entity/bo/Email.java
index 91ff7f5a1b7..cb6df556ab7 100644
--- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/entity/bo/Email.java
+++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/entity/bo/Email.java
@@ -21,6 +21,10 @@ public List getRecipients() {
return recipients;
}
+ public String getRecipientsString() {
+ return String.join(",", recipients);
+ }
+
public void setRecipients(List recipients) {
this.recipients = recipients;
}
diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultEmailService.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultEmailService.java
index b2c9879af18..53b14ed5ca0 100644
--- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultEmailService.java
+++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultEmailService.java
@@ -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";
+ }
}
}
diff --git a/pom.xml b/pom.xml
index aa0af2ffaac..798d6c2e2c5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -79,9 +79,10 @@
Cairo-SR4
2.0.5.RELEASE
Finchley.SR1
- 2.3.0
- 1.1.1
- 3.23.1-GA
+ 2.3.0
+ 1.1.1
+ 1.6.2
+ 3.23.1-GA
3.6.0
2.19.1
@@ -349,13 +350,18 @@
activation
${javax.activation.version}
-
-
- org.javassist
- javassist
- ${javassist.version}
-
-
+
+ com.sun.mail
+ javax.mail
+ ${javax.mail.version}
+
+
+
+ org.javassist
+ javassist
+ ${javassist.version}
+
+