Example usage for org.apache.commons.mail MultiPartEmail MultiPartEmail

List of usage examples for org.apache.commons.mail MultiPartEmail MultiPartEmail

Introduction

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

Prototype

MultiPartEmail

Source Link

Usage

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

protected MultiPartEmail createMultiPartEmail(String text) {
    MultiPartEmail email = new MultiPartEmail();
    try {//from   w ww  . jav a  2 s. c  o  m
        email.setMsg(text);
        return email;
    } catch (EmailException e) {
        throw new FlowableException("Could not create text-only email", e);
    }
}

From source file:org.igov.io.mail.Mail.java

public void sendOld() throws EmailException {
    LOG.info("init");
    try {/*w  w w .  j a va2s  .  c om*/
        MultiPartEmail oMultiPartEmail = new MultiPartEmail();
        LOG.info("(getHost()={})", getHost());
        oMultiPartEmail.setHostName(getHost());

        String[] asTo = { sMailOnly(getTo()) };
        if (getTo().contains("\\,")) {
            asTo = getTo().split("\\,");//sTo
            for (String s : asTo) {
                LOG.info("oMultiPartEmail.addTo (s={})", s);
                oMultiPartEmail.addTo(s, "receiver");
            }
        }

        LOG.info("(getFrom()={})", getFrom());
        LOG_BIG.debug("(getFrom()={})", getFrom());
        oMultiPartEmail.setFrom(getFrom(), getFrom());//"iGov"
        oMultiPartEmail.setSubject(getHead());
        LOG.info("getHead()={}", getHead());
        String sLogin = getAuthUser();
        if (sLogin != null && !"".equals(sLogin.trim())) {
            oMultiPartEmail.setAuthentication(sLogin, getAuthPassword());
            LOG.info("withAuth");
        } else {
            LOG.info("withoutAuth");
        }
        //oMultiPartEmail.setAuthentication(getAuthUser(), getAuthPassword());
        LOG.info("(getAuthUser()={})", getAuthUser());
        //LOG.info("getAuthPassword()=" + getAuthPassword());
        oMultiPartEmail.setSmtpPort(getPort());
        LOG.info("(getPort()={})", getPort());
        oMultiPartEmail.setSSL(isSSL());
        LOG.info("(isSSL()={})", isSSL());
        oMultiPartEmail.setTLS(isTLS());
        LOG.info("(isTLS()={})", isTLS());

        oSession = oMultiPartEmail.getMailSession();
        MimeMessage oMimeMessage = new MimeMessage(oSession);

        //oMimeMessage.setFrom(new InternetAddress(getFrom(), "iGov", DEFAULT_ENCODING));
        oMimeMessage.setFrom(new InternetAddress(getFrom(), getFrom()));
        //oMimeMessage.addRecipient(Message.RecipientType.CC, new InternetAddress(sTo, sToName, DEFAULT_ENCODING));
        String sReceiverName = "receiver";
        if (asTo.length == 1) {
            sReceiverName = getToName();
        }
        for (String s : asTo) {
            LOG.info("oMimeMessage.addRecipient (s={})", s);
            //oMultiPartEmail.addTo(s, "receiver");
            oMimeMessage.addRecipient(Message.RecipientType.TO,
                    new InternetAddress(s, sReceiverName, DEFAULT_ENCODING));
        }

        //oMimeMessage.addRecipient(Message.RecipientType.TO,
        //        new InternetAddress(sTo, "recipient", DEFAULT_ENCODING));
        //new InternetAddress(getTo(), "recipient", DEFAULT_ENCODING));
        oMimeMessage.setSubject(getHead(), DEFAULT_ENCODING);

        _AttachBody(getBody());
        //LOG.info("(getBody()={})", getBody());
        oMimeMessage.setContent(oMultiparts);

        //            oMimeMessage.getRecipients(Message.RecipientType.CC);
        //methodCallRunner.registrateMethod(Transport.class.getName(), "send", new Object[]{oMimeMessage});
        Transport.send(oMimeMessage);
        LOG.info("Send " + getTo() + "!!!!!!!!!!!!!!!!!!!!!!!!");
    } catch (Exception oException) {
        LOG.error("FAIL: {} (getTo()={})", oException.getMessage(), getTo());
        LOG.trace("FAIL:", oException);
        throw new EmailException("Error happened when sending email (" + getTo() + ")" + "Exception message: "
                + oException.getMessage(), oException);
    }
    LOG.info("SUCCESS: Sent!");
}

From source file:org.igov.io.mail.MailOld.java

public void init() throws EmailException {
    //if(oMultiPartEmail!=null){
    //    return;
    //}//  www. j  a v  a  2 s . com
    LOG.info("init");
    oMultiPartEmail = new MultiPartEmail();
    oMultiPartEmail.setHostName(getHost());
    LOG.info("(getHost()={})", getHost());
    oMultiPartEmail.addTo(getTo(), "receiver");
    LOG.info("(getTo()={})", getTo());
    oMultiPartEmail.setFrom(getFrom(), getFrom());//"iGov"
    LOG.info("(getFrom()={})", getFrom());
    oMultiPartEmail.setSubject(getHead());
    LOG.info("(getHead()={})", getHead());
}

