use of com.outjected.email.api.MailException in project simple-email by codylerum.
the class MessageConverter method convertMessage.
public EmailMessage convertMessage(Message m) throws MailException {
emailMessage = new EmailMessage();
try {
emailMessage.setFromAddresses(MailUtility.getInternetAddressses(m.getFrom()));
emailMessage.getToAddresses().addAll(MailUtility.getInternetAddressses(m.getRecipients(RecipientType.TO)));
emailMessage.setCcAddresses(MailUtility.getInternetAddressses(m.getRecipients(RecipientType.CC)));
emailMessage.setBccAddresses(MailUtility.getInternetAddressses(m.getRecipients(RecipientType.BCC)));
emailMessage.setSubject(m.getSubject());
emailMessage.setMessageId(m.getHeader("Message-ID")[0]);
emailMessage.getHeaders().addAll(MailUtility.getHeaders(m.getAllHeaders()));
if (m.getContentType().toLowerCase().contains("multipart/")) {
addMultiPart((MimeMultipart) m.getContent());
} else if (m.isMimeType("text/plain")) {
emailMessage.setTextBody((String) m.getContent());
}
} catch (IOException e) {
throw new MailException(e);
} catch (MessagingException e) {
throw new MailException(e);
}
return emailMessage;
}
use of com.outjected.email.api.MailException 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