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

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

Introduction

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

Prototype

@Deprecated
public void setTLS(final boolean withTLS) 

Source Link

Document

Set or disable the STARTTLS encryption.

Usage

From source file:com.pronoiahealth.olhie.server.services.MailSendingService.java

/**
 * Sends a password reset email to the email address provided
 * /* w ww.  ja  v  a 2s . c  o  m*/
 * @param toEmail
 * @param newPwd
 * @throws Exception
 */
public void sendPwdResetMailFromApp(String toEmail, String newPwd) throws Exception {
    Email email = new SimpleEmail();
    email.setSmtpPort(Integer.parseInt(smtpPort));
    email.setAuthenticator(new DefaultAuthenticator(fromAddress, fromPwd));
    email.setDebug(Boolean.parseBoolean(debugEnabled));
    email.setHostName(smtpSever);
    email.setFrom(fromAddress);
    email.setSubject("Reset Olhie Password");
    email.setMsg("You have requested that your password be reset. Your new Olhie password is " + newPwd);
    email.addTo(toEmail);
    email.setTLS(Boolean.parseBoolean(tlsEnabled));
    email.setSocketTimeout(10000);
    email.setSocketConnectionTimeout(12000);
    email.send();
}

From source file:com.pronoiahealth.olhie.server.services.MailSendingService.java

/**
 * Author Request email that goes to the olhie administrator
 * /*w  ww.jav  a2  s .com*/
 * @param toEmail
 * @param userId
 * @param firstName
 * @param lastName
 * @param regId
 * @throws Exception
 */
public void sendRequestAuthorMailFromApp(String toEmail, String userId, String firstName, String lastName,
        String regId) throws Exception {
    Email email = new SimpleEmail();
    email.setSmtpPort(Integer.parseInt(smtpPort));
    email.setAuthenticator(new DefaultAuthenticator(fromAddress, fromPwd));
    email.setDebug(Boolean.parseBoolean(debugEnabled));
    email.setHostName(smtpSever);
    email.setFrom(fromAddress);
    email.setSubject("Author Request");
    email.setMsg("User Id: " + userId + " Name: " + firstName + " " + lastName + " Registration Id: " + regId);
    email.addTo(toEmail);
    email.setTLS(Boolean.parseBoolean(tlsEnabled));
    email.setSocketTimeout(10000);
    email.setSocketConnectionTimeout(12000);
    email.send();
}

From source file:com.pronoiahealth.olhie.server.services.MailSendingService.java

/**
 * @param toEmail//from   w  ww  .j  ava 2 s. c  o m
 * @param userId
 * @param firstName
 * @param lastName
 * @param eventId
 * @param details
 * @throws Exception
 */
public void sendRequestMailForCalendarEventFromApp(String toEmail, String userId, String firstName,
        String lastName, String eventId, String details) throws Exception {
    Email email = new SimpleEmail();
    email.setSmtpPort(Integer.parseInt(smtpPort));
    email.setAuthenticator(new DefaultAuthenticator(fromAddress, fromPwd));
    email.setDebug(Boolean.parseBoolean(debugEnabled));
    email.setHostName(smtpSever);
    email.setFrom(fromAddress);
    email.setSubject("Calendar Event Request");
    email.setMsg("User Id: " + userId + " Name: " + firstName + " " + lastName + " Event Id: " + eventId + "\n"
            + details);
    email.addTo(toEmail);
    email.setTLS(Boolean.parseBoolean(tlsEnabled));
    email.setSocketTimeout(10000);
    email.setSocketConnectionTimeout(12000);
    email.send();
}

From source file:com.music.service.EmailService.java

private Email createEmail(boolean html) {
    Email e = null;
    if (html) {/*  w  w  w . ja  va 2s  .  c o  m*/
        e = new HtmlEmail();
    } else {
        e = new SimpleEmail();
    }
    e.setHostName(smtpHost);
    if (!StringUtils.isEmpty(smtpUser)) {
        e.setAuthentication(smtpUser, smtpPassword);
    }

    if (!StringUtils.isEmpty(smtpBounceEmail)) {
        e.setBounceAddress(smtpBounceEmail);
    }

    e.setTLS(true);
    e.setSmtpPort(587); //tls port
    e.setCharset("UTF8");
    //e.setDebug(true);

    return e;
}

From source file:cl.alma.scrw.bpmn.tasks.MailActivityBehavior.java

protected void setMailServerProperties(Email email) {
    ProcessEngineConfigurationImpl processEngineConfiguration = Context.getProcessEngineConfiguration();

    String host = processEngineConfiguration.getMailServerHost();
    if (host == null) {
        throw new ActivitiException("Could not send email: no SMTP host is configured");
    }/*w w w  . j av  a 2s  .  c  om*/
    email.setHostName(host);

    int port = processEngineConfiguration.getMailServerPort();
    email.setSmtpPort(port);

    email.setTLS(processEngineConfiguration.getMailServerUseTLS());

    String user = processEngineConfiguration.getMailServerUsername();
    String password = processEngineConfiguration.getMailServerPassword();
    if (user != null && password != null) {
        email.setAuthentication(user, password);
    }
}

