Search in sources :

Example 1 with MailSessionAuthenticator

use of com.outjected.email.impl.MailSessionAuthenticator in project simple-email by codylerum.

the class MailUtility method createSession.

public static Session createSession(SessionConfig mailConfig) {
    if (!Strings.isNullOrBlank(mailConfig.getJndiSessionName())) {
        try {
            return InitialContext.doLookup(mailConfig.getJndiSessionName());
        } catch (NamingException e) {
            throw new MailException("Unable to lookup JNDI JavaMail Session", e);
        }
    }
    Session session;
    Properties props = new Properties();
    if (!Strings.isNullOrBlank(mailConfig.getServerHost()) && mailConfig.getServerPort() > 0) {
        props.setProperty("mail.smtp.host", mailConfig.getServerHost());
        props.setProperty("mail.smtp.port", mailConfig.getServerPort().toString());
        props.setProperty("mail.smtp.starttls.enable", mailConfig.getEnableTls().toString());
        props.setProperty("mail.smtp.starttls.required", mailConfig.getRequireTls().toString());
        props.setProperty("mail.smtp.ssl.enable", mailConfig.getEnableSsl().toString());
        props.setProperty("mail.smtp.auth", mailConfig.getAuth().toString());
    } else {
        throw new MailException("Server Host and Server  Port must be set in MailConfig");
    }
    if (!Strings.isNullOrBlank(mailConfig.getDomainName())) {
        props.put(MailUtility.DOMAIN_PROPERTY_KEY, mailConfig.getDomainName());
    }
    if (mailConfig.getUsername() != null && mailConfig.getUsername().length() != 0 && mailConfig.getPassword() != null && mailConfig.getPassword().length() != 0) {
        MailSessionAuthenticator authenticator = new MailSessionAuthenticator(mailConfig.getUsername(), mailConfig.getPassword());
        session = Session.getInstance(props, authenticator);
    } else {
        session = Session.getInstance(props, null);
    }
    return session;
}
Also used : NamingException(javax.naming.NamingException) MailSessionAuthenticator(com.outjected.email.impl.MailSessionAuthenticator) MailException(com.outjected.email.api.MailException) Properties(java.util.Properties) Session(javax.mail.Session)

Aggregations

MailException (com.outjected.email.api.MailException)1 MailSessionAuthenticator (com.outjected.email.impl.MailSessionAuthenticator)1 Properties (java.util.Properties)1 Session (javax.mail.Session)1 NamingException (javax.naming.NamingException)1