use of com.sun.mail.smtp.SMTPTransport in project apollo by apolloconfig.
the class DefaultEmailService method send.
@Override
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
}
}
}
}
Aggregations