From source file:iddb.core.util.MailManager.java

private void setEmailProps(Email email) throws EmailException {
    email.setFrom(props.getProperty("from"), "IPDB");
    if (props.containsKey("bounce"))
        email.setBounceAddress(props.getProperty("bounce"));
    if (props.containsKey("host"))
        email.setHostName(props.getProperty("host"));
    if (props.containsKey("port"))
        email.setSmtpPort(Integer.parseInt(props.getProperty("port")));
    if (props.containsKey("username"))
        email.setAuthenticator(// w  w w  . j  a  va  2  s  . c  o m
                new DefaultAuthenticator(props.getProperty("username"), props.getProperty("password")));
    if (props.containsKey("ssl") && props.getProperty("ssl").equalsIgnoreCase("true"))
        email.setSSL(true);
    if (props.containsKey("tls") && props.getProperty("tls").equalsIgnoreCase("true"))
        email.setTLS(true);
    if (props.containsKey("debug") && props.getProperty("debug").equalsIgnoreCase("true"))
        email.setDebug(true);
}

From source file:com.googlecode.fascinator.messaging.EmailNotificationConsumer.java

private void sendEmails(List<String> toList, List<String> ccList, String subject, String body,
        String fromAddress, String fromName, boolean isHtml) throws EmailException {
    Email email = null;
    if (isHtml) {
        email = new HtmlEmail();
        ((HtmlEmail) email).setHtmlMsg(body);
    } else {//from   w ww . ja v a 2s .c om
        email = new SimpleEmail();
        email.setMsg(body);
    }
    email.setDebug(debug);
    email.setHostName(smtpHost);
    if (smtpUsername != null || smtpPassword != null) {
        email.setAuthentication(smtpUsername, smtpPassword);
    }
    email.setSmtpPort(smtpPort);
    email.setSslSmtpPort(smtpSslPort);
    email.setSSL(smtpSsl);
    email.setTLS(smtpTls);
    email.setSubject(subject);

    for (String to : toList) {
        email.addTo(to);
    }
    if (ccList != null) {
        for (String cc : ccList) {
            email.addCc(cc);
        }
    }
    email.setFrom(fromAddress, fromName);
    email.send();
}

From source file:com.googlecode.fascinator.portal.process.EmailNotifier.java

/**
 * Send the actual email./*from  w  ww .  j a  va  2  s  .c  o  m*/
 * 
 * @param oid
 * @param from
 * @param recipient
 * @param subject
 * @param body
 * @return
 */
public boolean email(String oid, String from, String recipient, String subject, String body) {
    try {
        Email email = new SimpleEmail();
        log.debug("Email host: " + host);
        log.debug("Email port: " + port);
        log.debug("Email username: " + username);
        log.debug("Email from: " + from);
        log.debug("Email to: " + recipient);
        log.debug("Email Subject is: " + subject);
        log.debug("Email Body is: " + body);
        email.setHostName(host);
        email.setSmtpPort(Integer.parseInt(port));
        email.setAuthenticator(new DefaultAuthenticator(username, password));
        // the method setSSL is deprecated on the newer versions of commons
        // email...
        email.setSSL("true".equalsIgnoreCase(ssl));
        email.setTLS("true".equalsIgnoreCase(tls));
        email.setFrom(from);
        email.setSubject(subject);
        email.setMsg(body);
        if (recipient.indexOf(",") >= 0) {
            String[] recs = recipient.split(",");
            for (String rec : recs) {
                email.addTo(rec);
            }
        } else {
            email.addTo(recipient);
        }
        email.send();
    } catch (Exception ex) {
        log.debug("Error sending notification mail for oid:" + oid, ex);
        return false;
    }
    return true;
}

From source file:au.edu.ausstage.utils.EmailManager.java

/**
 * A method for sending a simple email message
 *
 * @param subject the subject of the message
 * @param message the text of the message
 *
 * @return true, if and only if, the email is successfully sent
 *///from w  w  w . jav a2s .  co  m
public boolean sendSimpleMessage(String subject, String message) {

    // check the input parameters
    if (InputUtils.isValid(subject) == false || InputUtils.isValid(message) == false) {
        throw new IllegalArgumentException("The subject and message parameters cannot be null");
    }

    try {
        // define helper variables
        Email email = new SimpleEmail();

        // configure the instance of the email class
        email.setSmtpPort(options.getPortAsInt());

        // define authentication if required
        if (InputUtils.isValid(options.getUser()) == true) {
            email.setAuthenticator(new DefaultAuthenticator(options.getUser(), options.getPassword()));
        }

        // turn on / off debugging
        email.setDebug(DEBUG);

        // set the host name
        email.setHostName(options.getHost());

        // set the from email address
        email.setFrom(options.getFromAddress());

        // set the subject
        email.setSubject(subject);

        // set the message
        email.setMsg(message);

        // set the to address
        String[] addresses = options.getToAddress().split(":");

        for (int i = 0; i < addresses.length; i++) {
            email.addTo(addresses[i]);
        }

        // set the security options
        if (options.getTLS() == true) {
            email.setTLS(true);
        }

        if (options.getSSL() == true) {
            email.setSSL(true);
        }

        // send the email
        email.send();

    } catch (EmailException ex) {
        if (DEBUG) {
            System.err.println("ERROR: Sending of email failed.\n" + ex.toString());
        }

        return false;
    }
    return true;
}

