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:org.activiti.engine.impl.bpmn.behavior.MailActivityBehavior.java

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

    String mailSessionJndi = processEngineConfiguration.getMailSesionJndi();
    if (mailSessionJndi != null) {
        try {/* www  .ja va 2s  . c o m*/
            email.setMailSessionFromJNDI(mailSessionJndi);
        } catch (NamingException e) {
            throw new ActivitiException("Could not send email: Incorrect JNDI configuration", e);
        }
    } else {
        String host = processEngineConfiguration.getMailServerHost();
        if (host == null) {
            throw new ActivitiException("Could not send email: no SMTP host is configured");
        }
        email.setHostName(host);

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

        email.setSSL(processEngineConfiguration.getMailServerUseSSL());
        email.setTLS(processEngineConfiguration.getMailServerUseTLS());

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

From source file:org.camunda.bpm.engine.impl.bpmn.behavior.MailActivityBehavior.java

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

    String host = processEngineConfiguration.getMailServerHost();
    if (host == null) {
        throw new ProcessEngineException("Could not send email: no SMTP host is configured");
    }/* w ww.j a  v a  2  s  .  c  o m*/
    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:org.camunda.bpm.example.coffee.service.SendMailTask.java

private void sendMail(String to) throws EmailException {
    String host = mailProperties.getProperty("host");
    Integer port = Integer.valueOf(mailProperties.getProperty("port"));
    String user = mailProperties.getProperty("user");
    String password = mailProperties.getProperty("password");

    String from = mailProperties.getProperty("from");

    Email email = new SimpleEmail();
    email.setHostName(host);/*from   ww  w  .  j  a  v  a2s . c  o  m*/
    email.setTLS(true);
    email.setSmtpPort(port);
    email.setAuthentication(user, password);

    email.setFrom(from);
    email.setSubject("Your Coffee");
    email.setMsg("...is ready to drink.");
    email.addTo(to);

    getMailProvider().send(email);
}

From source file:org.chenillekit.mail.services.impl.MailServiceImpl.java

private void setEmailStandardData(Email email) {
    email.setHostName(smtpServer);//  w  ww  . ja  v  a  2 s .  c  om

    if (smtpUser != null && smtpUser.length() > 0)
        email.setAuthentication(smtpUser, smtpPassword);

    email.setDebug(smtpDebug);
    email.setSmtpPort(smtpPort);
    email.setSSL(smtpSSL);
    email.setSslSmtpPort(String.valueOf(smtpSslPort));
    email.setTLS(smtpTLS);
}

From source file:org.cobbzilla.mail.sender.SmtpMailSender.java

@Override
public void send(SimpleEmailMessage message) throws EmailException {

    Email email = constructEmail(message);
    email.setHostName(config.getHost());
    email.setSmtpPort(config.getPort());
    if (config.getHasMailUser()) {
        email.setAuthenticator(new DefaultAuthenticator(config.getUser(), config.getPassword()));
    }/* w  ww. j a  v a  2 s.  co  m*/
    email.setTLS(config.isTlsEnabled());
    email.setSubject(message.getSubject());
    if (message.getToName() != null) {
        email.addTo(message.getToEmail(), message.getToName());
    } else {
        email.addTo(message.getToEmail());
    }
    if (message.getBcc() != null) {
        email.addBcc(message.getBcc());
    }
    if (message.getCc() != null) {
        email.addCc(message.getCc());
    }
    if (message.getFromName() != null) {
        email.setFrom(message.getFromEmail(), message.getFromName());
    } else {
        email.setFrom(message.getFromEmail());
    }

    sendEmail_internal(email);
}

From source file:org.ms123.common.workflow.tasks.TaskMailExecutor.java

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

    String host = processEngineConfiguration.getMailServerHost();
    if (host == null) {
        throw new RuntimeException("TaskMailExecutor:Could not send email: no SMTP host is configured");
    }/*from   w  ww.  j  a v a 2s  . c  o m*/
    email.setHostName(host);

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

    email.setSSL(processEngineConfiguration.getMailServerUseSSL());
    email.setTLS(processEngineConfiguration.getMailServerUseTLS());

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

From source file:org.ms123.common.workflow.TaskSendExecutor.java

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

    String host = processEngineConfiguration.getMailServerHost();
    if (host == null) {
        throw new RuntimeException("TaskSendExecutor:Could not send email: no SMTP host is configured");
    }/*  w  ww .j  a  v  a  2 s  . co  m*/
    email.setHostName(host);

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

    email.setSSL(processEngineConfiguration.getMailServerUseSSL());
    email.setTLS(processEngineConfiguration.getMailServerUseTLS());

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

From source file:org.openepics.discs.calib.ejb.ReminderSvc.java

private void sendMail(String from, String[] to, String subject, String message) {
    try {//from w w  w  . j  a v  a  2s  .c  om
        Email email = new SimpleEmail();

        logger.info("sending email ...");
        email.setHostName("exchange.nscl.msu.edu");
        email.setSmtpPort(25);
        // email.setSmtpPort(465);
        // email.setAuthenticator(new DefaultAuthenticator("iser", "xxxxxx"));
        // email.setSSLOnConnect(true);
        email.setTLS(true);
        // email.setDebug(true);
        email.setFrom(from);
        email.setSubject(subject);
        email.setMsg(message);
        email.addTo(to);
        email.send();
    } catch (Exception e) {
        logger.severe("error while sending email");
    }
}

From source file:org.openhab.action.mail.internal.Mail.java

/**
 * Sends an email with attachment(s) via SMTP
 * //w  ww .  j ava 2 s .c  o  m
 * @param to the email address of the recipient
 * @param subject the subject of the email
 * @param message the body of the email
 * @param attachmentUrlList a list of URL strings of the contents to send as attachments
 * 
 * @return <code>true</code>, if sending the email has been successful and 
 * <code>false</code> in all other cases.
 */
@ActionDoc(text = "Sends an email with attachment via SMTP")
static public boolean sendMail(@ParamDoc(name = "to") String to, @ParamDoc(name = "subject") String subject,
        @ParamDoc(name = "message") String message,
        @ParamDoc(name = "attachmentUrlList") List<String> attachmentUrlList) {
    boolean success = false;
    if (MailActionService.isProperlyConfigured) {
        Email email = new SimpleEmail();
        if (attachmentUrlList != null && !attachmentUrlList.isEmpty()) {
            email = new MultiPartEmail();
            for (String attachmentUrl : attachmentUrlList) {
                // Create the attachment
                try {
                    EmailAttachment attachment = new EmailAttachment();
                    attachment.setURL(new URL(attachmentUrl));
                    attachment.setDisposition(EmailAttachment.ATTACHMENT);
                    String fileName = attachmentUrl.replaceFirst(".*/([^/?]+).*", "$1");
                    attachment.setName(StringUtils.isNotBlank(fileName) ? fileName : "Attachment");
                    ((MultiPartEmail) email).attach(attachment);
                } catch (MalformedURLException e) {
                    logger.error("Invalid attachment url.", e);
                } catch (EmailException e) {
                    logger.error("Error adding attachment to email.", e);
                }
            }
        }

        email.setHostName(hostname);
        email.setSmtpPort(port);
        email.setTLS(tls);

        if (StringUtils.isNotBlank(username)) {
            if (popBeforeSmtp) {
                email.setPopBeforeSmtp(true, hostname, username, password);
            } else {
                email.setAuthenticator(new DefaultAuthenticator(username, password));
            }
        }

        try {
            if (StringUtils.isNotBlank(charset)) {
                email.setCharset(charset);
            }
            email.setFrom(from);
            String[] toList = to.split(";");
            for (String toAddress : toList) {
                email.addTo(toAddress);
            }
            if (!StringUtils.isEmpty(subject))
                email.setSubject(subject);
            if (!StringUtils.isEmpty(message))
                email.setMsg(message);
            email.send();
            logger.debug("Sent email to '{}' with subject '{}'.", to, subject);
            success = true;
        } catch (EmailException e) {
            logger.error("Could not send e-mail to '" + to + "'.", e);
        }
    } else {
        logger.error(
                "Cannot send e-mail because of missing configuration settings. The current settings are: "
                        + "Host: '{}', port '{}', from '{}', useTLS: {}, username: '{}', password '{}'",
                new Object[] { hostname, String.valueOf(port), from, String.valueOf(tls), username, password });
    }

    return success;
}

From source file:org.openhab.io.net.actions.Mail.java

/**
 * Sends an email with attachment via SMTP
 * /*from www .  j av  a 2s  .c om*/
 * @param to the email address of the recipient
 * @param subject the subject of the email
 * @param message the body of the email
 * @param attachmentUrl a URL string of the content to send as an attachment
 * 
 * @return <code>true</code>, if sending the email has been successful and 
 * <code>false</code> in all other cases.
 */
static public boolean sendMail(String to, String subject, String message, String attachmentUrl) {
    boolean success = false;
    if (initialized) {
        Email email = new SimpleEmail();
        if (attachmentUrl != null) {
            // Create the attachment
            try {
                email = new MultiPartEmail();
                EmailAttachment attachment = new EmailAttachment();
                attachment.setURL(new URL(attachmentUrl));
                attachment.setDisposition(EmailAttachment.ATTACHMENT);
                attachment.setName("Attachment");
                ((MultiPartEmail) email).attach(attachment);
            } catch (MalformedURLException e) {
                logger.error("Invalid attachment url.", e);
            } catch (EmailException e) {
                logger.error("Error adding attachment to email.", e);
            }
        }

        email.setHostName(hostname);
        email.setSmtpPort(port);
        email.setTLS(tls);

        if (StringUtils.isNotBlank(username)) {
            if (popBeforeSmtp) {
                email.setPopBeforeSmtp(true, hostname, username, password);
            } else {
                email.setAuthenticator(new DefaultAuthenticator(username, password));
            }
        }

        try {
            email.setFrom(from);
            email.addTo(to);
            if (!StringUtils.isEmpty(subject))
                email.setSubject(subject);
            if (!StringUtils.isEmpty(message))
                email.setMsg(message);
            email.send();
            logger.debug("Sent email to '{}' with subject '{}'.", to, subject);
            success = true;
        } catch (EmailException e) {
            logger.error("Could not send e-mail to '" + to + ".", e);
        }
    } else {
        logger.error(
                "Cannot send e-mail because of missing configuration settings. The current settings are: "
                        + "Host: '{}', port '{}', from '{}', useTLS: {}, username: '{}', password '{}'",
                new String[] { hostname, String.valueOf(port), from, String.valueOf(tls), username, password });
    }

    return success;
}