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:easycar.controller.BookingListController.java

public void emailRemind() {
    Email email = new SimpleEmail();
    email.setHostName("smtp.googlemail.com");
    email.setSmtpPort(465);/* www.  j ava  2s  .co m*/
    email.setAuthenticator(new DefaultAuthenticator("easycarremind", "easycartest"));
    email.setSSLOnConnect(true);
    try {
        email.setFrom("easycarremind@gmail.com");
        email.setSubject("Reminder to return car");
        email.setMsg("Dear " + selectedBooking.getCustomerName()
                + ",\n\nPlease note that you are supposed to return the car with id "
                + selectedBooking.getCarId() + " to us on " + selectedBooking.getEndDateToDisplay()
                + "\n\nBest regards,\nEasy Car Team.");
        email.addTo(selectedBooking.getCustomerEmail());
        email.send();
    } catch (EmailException e) {
        e.printStackTrace();
    }

    selectedBooking.setLastEmailRemindDate(getZeroTimeDate(new Date()));
    try {
        bookingService.editBooking(selectedBooking);
    } catch (IOException e) {
        e.printStackTrace();
    }

    FacesContext.getCurrentInstance().addMessage("messageDetailDialog", new FacesMessage(
            FacesMessage.SEVERITY_INFO, dictionaryController.getDictionary().get("REMINDER_SENT"), null));
}

From source file:easycar.controller.BookingListController.java