From source file:com.mirth.connect.connectors.smtp.SmtpMessageDispatcher.java

@Override
public void doDispatch(UMOEvent event) throws Exception {
    monitoringController.updateStatus(connector, connectorType, Event.BUSY);
    MessageObject mo = messageObjectController.getMessageObjectFromEvent(event);

    if (mo == null) {
        return;//ww  w . ja  v  a2s  .c  om
    }

    try {
        Email email = null;

        if (connector.isHtml()) {
            email = new HtmlEmail();
        } else {
            email = new MultiPartEmail();
        }

        email.setCharset(connector.getCharsetEncoding());

        email.setHostName(replacer.replaceValues(connector.getSmtpHost(), mo));

        try {
            email.setSmtpPort(Integer.parseInt(replacer.replaceValues(connector.getSmtpPort(), mo)));
        } catch (NumberFormatException e) {
            // Don't set if the value is invalid
        }

        try {
            email.setSocketConnectionTimeout(
                    Integer.parseInt(replacer.replaceValues(connector.getTimeout(), mo)));
        } catch (NumberFormatException e) {
            // Don't set if the value is invalid
        }

        if ("SSL".equalsIgnoreCase(connector.getEncryption())) {
            email.setSSL(true);
        } else if ("TLS".equalsIgnoreCase(connector.getEncryption())) {
            email.setTLS(true);
        }

        if (connector.isAuthentication()) {
            email.setAuthentication(replacer.replaceValues(connector.getUsername(), mo),
                    replacer.replaceValues(connector.getPassword(), mo));
        }

        /*
         * NOTE: There seems to be a bug when calling setTo with a List
         * (throws a java.lang.ArrayStoreException), so we are using addTo
         * instead.
         */

        for (String to : replaceValuesAndSplit(connector.getTo(), mo)) {
            email.addTo(to);
        }

        // Currently unused
        for (String cc : replaceValuesAndSplit(connector.cc(), mo)) {
            email.addCc(cc);
        }

        // Currently unused
        for (String bcc : replaceValuesAndSplit(connector.getBcc(), mo)) {
            email.addBcc(bcc);
        }

        // Currently unused
        for (String replyTo : replaceValuesAndSplit(connector.getReplyTo(), mo)) {
            email.addReplyTo(replyTo);
        }

        for (Entry<String, String> header : connector.getHeaders().entrySet()) {
            email.addHeader(replacer.replaceValues(header.getKey(), mo),
                    replacer.replaceValues(header.getValue(), mo));
        }

        email.setFrom(replacer.replaceValues(connector.getFrom(), mo));
        email.setSubject(replacer.replaceValues(connector.getSubject(), mo));

        String body = replacer.replaceValues(connector.getBody(), mo);

        if (connector.isHtml()) {
            ((HtmlEmail) email).setHtmlMsg(body);
        } else {
            email.setMsg(body);
        }

        /*
         * If the MIME type for the attachment is missing, we display a
         * warning and set the content anyway. If the MIME type is of type
         * "text" or "application/xml", then we add the content. If it is
         * anything else, we assume it should be Base64 decoded first.
         */
        for (Attachment attachment : connector.getAttachments()) {
            String name = replacer.replaceValues(attachment.getName(), mo);
            String mimeType = replacer.replaceValues(attachment.getMimeType(), mo);
            String content = replacer.replaceValues(attachment.getContent(), mo);

            byte[] bytes;

            if (StringUtils.indexOf(mimeType, "/") < 0) {
                logger.warn("valid MIME type is missing for email attachment: \"" + name
                        + "\", using default of text/plain");
                attachment.setMimeType("text/plain");
                bytes = content.getBytes();
            } else if ("application/xml".equalsIgnoreCase(mimeType)
                    || StringUtils.startsWith(mimeType, "text/")) {
                logger.debug("text or XML MIME type detected for attachment \"" + name + "\"");
                bytes = content.getBytes();
            } else {
                logger.debug("binary MIME type detected for attachment \"" + name
                        + "\", performing Base64 decoding");
                bytes = Base64.decodeBase64(content);
            }

            ((MultiPartEmail) email).attach(new ByteArrayDataSource(bytes, mimeType), name, null);
        }

        /*
         * From the Commons Email JavaDoc: send returns
         * "the message id of the underlying MimeMessage".
         */
        String response = email.send();
        messageObjectController.setSuccess(mo, response, null);
    } catch (EmailException e) {
        alertController.sendAlerts(connector.getChannelId(), Constants.ERROR_402,
                "Error sending email message.", e);
        messageObjectController.setError(mo, Constants.ERROR_402, "Error sending email message.", e, null);
        connector.handleException(new Exception(e));
    } finally {
        monitoringController.updateStatus(connector, connectorType, Event.DONE);
    }
}