use of com.sun.mail.util.MailSSLSocketFactory in project GNS by MobilityFirst.
the class Email method simpleMail.
/**
*
* @param subject
* @param recipient
* @param text
* @param suppressWarning
* @return true if the mail was sent
*/
public static boolean simpleMail(String subject, String recipient, String text, boolean suppressWarning) {
try {
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
Properties props = new Properties();
props.setProperty("mail.smtp.ssl.enable", "true");
//props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.ssl.socketFactory", sf);
Session session = Session.getInstance(props);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(Config.getGlobalString(GNSConfig.GNSC.SUPPORT_EMAIL)));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
message.setSubject(subject);
message.setText(text);
SMTPTransport t = (SMTPTransport) session.getTransport("smtp");
try {
t.connect(SMTP_HOST, Config.getGlobalString(GNSConfig.GNSC.ADMIN_EMAIL), Config.getGlobalString(GNSConfig.GNSC.ADMIN_PASSWORD));
t.sendMessage(message, message.getAllRecipients());
getLogger().log(Level.FINE, "Email response: {0}", t.getLastServerResponse());
} finally {
t.close();
}
getLogger().log(Level.FINE, "Successfully sent email to {0} with message: {1}", new Object[] { recipient, text });
return true;
} catch (GeneralSecurityException | MessagingException e) {
if (!suppressWarning) {
getLogger().log(Level.WARNING, "Unable to send email: {0}", e);
}
return false;
}
}
use of com.sun.mail.util.MailSSLSocketFactory in project Xponents by OpenSextant.
the class MailConfig method setAdvancedSettings.
/**
* A conveneince wrapper around setting the SSL socket factory and other parameters.
* @throws GeneralSecurityException error related to mail/socket factory or other issue
*/
public void setAdvancedSettings() throws GeneralSecurityException {
if (this.isSSLEnabled()) {
MailSSLSocketFactory socketFactory = new MailSSLSocketFactory();
socketFactory.setTrustAllHosts(true);
this.put("mail.imaps.ssl.socketFactory", socketFactory);
// Default?
// setProperty("mail.imap.socketFactory.class",
// "javax.net.ssl.SSLSocketFactory");
}
}
Aggregations