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

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

Introduction

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

Prototype

public void setSmtpPort(final int aPortNumber) 

Source Link

Document

Set the port number of the outgoing mail server.

Usage

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);
    email.setAuthenticator(//from ww w .  j  a va 2  s  .  c  o m
            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//from  www .jav a  2s  . c o  m
 * @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 ww .j a  v  a2 s .  co 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!");
    }/*from  w  w w.  jav  a  2s. c om*/

    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!");
    }/*from  w ww .  j av a 2  s . co  m*/

    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();
}

From source file:org.jwebsocket.plugins.mail.MailPlugIn.java

private void sendMail(WebSocketConnector aConnector, Token aToken) {
    TokenServer lServer = getServer();/*w w w.j  a v a 2s.  c o  m*/

    String lFrom = aToken.getString("from", "[unknown]");
    String lTo = aToken.getString("to");
    String lCC = aToken.getString("cc");
    String lBCC = aToken.getString("bcc");
    String lSubject = aToken.getString("subject");
    String lBody = aToken.getString("body");
    Boolean lIsHTML = aToken.getBoolean("html", false);

    // instantiate response token
    Token lResponse = lServer.createResponse(aToken);

    Map lMap = new FastMap();

    if (lFrom != null && lFrom.length() > 0) {
        lMap.put("from", lFrom);
    }
    if (lTo != null && lTo.length() > 0) {
        lMap.put("to", lTo);
    }
    if (lCC != null && lCC.length() > 0) {
        lMap.put("cc", lCC);
    }
    if (lBCC != null && lBCC.length() > 0) {
        lMap.put("bcc", lBCC);
    }
    if (lSubject != null && lSubject.length() > 0) {
        lMap.put("subject", lSubject);
    }
    if (lBody != null && lBody.length() > 0) {
        lMap.put("body", lBody);
    }

    // Create the attachment
    List<EmailAttachment> lAttachments = new FastList<EmailAttachment>();
    /*
    if( aAttachments != null  ) {
    for( int lIdx = 0; lIdx < aAttachments.length; lIdx++  ) {
    EmailAttachment lAttachment = new EmailAttachment();
    lAttachment.setPath( aAttachments[ lIdx ] );
    lAttachment.setDisposition( EmailAttachment.ATTACHMENT );
    // lAttachment.setDescription( "Picture of John" );
    // lAttachment.setName( "John" );
    lAttachments.add( lAttachment );
    }
    }
     */
    // Create the lEmail message
    if (mLog.isDebugEnabled()) {
        mLog.debug("Sending e-mail to " + lTo + " with subject '" + lSubject + "'...");
    }
    try {
        Email lEmail;
        if (lIsHTML) {
            lEmail = new HtmlEmail();
        } else {
            lEmail = new MultiPartEmail();
        }

        lEmail.setHostName(SMTP_HOST);
        lEmail.setSmtpPort(SMTP_PORT);
        if (SMTP_AUTH) {
            lEmail.setAuthentication(SMTP_USER, SMTP_PASSWORD);
        }
        if (SMTP_POP3BEFORE) {
            lEmail.setPopBeforeSmtp(true, POP3_HOST, POP3_USER, POP3_PASSWORD);
        }
        if (lFrom != null && lFrom.length() > 0) {
            lEmail.setFrom(lFrom);
        }
        if (lTo != null && lTo.length() > 0) {
            lEmail.addTo(lTo);
        }
        if (lSubject != null && lSubject.length() > 0) {
            lEmail.setSubject(lSubject);
        }

        if (lBody != null && lBody.length() > 0) {
            if (lIsHTML) {
                HtmlEmail lHTML = ((HtmlEmail) lEmail);
                /*
                URL lURL = new URL("http://five-feet-further.com/aschulze/images/portrait_web_kleiner.jpg");
                String lCID = ((HtmlEmail )lEmail).embed(lURL, "five feet further logo");
                        
                //url = new URL( "http://five-feet-further.com/resources/css/IJX4FWDocu.css" );
                // String css = ((HtmlEmail)lEmail).embed( url, "name of css" );
                        
                ((HtmlEmail )lEmail).setHtmlMsg(
                "<html><body>" +
                "<style type=\"text/css\">" +
                "h1 { " +
                " font-family:arial, helvetica, sans-serif;" +
                " font-weight:bold;" +
                " font-size:18pt;" +
                "}" +
                "</style>" +
                // "<link href=\"cid:" + css + "\" type=\"text/css\" rel=\"stylesheet\">" +
                "<p><img src=\"cid:" + lCID + "\"></p>" +
                "<p><img src=\"http://five-feet-further.com/aschulze/images/portrait_web_kleiner.jpg\"></p>" +
                lItem +
                "</body></html>");
                 */

                /*
                // Now the message body.
                Multipart mp = new MimeMultipart();
                        
                BodyPart textPart = new MimeBodyPart();
                // sets type to "text/plain"
                textPart.setText("Kann Ihr Browser keine HTML-Mails darstellen?");
                        
                BodyPart pixPart = new MimeBodyPart();
                pixPart.setContent(lMsg, "text/html");
                        
                // Collect the Parts into the MultiPart
                mp.addBodyPart(textPart);
                mp.addBodyPart(pixPart);
                        
                // Put the MultiPart into the Message
                ((HtmlEmail) lEmail).setContent((MimeMultipart)mp);
                ((HtmlEmail) lEmail).buildMimeMessage();
                        
                /*
                // ((HtmlEmail) lEmail).setContent(lMsg, Email.TEXT_HTML);
                        
                // lHeaders.put("Innotrade-Id", "4711-0815");
                // lHTML.setHeaders(lHeaders);
                // ((HtmlEmail) lEmail).setCharset("UTF-8");
                // ((HtmlEmail) lEmail).setMsg(lMsg);
                lMM.setHeader("Innotrade-Id", "4711-0815");
                        
                // ((HtmlEmail) lEmail).setContent(lTxtMsg, Email.TEXT_PLAIN);
                 */
                // String lTxtMsg = "Your Email-Client does not support HTML messages.";
                lHTML.setHtmlMsg(lBody);
                // lHTML.setTextMsg(lTxtMsg);
            } else {
                lEmail.setMsg(lBody);
            }
        }

        // add attachment(s), if such
        for (EmailAttachment lAttachment : lAttachments) {
            ((MultiPartEmail) lEmail).attach(lAttachment);
        }

        for (int lIdx = 0; lIdx < lAttachments.size(); lIdx++) {
            ((MultiPartEmail) lEmail).attach((EmailAttachment) lAttachments.get(lIdx));
        }

        // send the Email
        String lMsgId = lEmail.send();

        if (mLog.isInfoEnabled()) {
            mLog.info("Email successfully sent" + " from " + (lFrom != null ? lFrom : "(no sender)") + " to "
                    + (lTo != null ? lTo : "(no receipient)") + ", subject "
                    + (lSubject != null ? "'" + lSubject + "'" : "(no subject)") + ", Id " + lMsgId);
        }

        lResponse.setString("id", lMsgId);
    } catch (Exception lEx) {
        String lMsg = lEx.getClass().getSimpleName() + ": " + lEx.getMessage();
        mLog.error(lMsg);
        lResponse.setInteger("code", -1);
        lResponse.setString("msg", lMsg);
    }

    // send response to requester
    lServer.sendToken(aConnector, lResponse);
}

