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;
}
Aggregations