Example usage for org.apache.commons.mail MultiPartEmail setStartTLSRequired

List of usage examples for org.apache.commons.mail MultiPartEmail setStartTLSRequired

Introduction

In this page you can find the example usage for org.apache.commons.mail MultiPartEmail setStartTLSRequired.

Prototype

public Email setStartTLSRequired(final boolean startTlsRequired) 

Source Link

Document

Set or disable the required STARTTLS encryption.

Usage

From source file:com.commander4j.email.JeMail.java

public void postMail(String recipientsTO[], String subject, String message, String attachmentFilename,
        String attachmentLongFilename) throws MessagingException {

    logger.debug("SMTP_AUTH_REQD=" + SMTP_AUTH_REQD);
    logger.debug("MAIL_SMTP_HOST_NAME=" + SMTP_HOST_NAME);
    logger.debug("MAIL_SMTP_AUTH_USER=" + SMTP_AUTH_USER);
    logger.debug("MAIL_SMTP_AUTH_PWD=********");
    logger.debug("MAIL_SMTP_PORT=" + MAIL_SMTP_PORT);
    logger.debug("MAIL_SMTP_SSL_PORT=" + MAIL_SMTP_SSL_PORT);
    logger.debug("MAIL_SMTP_FROM_ADRESS=" + SMTP_FROM_ADRESS);
    logger.debug("MAIL_SMTP_USE_SSL=" + SMTP_USE_SSL);

    //Email email = new SimpleEmail();
    logger.debug("Creating MultiPart Email");
    MultiPartEmail email = new MultiPartEmail();

    logger.debug("Setting Host Name to " + SMTP_HOST_NAME);
    email.setHostName(SMTP_HOST_NAME);/*from ww  w.j av a  2s . c  o  m*/
    logger.debug("Setting SMTP Port to " + MAIL_SMTP_PORT);
    email.setSmtpPort(Integer.valueOf(MAIL_SMTP_PORT));

    logger.debug("Setting SMTP SSL Port to " + MAIL_SMTP_SSL_PORT);
    email.setSslSmtpPort(MAIL_SMTP_SSL_PORT);

    logger.debug("Setting Use SSL on Connect to  " + SMTP_USE_SSL);
    email.setSSLOnConnect(Boolean.valueOf(SMTP_USE_SSL));

    logger.debug("Authentication Required =  " + SMTP_AUTH_REQD);

    if (SMTP_AUTH_REQD.toUpperCase().equals("TRUE")) {
        email.setAuthenticator(new DefaultAuthenticator(SMTP_AUTH_USER, SMTP_AUTH_PWD));

    }

    logger.debug("Setting SMTP USE SSL =  " + SMTP_USE_SSL);
    email.setSSLOnConnect(Boolean.valueOf(SMTP_USE_SSL));

    logger.debug("Setting SMTP USE TLS =  " + SMTP_USE_TLS);
    email.setStartTLSEnabled(Boolean.valueOf(SMTP_USE_TLS));
    email.setStartTLSRequired(Boolean.valueOf(SMTP_USE_TLS));

    try {
        logger.debug("From Address =  " + SMTP_FROM_ADRESS);
        email.setFrom(SMTP_FROM_ADRESS);
        email.setSubject(subject);
        email.setMsg(message + "\n\n");
        for (int x = 1; x <= recipientsTO.length; x++) {
            logger.debug("Add To Address =  " + recipientsTO[x - 1]);
            email.addTo(recipientsTO[x - 1]);
        }

        if (JUtility.replaceNullStringwithBlank(attachmentFilename).equals("") == false) {
            // Create the attachment
            EmailAttachment attachment = new EmailAttachment();
            attachment.setPath(attachmentLongFilename);
            attachment.setDisposition(EmailAttachment.ATTACHMENT);
            attachment.setDescription(attachmentFilename);
            attachment.setName(attachmentFilename);

            // add the attachment
            logger.debug("Add Attachment");
            email.attach(attachment);
        }

        logger.debug("Sending");
        email.send();
        logger.debug("Sent successfully");
    } catch (EmailException e) {
        logger.error("Unable to send email : " + e.getCause().getMessage());
    }

}