public void emailRemindAll() {
    FullBookingDto currentFullBookingDto;
    for (int i = 0; i < baseFullBookingList.size(); i++) {
        currentFullBookingDto = baseFullBookingList.get(i);

        if (!currentFullBookingDto.isRemindButtonOn()) {
            continue;
        }/*w ww  . j  a  v  a2s.  c o  m*/

        Email email = new SimpleEmail();
        email.setHostName("smtp.googlemail.com");
        email.setSmtpPort(465);
        email.setAuthenticator(new DefaultAuthenticator("easycarremind", "easycartest"));
        email.setSSLOnConnect(true);

        try {
            email.setFrom("easycarremind@gmail.com");
            email.setSubject("Reminder to return car");
            email.setMsg("Dear " + currentFullBookingDto.getCustomerName()
                    + ",\n\nPlease note that you are supposed to return the car with id "
                    + currentFullBookingDto.getCarId() + " to us on "
                    + currentFullBookingDto.getEndDateToDisplay() + "\n\nBest regards,\nEasy Car Team.");
            email.addTo(currentFullBookingDto.getCustomerEmail());
            email.send();
        } catch (EmailException e) {
            e.printStackTrace();
        }

        currentFullBookingDto.setLastEmailRemindDate(getZeroTimeDate(new Date()));
        try {
            bookingService.editBooking(currentFullBookingDto);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    FacesContext.getCurrentInstance().addMessage("messageEmailRemindAll", new FacesMessage(
            FacesMessage.SEVERITY_INFO, dictionaryController.getDictionary().get("REMINDER_SENT"), null));
}

From source file:net.wikiadmin.clientnotification.SendMain.java

/** sendMainText. */
void sendMainText(String cMail, String cName, String cCredit) {

    /* i need your data!!! :D */
    XmlProp readData = new XmlProp("mailsettings.xml");
    readData.readData();/*  www  .jav  a2s. c  o m*/

    userS = readData.getUser();
    passS = readData.getpass();
    hostS = readData.gethost();
    portS = readData.getport();
    sslS = readData.getssl();
    fromS = readData.getfrom();
    toS = cMail;
    intPortS = Integer.parseInt(portS);
    sslBoolean = Boolean.parseBoolean(sslS);
    themeText = readData.getTheme();
    bodyText = readData.getBody() + cName + ".\n" + readData.getBodyP2() + cCredit + readData.getBodyP3() + "\n"
            + readData.getBodyContacts();

    try {
        Email email = new SimpleEmail();
        email.setHostName(hostS);
        email.setAuthenticator(new DefaultAuthenticator(userS, passS));
        email.setSSLOnConnect(sslBoolean);
        email.setSmtpPort(intPortS);
        email.setFrom(fromS);
        email.setSubject(themeText);
        email.setMsg(bodyText);
        email.addTo(toS);
        email.send();
    } catch (EmailException ex) {
        ex.printStackTrace();
        System.out.println("");
    }
}

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

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

    boolean isMailServerSet = false;
    if (tenantId != null && tenantId.length() > 0) {
        if (processEngineConfiguration.getMailSessionJndi(tenantId) != null) {
            setEmailSession(email, processEngineConfiguration.getMailSessionJndi(tenantId));
            isMailServerSet = true;// w  w w  .  ja  v  a2s  . c o  m

        } else if (processEngineConfiguration.getMailServer(tenantId) != null) {
            MailServerInfo mailServerInfo = processEngineConfiguration.getMailServer(tenantId);
            String host = mailServerInfo.getMailServerHost();
            if (host == null) {
                throw new ActivitiException(
                        "Could not send email: no SMTP host is configured for tenantId " + tenantId);
            }
            email.setHostName(host);

            email.setSmtpPort(mailServerInfo.getMailServerPort());

            email.setSSLOnConnect(processEngineConfiguration.getMailServerUseSSL());
            email.setStartTLSEnabled(processEngineConfiguration.getMailServerUseTLS());

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

            isMailServerSet = true;
        }
    }

    if (!isMailServerSet) {
        String mailSessionJndi = processEngineConfiguration.getMailSessionJndi();
        if (mailSessionJndi != null) {
            setEmailSession(email, mailSessionJndi);

        } 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.setSSLOnConnect(processEngineConfiguration.getMailServerUseSSL());
            email.setStartTLSEnabled(processEngineConfiguration.getMailServerUseTLS());

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

From source file:org.apache.airavata.credential.store.notifier.impl.EmailNotifier.java

public void notifyMessage(NotificationMessage message) throws CredentialStoreException {
    try {/* w  ww  . j ava2 s.  c o m*/
        Email email = new SimpleEmail();
        email.setHostName(this.emailNotifierConfiguration.getEmailServer());
        email.setSmtpPort(this.emailNotifierConfiguration.getEmailServerPort());
        email.setAuthenticator(new DefaultAuthenticator(this.emailNotifierConfiguration.getEmailUserName(),
                this.emailNotifierConfiguration.getEmailPassword()));
        email.setSSLOnConnect(this.emailNotifierConfiguration.isSslConnect());
        email.setFrom(this.emailNotifierConfiguration.getFromAddress());

        EmailNotificationMessage emailMessage = (EmailNotificationMessage) message;

        email.setSubject(emailMessage.getSubject());
        email.setMsg(emailMessage.getMessage());
        email.addTo(emailMessage.getSenderEmail());
        email.send();

    } catch (EmailException e) {
        log.error("[CredentialStore]Error sending email notification message.");
        throw new CredentialStoreException("Error sending email notification message", e);
    }

}

From source file:org.dllearner.algorithms.qtl.experiments.PRConvergenceExperiment.java

private void sendFinishedMail() throws EmailException, IOException {
    Properties config = new Properties();
    config.load(Thread.currentThread().getContextClassLoader()
            .getResourceAsStream("org/dllearner/algorithms/qtl/qtl-mail.properties"));

    Email email = new SimpleEmail();
    email.setHostName(config.getProperty("hostname"));
    email.setSmtpPort(465);//from  w ww .  ja  v a2s .c o m
    email.setAuthenticator(
            new DefaultAuthenticator(config.getProperty("username"), config.getProperty("password")));
    email.setSSLOnConnect(true);
    email.setFrom(config.getProperty("from"));
    email.setSubject("QTL evaluation finished.");
    email.setMsg("QTL evaluation finished.");
    email.addTo(config.getProperty("to"));
    email.send();
}

From source file:org.fao.geonet.util.MailUtil.java

/**
 * Create data information to compose the mail
 *
 * @param hostName/*w w  w .j a v a  2  s  .com*/
 * @param smtpPort
 * @param from
 * @param username
 * @param password
 * @param email
 * @param ssl
 * @param tls
 * @param ignoreSslCertificateErrors
 */
private static void configureBasics(String hostName, Integer smtpPort, String from, String username,
        String password, Email email, Boolean ssl, Boolean tls, Boolean ignoreSslCertificateErrors) {
    if (hostName != null) {
        email.setHostName(hostName);
    } else {
        throw new IllegalArgumentException(
                "Missing settings in System Configuration (see Administration menu) - cannot send mail");
    }
    if (StringUtils.isNotBlank(smtpPort + "")) {
        email.setSmtpPort(smtpPort);
    } else {
        throw new IllegalArgumentException(
                "Missing settings in System Configuration (see Administration menu) - cannot send mail");
    }
    if (username != null) {
        email.setAuthenticator(new DefaultAuthenticator(username, password));
    }

    email.setDebug(true);

    if (tls != null && tls) {
        email.setStartTLSEnabled(tls);
        email.setStartTLSRequired(tls);
    }

    if (ssl != null && ssl) {
        email.setSSLOnConnect(ssl);
        if (StringUtils.isNotBlank(smtpPort + "")) {
            email.setSslSmtpPort(smtpPort + "");
        }
    }

    if (ignoreSslCertificateErrors != null && ignoreSslCertificateErrors) {
        try {
            Session mailSession = email.getMailSession();
            Properties p = mailSession.getProperties();
            p.setProperty("mail.smtp.ssl.trust", "*");

        } catch (EmailException e) {
            // Ignore the exception. Can't be reached because the host name is always set above or an
            // IllegalArgumentException is thrown.
        }
    }

    if (StringUtils.isNotBlank(from)) {
        try {
            email.setFrom(from);
        } catch (EmailException e) {
            throw new IllegalArgumentException(
                    "Invalid 'from' email setting in System Configuration (see Administration menu) - cannot send "
                            + "mail",
                    e);
        }
    } else {
        throw new IllegalArgumentException(
                "Missing settings in System Configuration (see Administration menu) - cannot send mail");
    }
}

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

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

    boolean isMailServerSet = false;
    if (tenantId != null && tenantId.length() > 0) {
        if (processEngineConfiguration.getMailSessionJndi(tenantId) != null) {
            setEmailSession(email, processEngineConfiguration.getMailSessionJndi(tenantId));
            isMailServerSet = true;//from w w w .j  a  v a  2  s .c  o  m

        } else if (processEngineConfiguration.getMailServer(tenantId) != null) {
            MailServerInfo mailServerInfo = processEngineConfiguration.getMailServer(tenantId);
            String host = mailServerInfo.getMailServerHost();
            if (host == null) {
                throw new FlowableException(
                        "Could not send email: no SMTP host is configured for tenantId " + tenantId);
            }
            email.setHostName(host);

            email.setSmtpPort(mailServerInfo.getMailServerPort());

            email.setSSLOnConnect(mailServerInfo.isMailServerUseSSL());
            email.setStartTLSEnabled(mailServerInfo.isMailServerUseTLS());

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

            isMailServerSet = true;
        }
    }

    if (!isMailServerSet) {
        String mailSessionJndi = processEngineConfiguration.getMailSessionJndi();
        if (mailSessionJndi != null) {
            setEmailSession(email, mailSessionJndi);

        } else {
            String host = processEngineConfiguration.getMailServerHost();
            if (host == null) {
                throw new FlowableException("Could not send email: no SMTP host is configured");
            }
            email.setHostName(host);

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

            email.setSSLOnConnect(processEngineConfiguration.getMailServerUseSSL());
            email.setStartTLSEnabled(processEngineConfiguration.getMailServerUseTLS());

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

From source file:org.graylog2.alerts.FormattedEmailAlertSender.java

private void sendEmail(String emailAddress, Stream stream, AlertCondition.CheckResult checkResult,
        List<Message> backlog) throws TransportConfigurationException, EmailException {
    LOG.debug("Sending mail to " + emailAddress);
    if (!configuration.isEnabled()) {
        throw new TransportConfigurationException(
                "Email transport is not enabled in server configuration file!");
    }/*ww  w.  j ava2  s.  c  o  m*/

    final Email email = new SimpleEmail();
    email.setCharset(EmailConstants.UTF_8);

    if (isNullOrEmpty(configuration.getHostname())) {
        throw new TransportConfigurationException(
                "No hostname configured for email transport while trying to send alert email!");
    } else {
        email.setHostName(configuration.getHostname());
    }
    email.setSmtpPort(configuration.getPort());
    if (configuration.isUseSsl()) {
        email.setSslSmtpPort(Integer.toString(configuration.getPort()));
    }

    if (configuration.isUseAuth()) {
        email.setAuthenticator(new DefaultAuthenticator(Strings.nullToEmpty(configuration.getUsername()),
                Strings.nullToEmpty(configuration.getPassword())));
    }

    email.setSSLOnConnect(configuration.isUseSsl());
    email.setStartTLSEnabled(configuration.isUseTls());
    if (pluginConfig != null && !isNullOrEmpty(pluginConfig.getString("sender"))) {
        email.setFrom(pluginConfig.getString("sender"));
    } else {
        email.setFrom(configuration.getFromEmail());
    }
    email.setSubject(buildSubject(stream, checkResult, backlog));
    email.setMsg(buildBody(stream, checkResult, backlog));
    email.addTo(emailAddress);

    email.send();
}

From source file:org.graylog2.alerts.StaticEmailAlertSender.java

private void sendEmail(String emailAddress, Stream stream, AlertCondition.CheckResult checkResult,
        List<Message> backlog) throws TransportConfigurationException, EmailException {
    LOG.debug("Sending mail to " + emailAddress);
    if (!configuration.isEnabled()) {
        throw new TransportConfigurationException(
                "Email transport is not enabled in server configuration file!");
    }//  w  w w.  j a  va 2s  . c  om

    final Email email = new SimpleEmail();
    email.setCharset(EmailConstants.UTF_8);

    if (Strings.isNullOrEmpty(configuration.getHostname())) {
        throw new TransportConfigurationException(
                "No hostname configured for email transport while trying to send alert email!");
    } else {
        email.setHostName(configuration.getHostname());
    }
    email.setSmtpPort(configuration.getPort());
    if (configuration.isUseSsl()) {
        email.setSslSmtpPort(Integer.toString(configuration.getPort()));
    }

    if (configuration.isUseAuth()) {
        email.setAuthenticator(new DefaultAuthenticator(Strings.nullToEmpty(configuration.getUsername()),
                Strings.nullToEmpty(configuration.getPassword())));
    }

    email.setSSLOnConnect(configuration.isUseSsl());
    email.setStartTLSEnabled(configuration.isUseTls());
    if (pluginConfig != null && !Strings.isNullOrEmpty(pluginConfig.getString("sender"))) {
        email.setFrom(pluginConfig.getString("sender"));
    } else {
        email.setFrom(configuration.getFromEmail());
    }
    email.setSubject(buildSubject(stream, checkResult, backlog));
    email.setMsg(buildBody(stream, checkResult, backlog));
    email.addTo(emailAddress);

    email.send();
}