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:com.esofthead.mycollab.servlet.EmailValidationServlet.java

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String smtpUserName = request.getParameter("smtpUserName");
    String smtpPassword = request.getParameter("smtpPassword");
    String smtpHost = request.getParameter("smtpHost");
    String smtpPort = request.getParameter("smtpPort");
    String tls = request.getParameter("tls");

    int mailServerPort;
    try {/*www.j  a v a  2  s.co  m*/
        mailServerPort = Integer.parseInt(smtpPort);
    } catch (Exception e) {
        PrintWriter out = response.getWriter();
        out.write("Port must be an integer value");
        return;
    }
    try {
        Email email = new SimpleEmail();
        email.setHostName(smtpHost);
        email.setSmtpPort(mailServerPort);
        email.setAuthenticator(new DefaultAuthenticator(smtpUserName, smtpPassword));
        if (tls.equals("true")) {
            email.setSSLOnConnect(true);
        } else {
            email.setSSLOnConnect(false);
        }
        email.setFrom(smtpUserName);
        email.setSubject("MyCollab Test Email");
        email.setMsg("This is a test mail ... :-)");
        email.addTo(smtpUserName);
        email.send();
    } catch (EmailException e) {
        PrintWriter out = response.getWriter();
        out.write("Cannot establish SMTP connection. Please recheck your config.");
        return;
    }
}

From source file:ch.fihlon.moodini.business.token.control.TokenService.java

@SneakyThrows
private void sendChallenge(@NotNull final String email, @NotNull final Challenge challenge) {
    final SmtpConfiguration smtp = configuration.getSmtp();
    final Email mail = new SimpleEmail();
    mail.setHostName(smtp.getHostname());
    mail.setSmtpPort(smtp.getPort());/* w w  w  .  jav a  2s .c o m*/
    mail.setAuthenticator(new DefaultAuthenticator(smtp.getUser(), smtp.getPassword()));
    mail.setSSLOnConnect(smtp.getSsl());
    mail.setFrom(smtp.getFrom());
    mail.setSubject("Your challenge to login to Moodini");
    mail.setMsg(String.format("Your one time challenge, valid for 10 minutes: %s", challenge.getChallenge()));
    mail.addTo(email);
    mail.send();
}

From source file:com.github.frapontillo.pulse.email.EmailNotifier.java

/**
 * Send an email, using the provided parameters, notifying if the pipeline succeeded or errored.
 *
 * @param parameters The {@link EmailNotifierConfig} to use.
 * @param isSuccess  {@code true} to report a success, {@code false} to report an error.
 */// w  w w  . j  a  va 2 s  . c om
private void sendEmail(EmailNotifierConfig parameters, boolean isSuccess) {
    if (parameters.getAddresses() == null || parameters.getAddresses().length == 0) {
        return;
    }
    try {
        Email email = new SimpleEmail();
        email.setHostName(parameters.getHost());
        email.setSmtpPort(parameters.getPort());
        email.setAuthenticator(new DefaultAuthenticator(parameters.getUsername(), parameters.getPassword()));
        email.setSSLOnConnect(parameters.getUseSsl());
        email.setFrom(parameters.getFrom());
        email.setSubject(parameters.getSubject());
        String body;
        if (isSuccess) {
            body = parameters.getBodySuccess();
        } else {
            body = parameters.getBodyError();
        }
        body = body.replace("{{NAME}}", getProcessInfo().getName());
        email.setMsg(body);
        email.addTo(parameters.getAddresses());
        email.send();
    } catch (EmailException e) {
        logger.error(e);
        e.printStackTrace();
    }
}

From source file:at.treedb.util.Mail.java

/**
 * /*from   ww  w  . j a v a2 s .com*/
 * @param sendTo
 * @param subject
 * @param message
 * @throws EmailException
 */
public void sendMail(String mailTo, String mailFrom, String subject, String message) throws Exception {
    Objects.requireNonNull(mailTo, "Mail.sendMail(): mailTo can not be null!");
    Objects.requireNonNull(mailFrom, "Mail.sendMail(): mailFrom can not be null!");
    Objects.requireNonNull(subject, "Mail.sendMail(): subject can not be null!");
    Objects.requireNonNull(message, "Mail.sendMail(): message can not be null!");
    Email email = new SimpleEmail();
    email.setHostName(smtpHost);

    email.setAuthenticator(new DefaultAuthenticator(smtpUser, smtpPassword));
    if (transportSecurity == TransportSecurity.SSL) {
        email.setSSLOnConnect(true);
        email.setSSLCheckServerIdentity(false);
    } else if (transportSecurity == TransportSecurity.STARTTLS) {
        email.setStartTLSRequired(true);
    }
    email.setSmtpPort(smtpPort);
    email.setFrom(mailFrom);
    email.setSubject(subject);
    email.setMsg(message);
    email.addTo(mailTo);
    email.send();
}

