-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support default email feature via portal config
- Loading branch information
1 parent
78fc113
commit 2097464
Showing
5 changed files
with
137 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 92 additions & 3 deletions
95
.../src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultEmailService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters