Example usage for org.apache.commons.mail Email setSSLOnConnect

List of usage examples for org.apache.commons.mail Email setSSLOnConnect

Introduction

In this page you can find the example usage for org.apache.commons.mail Email setSSLOnConnect.

Prototype

public Email setSSLOnConnect(final boolean ssl) 

Source Link

Document

Sets whether SSL/TLS encryption should be enabled for the SMTP transport upon connection (SMTPS/POPS).

Usage

From source file:org.kuali.mobility.email.service.EmailServiceImpl.java

@Override
public boolean sendEmail(String body, String subject, String emailAddressTo, String emailAddressFrom) {
    boolean emailSent = false;

    if (emailAddressFrom == null || StringUtils.isEmpty(emailAddressFrom)) {
        emailAddressFrom = kmeProperties.getProperty("email.from");
        if (emailAddressFrom == null) {
            return emailSent;
        }//from   w ww  . j a v  a2  s  .com
    }

    if (emailAddressTo == null || StringUtils.isEmpty(emailAddressTo)) {
        return emailSent;
    }

    if (subject == null || StringUtils.isEmpty(subject)) {
        return emailSent;
    }

    if (body == null || StringUtils.isEmpty(body)) {
        return emailSent;
    }

    try {
        Email email = new SimpleEmail();
        email.setHostName(kmeProperties.getProperty("email.host"));
        email.setSmtpPort(Integer.parseInt(kmeProperties.getProperty("email.port")));
        email.setAuthenticator(new DefaultAuthenticator(kmeProperties.getProperty("email.username"),
                kmeProperties.getProperty("email.passsword")));
        email.setSSLOnConnect(true);
        email.setFrom(emailAddressFrom);
        email.setSubject(subject);
        email.setMsg(body);
        email.addTo(emailAddressTo);
        email.send();
        emailSent = true;
        LOG.debug("Mail Sent...");
    } catch (EmailException e) {
        LOG.error("Mail send failed...", e);
    }
    return emailSent;
}

From source file:org.opencms.mail.CmsMailUtil.java

/**
 * Configures the mail from the given mail host configuration data.<p>
 *
 * @param host the mail host configuration
 * @param mail the email instance/*  w ww  . ja v a  2 s  .  c  o m*/
 */
public static void configureMail(CmsMailHost host, Email mail) {

    // set the host to the default mail host
    mail.setHostName(host.getHostname());
    mail.setSmtpPort(host.getPort());

    // check if username and password are provided
    String userName = host.getUsername();
    if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(userName)) {
        // authentication needed, set user name and password
        mail.setAuthentication(userName, host.getPassword());
    }
    String security = host.getSecurity() != null ? host.getSecurity().trim() : null;
    if (SECURITY_SSL.equalsIgnoreCase(security)) {
        mail.setSslSmtpPort("" + host.getPort());
        mail.setSSLOnConnect(true);
    } else if (SECURITY_STARTTLS.equalsIgnoreCase(security)) {
        mail.setStartTLSEnabled(true);
    }

    try {
        // set default mail from address
        mail.setFrom(OpenCms.getSystemInfo().getMailSettings().getMailFromDefault());
    } catch (EmailException e) {
        // default email address is not valid, log error
        LOG.error(Messages.get().getBundle().key(Messages.LOG_INVALID_SENDER_ADDRESS_0), e);
    }
}

From source file:org.openhab.binding.mail.internal.SMTPHandler.java

/**
 * use this server to send a mail//  w w w . j  ava2  s . c  om
 *
 * @param mail the Email that needs to be sent
 * @return true if successful, false if failed
 */
public boolean sendMail(Email mail) {
    try {
        if (mail.getFromAddress() == null) {
            mail.setFrom(config.sender);
        }
        mail.setHostName(config.hostname);
        switch (config.security) {
        case SSL:
            mail.setSSLOnConnect(true);
            mail.setSslSmtpPort(config.port.toString());
            break;
        case TLS:
            mail.setStartTLSEnabled(true);
            mail.setStartTLSRequired(true);
            mail.setSmtpPort(config.port);
            break;
        case PLAIN:
            mail.setSmtpPort(config.port);
        }
        if (!config.username.isEmpty() && !config.password.isEmpty()) {
            mail.setAuthenticator(new DefaultAuthenticator(config.username, config.password));
        }
        mail.send();
    } catch (EmailException e) {
        logger.warn("Trying to send mail but exception occured: {} ", e.getMessage());
        return false;
    }
    return true;
}

From source file:org.ow2.frascati.akka.fabric.peakforecast.lib.EmailImpl.java

@Override
public void send(String message) {

    try {/*from   w w  w . ja  va2s  .  c o  m*/
        Email email = new SimpleEmail();
        email.setHostName("smtp.googlemail.com");
        email.setSmtpPort(465);
        //email.setAuthenticator(new DefaultAuthenticator("username", "password"));
        email.setAuthenticator(new DefaultAuthenticator("fouomenedaniel@gmail.com", "motdepasse"));
        email.setSSLOnConnect(true);
        email.setFrom("fouomenedaniel@gmail.com");
        email.setSubject("Alert PeakForecast");
        email.setMsg(message);
        String listtabmail[] = listEmails.split(" ");
        for (int i = 0; i < listtabmail.length; i++) {

            email.addTo(listtabmail[i]);

        }
        email.send();
        System.out.println("Message Email envoy !!!");
    } catch (EmailException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:org.paxml.bean.EmailTag.java

private Email createEmail(Collection<String> to, Collection<String> cc, Collection<String> bcc)
        throws EmailException {
    Email email;
    if (attachment == null || attachment.isEmpty()) {
        email = new SimpleEmail();
    } else {//  w  w w.  j  a  v  a2s. c  o  m
        MultiPartEmail mpemail = new MultiPartEmail();
        for (Object att : attachment) {
            mpemail.attach(makeAttachment(att.toString()));
        }
        email = mpemail;
    }

    if (StringUtils.isNotEmpty(username)) {
        String pwd = null;
        if (password instanceof Secret) {
            pwd = ((Secret) password).getDecrypted();
        } else if (password != null) {
            pwd = password.toString();
        }
        email.setAuthenticator(new DefaultAuthenticator(username, pwd));
    }

    email.setHostName(findHost());
    email.setSSLOnConnect(ssl);
    if (port > 0) {
        if (ssl) {
            email.setSslSmtpPort(port + "");
        } else {
            email.setSmtpPort(port);
        }
    }
    if (replyTo != null) {
        for (Object r : replyTo) {
            email.addReplyTo(r.toString());
        }
    }
    email.setFrom(from);
    email.setSubject(subject);
    email.setMsg(text);
    if (to != null) {
        for (String r : to) {
            email.addTo(r);
        }
    }
    if (cc != null) {
        for (String r : cc) {
            email.addCc(r);
        }
    }
    if (bcc != null) {
        for (String r : bcc) {
            email.addBcc(r);
        }
    }
    email.setSSLCheckServerIdentity(sslCheckServerIdentity);
    email.setStartTLSEnabled(tls);
    email.setStartTLSRequired(tls);
    return email;
}

From source file:org.sonatype.nexus.internal.email.EmailManagerImpl.java

/**
 * Apply server configuration to email./* w  w  w .j a va 2s .  c  o m*/
 */
@VisibleForTesting
Email apply(final EmailConfiguration configuration, final Email mail) throws EmailException {
    mail.setHostName(configuration.getHost());
    mail.setSmtpPort(configuration.getPort());
    mail.setAuthentication(configuration.getUsername(), configuration.getPassword());

    mail.setStartTLSEnabled(configuration.isStartTlsEnabled());
    mail.setStartTLSRequired(configuration.isStartTlsRequired());
    mail.setSSLOnConnect(configuration.isSslOnConnectEnabled());
    mail.setSSLCheckServerIdentity(configuration.isSslCheckServerIdentityEnabled());
    mail.setSslSmtpPort(Integer.toString(configuration.getPort()));

    // default from address
    if (mail.getFromAddress() == null) {
        mail.setFrom(configuration.getFromAddress());
    }

    // apply subject prefix if configured
    String subjectPrefix = configuration.getSubjectPrefix();
    if (subjectPrefix != null) {
        String subject = mail.getSubject();
        mail.setSubject(String.format("%s %s", subjectPrefix, subject));
    }

    // do this last (mail properties are set up from the email fields when you get the mail session)
    if (configuration.isNexusTrustStoreEnabled()) {
        SSLContext context = trustStore.getSSLContext();
        Session session = mail.getMailSession();
        Properties properties = session.getProperties();
        properties.remove(EmailConstants.MAIL_SMTP_SOCKET_FACTORY_CLASS);
        properties.put(EmailConstants.MAIL_SMTP_SSL_ENABLE, true);
        properties.put("mail.smtp.ssl.socketFactory", context.getSocketFactory());
    }

    return mail;
}

From source file:org.thousandturtles.gmail_demo.Main.java

public static void main(String[] args) {
    MailInfoRetriever retriever = new CLIMailInfoRetriever();
    MailInfo mailInfo = retriever.retrieveMailInfo();

    if (mailInfo.isNoAuth()) {
        System.out.println("Using No auth, mail with aspmx.l.google.com");
    } else {/*from   w w w.j a v  a 2  s.  c  o m*/
        System.out.printf("Username: %s%n", mailInfo.getUsername());
        System.out.printf("Password: %c***%c%n", mailInfo.getPassword().charAt(0),
                mailInfo.getPassword().charAt(mailInfo.getPassword().length() - 1));
    }
    System.out.printf("Mail target: %s%n", mailInfo.getMailTo());

    try {
        Email mail = new SimpleEmail();
        if (mailInfo.isNoAuth()) {
            mail.setHostName("aspmx.l.google.com");
            mail.setSmtpPort(25);
            mail.setFrom("no-reply@thousandturtles.org");
        } else {
            mail.setHostName("smtp.gmail.com");
            mail.setSmtpPort(465);
            mail.setAuthentication(mailInfo.getUsername(), mailInfo.getPassword());
            mail.setSSLOnConnect(true);
            mail.setFrom(mailInfo.getMailer());
        }
        mail.setCharset("UTF-8");
        mail.addTo(mailInfo.getMailTo(), "White Mouse");
        mail.setSubject("");
        mail.setMsg("?\nThis is a test mail!\n");
        mail.send();

        System.out.println("Mail sent successfully.");
    } catch (EmailException ee) {
        ee.printStackTrace();
    }
}

From source file:rapternet.irc.bots.wheatley.listeners.AutodlText.java

@Override
public void onMessage(final MessageEvent event) throws Exception {
    String message = Colors.removeFormattingAndColors(event.getMessage());
    if (event.getUser().getNick().equals("SHODAN")) { //Auto Download bot nick
        if (message.startsWith("Saved")) {
            // LOAD XML
            File fXmlFile = new File("Settings.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);
            NodeList nList = doc.getElementsByTagName("email");
            int mailsetting = 0; //NOTE: can setup multiple email profiles for different functions here
            Node nNode = nList.item(mailsetting);
            Element eElement = (Element) nNode;
            //SEND EMAIL STUFF
            Email email = new SimpleEmail();
            email.setHostName(eElement.getElementsByTagName("hostname").item(0).getTextContent());
            email.setSmtpPort(465);//from  w w w  .ja v a  2s .  c  o m
            email.setAuthenticator(
                    new DefaultAuthenticator(eElement.getElementsByTagName("username").item(0).getTextContent(),
                            eElement.getElementsByTagName("password").item(0).getTextContent()));
            email.setSSLOnConnect(true);
            email.setFrom(eElement.getElementsByTagName("from").item(0).getTextContent());
            email.setSubject(eElement.getElementsByTagName("subject").item(0).getTextContent());
            email.setMsg(message);
            email.addTo(eElement.getElementsByTagName("to").item(0).getTextContent());
            email.send();
            event.getBot().sendIRC().message(event.getChannel().getName(),
                    "AAAaaaAAHhhH I just connected to an email server, I feel dirty");
        }
    }
}

From source file:sce.Mail.java

public static void sendMail(String subject, String message, String recipient, String headerName,
        String headerValue) {/*  www.  j  av a2 s .c o m*/
    try {
        //load mail settings from quartz.properties
        Properties prop = new Properties();
        prop.load(Mail.class.getResourceAsStream("quartz.properties"));
        String smtp_hostname = prop.getProperty("smtp_hostname");
        int smtp_port = Integer.parseInt(prop.getProperty("smtp_port"));
        boolean smtp_ssl = prop.getProperty("smtp_ssl").equalsIgnoreCase("true");
        String smtp_username = prop.getProperty("smtp_username");
        String smtp_password = prop.getProperty("smtp_password");
        String smtp_mailfrom = prop.getProperty("smtp_mailfrom");

        Email email = new SimpleEmail();
        email.setHostName(smtp_hostname);
        email.setSmtpPort(smtp_port);
        email.setAuthenticator(new DefaultAuthenticator(smtp_username, smtp_password));
        email.setSSLOnConnect(smtp_ssl);
        email.setFrom(smtp_mailfrom);
        email.setSubject(subject);
        email.setMsg(message);
        String[] recipients = recipient.split(";"); //this is semicolon separated array of recipients
        for (String tmp : recipients) {
            email.addTo(tmp);
        }
        //email.addHeader(headerName, headerValue);
        email.send();
    } catch (EmailException e) {
    } catch (IOException e) {
    }
}