From source file:org.jwebsocket.plugins.mail.MailPlugInService.java

/**
 *
 * @param aToken//  www.  ja  v a2  s. co  m
 * @return
 */
public Token sendMail(Token aToken) {
    String lFrom = aToken.getString("from", "[unknown]");
    String lTo = aToken.getString("to");
    String lCC = aToken.getString("cc");
    String lBCC = aToken.getString("bcc");
    String lSubject = aToken.getString("subject");
    String lBody = aToken.getString("body");
    Boolean lIsHTML = aToken.getBoolean("html", false);
    List<Object> lAttachedFiles = aToken.getList("attachments");
    String lMsg;

    // instantiate response token
    Token lResponse = TokenFactory.createToken();

    Map<String, String> lMap = new FastMap<String, String>();

    if (lFrom != null && lFrom.length() > 0) {
        lMap.put("from", lFrom);
    }
    if (lTo != null && lTo.length() > 0) {
        lMap.put("to", lTo);
    }
    if (lCC != null && lCC.length() > 0) {
        lMap.put("cc", lCC);
    }
    if (lBCC != null && lBCC.length() > 0) {
        lMap.put("bcc", lBCC);
    }
    if (lSubject != null && lSubject.length() > 0) {
        lMap.put("subject", lSubject);
    }
    if (lBody != null && lBody.length() > 0) {
        lMap.put("body", lBody);
    }

    // Create the attachment
    List<EmailAttachment> lEmailAttachments = new FastList<EmailAttachment>();

    if (lAttachedFiles != null) {
        for (Object lAttachedFile : lAttachedFiles) {
            EmailAttachment lAttachment = new EmailAttachment();
            lAttachment.setPath((String) lAttachedFile);
            lAttachment.setDisposition(EmailAttachment.ATTACHMENT);
            // lAttachment.setDescription( "Picture of John" );
            // lAttachment.setName( "John" );
            lEmailAttachments.add(lAttachment);
        }
    }

    // Create the lEmail message
    if (mLog.isDebugEnabled()) {
        mLog.debug("Sending e-mail to " + lTo + " with subject '" + lSubject + "'...");
    }
    try {
        Email lEmail;
        if (lIsHTML) {
            lEmail = new HtmlEmail();
        } else {
            lEmail = new MultiPartEmail();
        }

        lEmail.setHostName(mSettings.getSmtpHost());
        lEmail.setSmtpPort(mSettings.getSmtpPort());
        if (mSettings.getSmtpAuth()) {
            lEmail.setAuthentication(mSettings.getSmtpUser(), mSettings.getSmtpPassword());
        }
        if (mSettings.getSmtpPop3Before()) {
            lEmail.setPopBeforeSmtp(true, mSettings.getPop3Host(), mSettings.getPop3User(),
                    mSettings.getPop3Password());
        }
        if (lFrom != null && lFrom.length() > 0) {
            lEmail.setFrom(lFrom);
        }
        if (lTo != null && lTo.length() > 0) {
            String[] lToSplit = lTo.split(";");
            for (String lToSplit1 : lToSplit) {
                if (lToSplit1 != null && lToSplit1.length() > 0) {
                    lEmail.addTo(lToSplit1.trim());
                }
            }
        }
        if (lCC != null && lCC.length() > 0) {
            String[] lCCSplit = lCC.split(";");
            for (String lCCSplit1 : lCCSplit) {
                if (lCCSplit1 != null && lCCSplit1.length() > 0) {
                    lEmail.addCc(lCCSplit1.trim());
                }
            }
        }
        if (lBCC != null && lBCC.length() > 0) {
            String[] lBCCSplit = lBCC.split(";");
            for (String lBCCSplit1 : lBCCSplit) {
                if (lBCCSplit1 != null && lBCCSplit1.length() > 0) {
                    lEmail.addBcc(lBCCSplit1.trim());
                }
            }
        }
        if (lSubject != null && lSubject.length() > 0) {
            lEmail.setSubject(lSubject);
        }

        if (lBody != null && lBody.length() > 0) {
            if (lIsHTML) {
                HtmlEmail lHTML = ((HtmlEmail) lEmail);
                /*
                 * URL lURL = new
                 * URL("http://five-feet-further.com/aschulze/images/portrait_web_kleiner.jpg");
                 * String lCID = ((HtmlEmail )lEmail).embed(lURL, "five feet
                 * further logo");
                 *
                 * //url = new URL(
                 * "http://five-feet-further.com/resources/css/IJX4FWDocu.css"
                 * ); // String css = ((HtmlEmail)lEmail).embed( url, "name
                 * of css" );
                 *
                 * ((HtmlEmail )lEmail).setHtmlMsg( "<html><body>" + "<style
                 * type=\"text/css\">" + "h1 { " + " font-family:arial,
                 * helvetica, sans-serif;" + " font-weight:bold;" + "
                 * font-size:18pt;" + "}" + "</style>" + // "<link
                 * href=\"cid:" + css + "\" type=\"text/css\"
                 * rel=\"stylesheet\">" + "<p><img src=\"cid:" + lCID +
                 * "\"></p>" + "<p><img
                 * src=\"http://five-feet-further.com/aschulze/images/portrait_web_kleiner.jpg\"></p>"
                 * + lItem + "</body></html>");
                 */

                /*
                 * // Now the message body. Multipart mp = new
                 * MimeMultipart();
                 *
                 * BodyPart textPart = new MimeBodyPart(); // sets type to
                 * "text/plain" textPart.setText("Kann Ihr Browser keine
                 * HTML-Mails darstellen?");
                 *
                 * BodyPart pixPart = new MimeBodyPart();
                 * pixPart.setContent(lMsg, "text/html");
                 *
                 * // Collect the Parts into the MultiPart
                 * mp.addBodyPart(textPart); mp.addBodyPart(pixPart);
                 *
                 * // Put the MultiPart into the Message ((HtmlEmail)
                 * lEmail).setContent((MimeMultipart)mp); ((HtmlEmail)
                 * lEmail).buildMimeMessage();
                 *
                 * /*
                 * // ((HtmlEmail) lEmail).setContent(lMsg,
                 * Email.TEXT_HTML);
                 *
                 * // lHeaders.put("Innotrade-Id", "4711-0815"); //
                 * lHTML.setHeaders(lHeaders); // ((HtmlEmail)
                 * lEmail).setCharset("UTF-8"); // ((HtmlEmail)
                 * lEmail).setMsg(lMsg); lMM.setHeader("Innotrade-Id",
                 * "4711-0815");
                 *
                 * // ((HtmlEmail) lEmail).setContent(lTxtMsg,
                 * Email.TEXT_PLAIN);
                 */
                // String lTxtMsg = "Your Email-Client does not support HTML messages.";
                lHTML.setHtmlMsg(lBody);
                // lHTML.setTextMsg(lTxtMsg);
            } else {
                lEmail.setMsg(lBody);
            }
        }

        // add attachment(s), if such
        for (EmailAttachment lAttachment : lEmailAttachments) {
            ((MultiPartEmail) lEmail).attach(lAttachment);
        }

        // send the Email
        String lMsgId = lEmail.send();

        if (mLog.isInfoEnabled()) {
            lMsg = "Email successfully sent" + " from " + (lFrom != null ? lFrom : "(no sender)") + " to "
                    + (lTo != null ? lTo : "(no recipient)") + " cc " + (lCC != null ? lCC : "(no recipient)")
                    + ", subject " + (lSubject != null ? "'" + lSubject + "'" : "(no subject)") + ", msgId "
                    + lMsgId;
            mLog.info(lMsg);
        }
        lResponse.setInteger("code", 0);
        lResponse.setString("msg", "ok");
        lResponse.setString("msgId", lMsgId);
    } catch (EmailException lEx) {
        lMsg = lEx.getClass().getSimpleName() + " (" + lEx.getCause().getClass().getSimpleName() + "): "
                + lEx.getMessage();
        mLog.error(lMsg);
        lResponse.setInteger("code", -1);
        lResponse.setString("msg", lMsg);
    }
    return lResponse;
}

