Search in sources :

Example 1 with MailSSLSocketFactory

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;
    }
}
Also used : MailSSLSocketFactory(com.sun.mail.util.MailSSLSocketFactory) InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) GeneralSecurityException(java.security.GeneralSecurityException) Properties(java.util.Properties) Session(javax.mail.Session) SMTPTransport(com.sun.mail.smtp.SMTPTransport)

Example 2 with MailSSLSocketFactory

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");
    }
}
Also used : MailSSLSocketFactory(com.sun.mail.util.MailSSLSocketFactory)

Aggregations

MailSSLSocketFactory (com.sun.mail.util.MailSSLSocketFactory)2 SMTPTransport (com.sun.mail.smtp.SMTPTransport)1 GeneralSecurityException (java.security.GeneralSecurityException)1 Properties (java.util.Properties)1 Message (javax.mail.Message)1 MessagingException (javax.mail.MessagingException)1 Session (javax.mail.Session)1 InternetAddress (javax.mail.internet.InternetAddress)1 MimeMessage (javax.mail.internet.MimeMessage)1