From source file:com.pax.pay.trans.receipt.paperless.AReceiptEmail.java

private int setBaseInfo(EmailInfo emailInfo, Email email) {
    try {/*w  ww  .j av a  2 s  .c  o  m*/
        //email.setDebug(true);
        email.setHostName(emailInfo.getHostName());
        email.setSmtpPort(emailInfo.getPort());
        email.setAuthentication(emailInfo.getUserName(), emailInfo.getPassword());
        email.setCharset("UTF-8");
        email.setSSLOnConnect(emailInfo.isSsl());
        if (emailInfo.isSsl())
            email.setSslSmtpPort(String.valueOf(emailInfo.getSslPort()));
        email.setFrom(emailInfo.getFrom());
    } catch (EmailException e) {
        e.printStackTrace();
        return -1;
    }
    return 0;
}

From source file:ch.sdi.core.impl.mail.MailSenderDefault.java

/**
 * @see ch.sdi.core.intf.MailSender#sendMail(java.lang.Object)
 *//*ww w. ja v  a  2 s .  c o  m*/
@Override
public void sendMail(Email aMail) throws SdiException {
    aMail.setHostName(myHost);
    if (mySslOnConnect) {
        aMail.setSslSmtpPort("" + myPort);
    } else {
        aMail.setSmtpPort(myPort);
    } // if..else mySslOnConnect

    aMail.setAuthenticator(myAuthenticator);
    aMail.setSSLOnConnect(mySslOnConnect);
    aMail.setStartTLSRequired(myStartTlsRequired);

    try {
        aMail.setFrom(mySenderAddress);
    } catch (EmailException t) {
        throw new SdiException("Problems setting the sender address to mail: " + mySenderAddress, t,
                SdiException.EXIT_CODE_MAIL_ERROR);
    }

    if (myDryRun) {
        myLog.debug("DryRun is set. Not sending the mail");
        // TODO: save locally in output dir
    } else {
        try {
            aMail.send();
            myLog.debug("mail successfully sent");
        } catch (Throwable t) {
            throw new SdiException("Problems sending a mail", t, SdiException.EXIT_CODE_MAIL_ERROR);
        }
    } // if..else myDryRun

}

From source file:in.flipbrain.controllers.BaseController.java

protected void sendEmail(String to, String subject, String body) throws EmailException {
    logger.debug("Sending email to " + to + "\nSubject: " + subject + "\nMessage: " + body);
    if ("true".equalsIgnoreCase(getConfigValue(Constants.EM_FAKE_SEND)))
        return;//  ww  w  .  ja v  a2 s. c o  m

    Email email = new SimpleEmail();
    email.setHostName(getConfigValue("smtp.host"));
    email.setSmtpPort(Integer.parseUnsignedInt(getConfigValue("smtp.port")));
    email.setAuthenticator(
            new DefaultAuthenticator(getConfigValue("smtp.user"), getConfigValue("smtp.password")));
    email.setSSLOnConnect(Boolean.parseBoolean(getConfigValue("smtp.ssl")));
    email.setFrom(getConfigValue("smtp.sender"));
    email.setSubject(subject);
    email.setMsg(body);
    email.addTo(to);
    email.send();
}

From source file:com.cws.esolutions.security.quartz.PasswordExpirationNotifier.java

/**
 * @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
 */// ww  w  .  j  a v  a2  s.c  o m