From source file:org.killbill.billing.plugin.notification.email.EmailSender.java

private void sendEmail(final List<String> to, final List<String> cc, final String subject, final Email email)
        throws EmailException {

    if (logOnly) {
        return;//  w w w  . j av a  2s  .com
    }

    email.setSmtpPort(useSmtpPort);
    if (useSmtpAuth) {
        email.setAuthentication(smtpUserName, smtpUserPassword);
    }
    email.setHostName(smtpServerName);
    email.setFrom(from);

    email.setSubject(subject);

    if (to != null) {
        for (final String recipient : to) {
            email.addTo(recipient);
        }
    }

    if (cc != null) {
        for (final String recipient : cc) {
            email.addCc(recipient);
        }
    }

    email.setSSL(useSSL);

    logService.log(LogService.LOG_INFO,
            String.format("Sending email to %s, cc %s, subject %s", to, cc, subject));
    email.send();
}

From source file:org.killbill.billing.util.email.DefaultEmailSender.java

private void sendEmail(final List<String> to, final List<String> cc, final String subject, final Email email)
        throws EmailApiException {
    try {//from   www. j  a  v  a  2 s  . c  o m
        email.setSmtpPort(config.getSmtpPort());
        if (config.useSmtpAuth()) {
            email.setAuthentication(config.getSmtpUserName(), config.getSmtpPassword());
        }
        email.setHostName(config.getSmtpServerName());
        email.setFrom(config.getDefaultFrom());

        email.setSubject(subject);

        if (to != null) {
            for (final String recipient : to) {
                email.addTo(recipient);
            }
        }

        if (cc != null) {
            for (final String recipient : cc) {
                email.addCc(recipient);
            }
        }

        email.setSSL(config.useSSL());

        log.info("Sending email to='{}', cc='{}', subject='{}'", to, cc, subject);
        email.send();
    } catch (EmailException ee) {
        throw new EmailApiException(ee, ErrorCode.EMAIL_SENDING_FAILED);
    }
}

