Search in sources :

Example 16 with SMTPTransport

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
            }
        }
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) DataHandler(javax.activation.DataHandler) Properties(java.util.Properties) IOException(java.io.IOException) SMTPTransport(com.sun.mail.smtp.SMTPTransport) Session(javax.mail.Session)

Aggregations

SMTPTransport (com.sun.mail.smtp.SMTPTransport)16 Session (javax.mail.Session)9 InternetAddress (javax.mail.internet.InternetAddress)9 MimeMessage (javax.mail.internet.MimeMessage)9 Properties (java.util.Properties)7 Message (javax.mail.Message)7 MessagingException (javax.mail.MessagingException)7 IOException (java.io.IOException)4 Date (java.util.Date)4 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)3 Session (jakarta.mail.Session)3 Socket (java.net.Socket)3 DataHandler (javax.activation.DataHandler)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Transport (javax.mail.Transport)2 MimeBodyPart (javax.mail.internet.MimeBodyPart)2 MimeMultipart (javax.mail.internet.MimeMultipart)2 Test (org.junit.Test)2 SMTPAddressFailedException (com.sun.mail.smtp.SMTPAddressFailedException)1 SMTPAddressSucceededException (com.sun.mail.smtp.SMTPAddressSucceededException)1