public void execute(final JobExecutionContext context) {
    final String methodName = PasswordExpirationNotifier.CNAME
            + "#execute(final JobExecutionContext jobContext)";

    if (DEBUG) {
        DEBUGGER.debug(methodName);
        DEBUGGER.debug("JobExecutionContext: {}", context);
    }

    final Map<String, Object> jobData = context.getJobDetail().getJobDataMap();

    if (DEBUG) {
        DEBUGGER.debug("jobData: {}", jobData);
    }

    try {
        UserManager manager = UserManagerFactory
                .getUserManager(bean.getConfigData().getSecurityConfig().getUserManager());

        if (DEBUG) {
            DEBUGGER.debug("UserManager: {}", manager);
        }

        List<String[]> accounts = manager.listUserAccounts();

        if (DEBUG) {
            DEBUGGER.debug("accounts: {}", accounts);
        }

        if ((accounts == null) || (accounts.size() == 0)) {
            return;
        }

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, 30);
        Long expiryTime = cal.getTimeInMillis();

        if (DEBUG) {
            DEBUGGER.debug("Calendar: {}", cal);
            DEBUGGER.debug("expiryTime: {}", expiryTime);
        }

        for (String[] account : accounts) {
            if (DEBUG) {
                DEBUGGER.debug("Account: {}", (Object) account);
            }

            List<Object> accountDetail = manager.loadUserAccount(account[0]);

            if (DEBUG) {
                DEBUGGER.debug("List<Object>: {}", accountDetail);
            }

            try {
                Email email = new SimpleEmail();
                email.setHostName((String) jobData.get("mailHost"));
                email.setSmtpPort(Integer.parseInt((String) jobData.get("portNumber")));

                if ((Boolean) jobData.get("isSecure")) {
                    email.setSSLOnConnect(true);
                }

                if ((Boolean) jobData.get("isAuthenticated")) {
                    email.setAuthenticator(new DefaultAuthenticator((String) jobData.get("username"),
                            PasswordUtils.decryptText((String) (String) jobData.get("password"),
                                    (String) jobData.get("salt"), secConfig.getSecretAlgorithm(),
                                    secConfig.getIterations(), secConfig.getKeyBits(),
                                    secConfig.getEncryptionAlgorithm(), secConfig.getEncryptionInstance(),
                                    systemConfig.getEncoding())));
                }

                email.setFrom((String) jobData.get("emailAddr"));
                email.addTo((String) accountDetail.get(6));
                email.setSubject((String) jobData.get("messageSubject"));
                email.setMsg(String.format((String) jobData.get("messageBody"), (String) accountDetail.get(4)));

                if (DEBUG) {
                    DEBUGGER.debug("SimpleEmail: {}", email);
                }

                email.send();
            } catch (EmailException ex) {
                ERROR_RECORDER.error(ex.getMessage(), ex);
            } catch (SecurityException sx) {
                ERROR_RECORDER.error(sx.getMessage(), sx);
            }
        }
    } catch (UserManagementException umx) {
        ERROR_RECORDER.error(umx.getMessage(), umx);
    }
}

From source file:net.scran24.user.server.services.HelpServiceImpl.java

private void sendEmailNotification(String name, String surveyId, String number, List<String> addresses) {
    Email email = new SimpleEmail();

    email.setHostName(smtpHostName);//from   w  w  w.  j  a  va 2s  . com
    email.setSmtpPort(smtpPort);
    email.setAuthenticator(new DefaultAuthenticator(smtpUserName, smtpPassword));
    email.setSSLOnConnect(true);
    email.setCharset(EmailConstants.UTF_8);

    try {
        email.setFrom(fromEmail, fromName);
        email.setSubject("Someone needs help completing their survey");
        email.setMsg("Please call " + name + " on " + number + " (survey id: " + surveyId + ")");

        for (String address : addresses)
            email.addTo(address);

        email.send();
    } catch (EmailException e) {
        log.error("Failed to send e-mail notification", e);
    }
}

From source file:com.mycollab.servlet.EmailValidationServlet.java

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String smtpUserName = request.getParameter("smtpUserName");
    String smtpPassword = request.getParameter("smtpPassword");
    String smtpHost = request.getParameter("smtpHost");
    String smtpPort = request.getParameter("smtpPort");
    String tls = request.getParameter("tls");
    String ssl = request.getParameter("ssl");

    int mailServerPort = 25;
    try {/*from  w w  w .  ja  v  a 2s .c om*/
        mailServerPort = Integer.parseInt(smtpPort);
    } catch (Exception e) {
        LOG.info("The smtp port value is not a number. We will use default port value is 25");
    }
    try {
        Email email = new SimpleEmail();
        email.setHostName(smtpHost);
        email.setSmtpPort(mailServerPort);
        email.setAuthenticator(new DefaultAuthenticator(smtpUserName, smtpPassword));
        if ("true".equals(tls)) {
            email.setStartTLSEnabled(true);
        } else {
            email.setStartTLSEnabled(false);
        }

        if ("true".equals(ssl)) {
            email.setSSLOnConnect(true);
        } else {
            email.setSSLOnConnect(false);
        }
        email.setFrom(smtpUserName);
        email.setSubject("MyCollab Test Email");
        email.setMsg("This is a test mail ... :-)");
        email.addTo(smtpUserName);
        email.send();
    } catch (EmailException e) {
        PrintWriter out = response.getWriter();
        out.write("Cannot establish SMTP connection. Please recheck your config.");
        LOG.warn("Can not login to SMTP", e);
    }
}