From source file:org.jooby.internal.mail.MultiPartEmailProvider.java

@Override
public MultiPartEmail get() {
    return factory.newEmail(new MultiPartEmail());
}

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

private void sendMail(WebSocketConnector aConnector, Token aToken) {
    TokenServer lServer = getServer();//from   w w w.  j  a  va  2s . com

    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/* w  w  w  . jav a 2  s. com*/
 * @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.kantega.respiro.mail.ServerConfig.java

public MultiPartEmail newMail() {
    MultiPartEmail mail = new MultiPartEmail();

    mail.setHostName(host);/*w w w  .  j ava2  s.  c om*/
    if (!ssl)
        mail.setSmtpPort(port);
    else
        mail.setSslSmtpPort(valueOf(port));
    if (username != null && password != null)
        mail.setAuthentication(username, password);

    try {
        if (!to.isEmpty())
            mail.setTo(to);
        if (!cc.isEmpty())
            mail.setCc(cc);
        if (!bcc.isEmpty())
            mail.setBcc(bcc);
        if (fromMail != null)
            mail.setFrom(fromMail);
    } catch (EmailException e) {
        throw new RuntimeException(e);
    }

    return mail;
}

From source file:org.kitodo.selenium.testframework.helper.MailSender.java

/**
 * Sends an email.//from www.  ja v  a  2  s .c  o  m
 * 
 * @param user
 *            The user name for login in to email account.
 * @param password
 *            The password for login in to email account.
 * @param subject
 *            The email subject.
 * @param message
 *            The email message.
 * @param attachedFile
 *            The attached file.
 * @param recipient
 *            The recipient email address.
 */
public static void sendEmail(String user, String password, String subject, String message, File attachedFile,
        String recipient) throws EmailException, AddressException {

    if (user != null && password != null && recipient != null) {
        InternetAddress address = new InternetAddress(recipient);

        ArrayList<InternetAddress> addressList = new ArrayList<>();
        addressList.add(address);

        EmailAttachment attachment = new EmailAttachment();
        if (attachedFile != null) {
            // Create the attachment
            attachment.setPath(attachedFile.getAbsolutePath());
            attachment.setDisposition(EmailAttachment.ATTACHMENT);
            attachment.setDescription("SeleniumScreenShot");
            attachment.setName("screenshot.png");
        }

        MultiPartEmail email = new MultiPartEmail();
        email.setHostName("smtp.gmail.com");
        email.setSmtpPort(465);
        email.setAuthenticator(new DefaultAuthenticator(user, password));
        email.setSSLOnConnect(true);
        email.setFrom("Travis CI Screenshot <kitodo.dev@gmail.com>");
        if (subject != null) {
            email.setSubject(subject);
        }
        if (message != null) {
            email.setMsg(message);
        }
        email.setTo(addressList);
        email.attach(attachment);
        email.send();
    } else {
        logger.error("Email was not send due to missing environmental variables");
    }

}

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

/**
 * Sends an email with attachment(s) via SMTP
 * //  w  ww.  j ava2s . com
 * @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.binding.mail.internal.MailBuilder.java

/**
 * Build the Mail/*from   ww w .j a  v  a 2s . c  om*/
 *
 * @return instance of Email
 * @throws EmailException if something goes wrong
 */
public Email build() throws EmailException {
    Email mail;

    if (attachmentURLs.isEmpty() && attachmentFiles.isEmpty() && html.isEmpty()) {
        // text mail without attachments
        mail = new SimpleEmail();
        if (!text.isEmpty()) {
            mail.setMsg(text);
        }
    } else if (html.isEmpty()) {
        // text mail with attachments
        MultiPartEmail multipartMail = new MultiPartEmail();
        if (!text.isEmpty()) {
            multipartMail.setMsg(text);
        }
        for (File file : attachmentFiles) {
            multipartMail.attach(file);
        }
        for (URL url : attachmentURLs) {
            EmailAttachment attachment = new EmailAttachment();
            attachment.setURL(url);
            attachment.setDisposition(EmailAttachment.ATTACHMENT);
            multipartMail.attach(attachment);
        }
        mail = multipartMail;
    } else {
        // html email
        HtmlEmail htmlMail = new HtmlEmail();
        if (!text.isEmpty()) {
            // alternate text supplied
            htmlMail.setTextMsg(text);
            htmlMail.setHtmlMsg(html);
        } else {
            htmlMail.setMsg(html);
        }
        for (File file : attachmentFiles) {
            htmlMail.attach(new FileDataSource(file), "", "");
        }
        for (URL url : attachmentURLs) {
            EmailAttachment attachment = new EmailAttachment();
            attachment.setURL(url);
            attachment.setDisposition(EmailAttachment.ATTACHMENT);
            htmlMail.attach(attachment);
        }
        mail = htmlMail;
    }

    mail.setTo(recipients);
    mail.setSubject(subject);

    if (!sender.isEmpty()) {
        mail.setFrom(sender);
    }

    return mail;
}