From source file:org.kuali.mobility.email.service.EmailServiceImpl.java

@Override
public boolean sendEmail(String body, String subject, String emailAddressTo, String emailAddressFrom) {
    boolean emailSent = false;

    if (emailAddressFrom == null || StringUtils.isEmpty(emailAddressFrom)) {
        emailAddressFrom = kmeProperties.getProperty("email.from");
        if (emailAddressFrom == null) {
            return emailSent;
        }//from w  w  w .j  a  v a2s .  c  om
    }

    if (emailAddressTo == null || StringUtils.isEmpty(emailAddressTo)) {
        return emailSent;
    }

    if (subject == null || StringUtils.isEmpty(subject)) {
        return emailSent;
    }

    if (body == null || StringUtils.isEmpty(body)) {
        return emailSent;
    }

    try {
        Email email = new SimpleEmail();
        email.setHostName(kmeProperties.getProperty("email.host"));
        email.setSmtpPort(Integer.parseInt(kmeProperties.getProperty("email.port")));
        email.setAuthenticator(new DefaultAuthenticator(kmeProperties.getProperty("email.username"),
                kmeProperties.getProperty("email.passsword")));
        email.setSSLOnConnect(true);
        email.setFrom(emailAddressFrom);
        email.setSubject(subject);
        email.setMsg(body);
        email.addTo(emailAddressTo);
        email.send();
        emailSent = true;
        LOG.debug("Mail Sent...");
    } catch (EmailException e) {
        LOG.error("Mail send failed...", e);
    }
    return emailSent;
}