Example usage for org.apache.commons.mail ImageHtmlEmail setFrom

List of usage examples for org.apache.commons.mail ImageHtmlEmail setFrom

Introduction

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

Prototype

public Email setFrom(final String email) throws EmailException 

Source Link

Document

Set the FROM field of the email to use the specified address.

Usage

From source file:org.apache.isis.core.runtime.services.email.EmailServiceDefault.java

@Override
public boolean send(final List<String> toList, final List<String> ccList, final List<String> bccList,
        final String subject, final String body, final DataSource... attachments) {

    try {// www. j a v  a 2  s.  com
        final ImageHtmlEmail email = new ImageHtmlEmail();

        final String senderEmailAddress = getSenderEmailAddress();
        final String senderEmailPassword = getSenderEmailPassword();
        final String senderEmailHostName = getSenderEmailHostName();
        final Integer senderEmailPort = getSenderEmailPort();
        final Boolean senderEmailTlsEnabled = getSenderEmailTlsEnabled();
        final int socketTimeout = getSocketTimeout();
        final int socketConnectionTimeout = getSocketConnectionTimeout();

        email.setAuthenticator(new DefaultAuthenticator(senderEmailAddress, senderEmailPassword));
        email.setHostName(senderEmailHostName);
        email.setSmtpPort(senderEmailPort);
        email.setStartTLSEnabled(senderEmailTlsEnabled);
        email.setDataSourceResolver(new DataSourceClassPathResolver("/", true));

        email.setSocketTimeout(socketTimeout);
        email.setSocketConnectionTimeout(socketConnectionTimeout);

        final Properties properties = email.getMailSession().getProperties();

        properties.put("mail.smtps.auth", "true");
        properties.put("mail.debug", "true");
        properties.put("mail.smtps.port", "" + senderEmailPort);
        properties.put("mail.smtps.socketFactory.port", "" + senderEmailPort);
        properties.put("mail.smtps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        properties.put("mail.smtps.socketFactory.fallback", "false");
        properties.put("mail.smtp.starttls.enable", "" + senderEmailTlsEnabled);

        email.setFrom(senderEmailAddress);

        email.setSubject(subject);
        email.setHtmlMsg(body);

        if (attachments != null && attachments.length > 0) {
            for (DataSource attachment : attachments) {
                email.attach(attachment, attachment.getName(), "");
            }
        }

        final String overrideTo = getEmailOverrideTo();
        final String overrideCc = getEmailOverrideCc();
        final String overrideBcc = getEmailOverrideBcc();

        final String[] toListElseOverride = actually(toList, overrideTo);
        if (notEmpty(toListElseOverride)) {
            email.addTo(toListElseOverride);
        }
        final String[] ccListElseOverride = actually(ccList, overrideCc);
        if (notEmpty(ccListElseOverride)) {
            email.addCc(ccListElseOverride);
        }
        final String[] bccListElseOverride = actually(bccList, overrideBcc);
        if (notEmpty(bccListElseOverride)) {
            email.addBcc(bccListElseOverride);
        }

        email.send();

    } catch (EmailException ex) {
        LOG.error("An error occurred while trying to send an email", ex);
        final Boolean throwExceptionOnFail = isThrowExceptionOnFail();
        if (throwExceptionOnFail) {
            throw new EmailServiceException(ex);
        }
        return false;
    }

    return true;
}

From source file:rems.Global.java

public static boolean sendEmail(String toEml, String ccEml, String bccEml, String attchmnt, String sbjct,
        String bdyTxt, String[] errMsgs) {
    try {//from   w w  w  .j a  va  2 s  .c  o  m
        String selSql = "SELECT smtp_client, mail_user_name, mail_password, smtp_port FROM sec.sec_email_servers WHERE (is_default = 't')";
        ResultSet selDtSt = Global.selectDataNoParams(selSql);
        selDtSt.last();
        int m = selDtSt.getRow();
        String smtpClnt = "";
        String fromEmlNm = "";
        String fromPswd = "";
        errMsgs[0] = "";
        int portNo = 0;
        if (m > 0) {
            selDtSt.beforeFirst();
            selDtSt.next();
            smtpClnt = selDtSt.getString(1);
            fromEmlNm = selDtSt.getString(2);
            fromPswd = selDtSt.getString(3);
            portNo = selDtSt.getInt(4);
        }
        selDtSt.close();
        String fromPassword = Global.decrypt(fromPswd, Global.AppKey);
        // load your HTML email template
        if (bdyTxt.contains("<body") == false || bdyTxt.contains("</body>") == false) {
            bdyTxt = "<body>" + bdyTxt + "</body>";
        }
        if (bdyTxt.contains("<html") == false || bdyTxt.contains("</html>") == false) {
            bdyTxt = "<!DOCTYPE html><html lang=\"en\">" + bdyTxt + "</html>";
        }
        String htmlEmailTemplate = bdyTxt;
        // define you base URL to resolve relative resource locations
        URL url = new URL(Global.AppUrl);
        // create the email message
        ImageHtmlEmail email = new ImageHtmlEmail();
        email.setDataSourceResolver(new DataSourceUrlResolver(url));
        email.setHostName(smtpClnt);
        email.setSmtpPort(portNo);
        email.setAuthentication(fromEmlNm, fromPassword);
        email.setDebug(true);
        email.setStartTLSEnabled(true);
        email.setStartTLSRequired(true);

        String spltChars = "\\s*;\\s*";
        String[] toEmails = removeDplctChars(toEml).trim().split(spltChars);
        String[] ccEmails = removeDplctChars(ccEml).trim().split(spltChars);
        String[] bccEmails = removeDplctChars(bccEml).trim().split(spltChars);
        String[] attchMnts = removeDplctChars(attchmnt).trim().split(spltChars);
        for (int i = 0; i < attchMnts.length; i++) {
            if (attchMnts[i].equals("")) {
                continue;
            }
            EmailAttachment attachment = new EmailAttachment();
            if (attchMnts[i].startsWith("http://") || attchMnts[i].startsWith("https://")) {
                attachment.setURL(new URL(attchMnts[i].replaceAll(" ", "%20")));
                //"http://www.apache.org/images/asf_logo_wide.gif"
            } else {
                attachment.setPath(attchMnts[i].replaceAll(" ", "%20"));
            }
            attachment.setDisposition(EmailAttachment.ATTACHMENT);
            //attachment.setDescription("Picture of John");
            //attachment.setName("John");
            // add the attachment
            email.attach(attachment);
        }
        int lovID = Global.getLovID("Email Addresses to Ignore");
        int toMailsAdded = 0;
        for (int i = 0; i < toEmails.length; i++) {
            if (Global.isEmailValid(toEmails[i], lovID)) {
                if (Global.getEnbldPssblValID(toEmails[i], lovID) <= 0) {
                    //DO Nothing
                    toMailsAdded++;
                } else {
                    toEmails[i] = "ToBeRemoved";
                    errMsgs[0] += "Address:" + toEmails[i] + " blacklisted by you!\r\n";
                }
            } else {
                errMsgs[0] += "Address:" + toEmails[i] + " is Invalid!\r\n";
            }
        }
        if (toMailsAdded <= 0) {
            return false;
        }
        for (int i = 0; i < toEmails.length; i++) {
            if (toEmails[i].equals("ToBeRemoved")) {
                toEmails = (String[]) ArrayUtils.remove(toEmails, i);
            }
        }
        if (toEmails.length > 0) {
            if (toEmails[0].equals("") == false) {
                email.addTo(toEmails);
            }
        }
        for (int i = 0; i < ccEmails.length; i++) {
            if (Global.isEmailValid(ccEmails[i], lovID)) {
                if (Global.getEnbldPssblValID(ccEmails[i], lovID) <= 0) {
                    //DO Nothing
                } else {
                    ccEmails[i] = "ToBeRemoved";
                    errMsgs[0] += "Address:" + ccEmails[i] + " blacklisted by you!\r\n";
                }
            } else {
                errMsgs[0] += "Address:" + ccEmails[i] + " is Invalid!\r\n";
            }
        }
        for (int i = 0; i < ccEmails.length; i++) {
            if (ccEmails[i].equals("ToBeRemoved")) {
                ccEmails = (String[]) ArrayUtils.remove(ccEmails, i);
            }
        }
        if (ccEmails.length > 0) {
            if (ccEmails[0].equals("") == false) {
                email.addCc(ccEmails);
            }
        }
        for (int i = 0; i < bccEmails.length; i++) {
            if (Global.isEmailValid(bccEmails[i], lovID)) {
                if (Global.getEnbldPssblValID(bccEmails[i], lovID) <= 0) {
                    //DO Nothing
                } else {
                    bccEmails[i] = "ToBeRemoved";
                    errMsgs[0] += "Address:" + bccEmails[i] + " blacklisted by you!\r\n";
                }
            } else {
                errMsgs[0] += "Address:" + bccEmails[i] + " is Invalid!\r\n";
            }
        }
        for (int i = 0; i < bccEmails.length; i++) {
            if (bccEmails[i].equals("ToBeRemoved")) {
                bccEmails = (String[]) ArrayUtils.remove(bccEmails, i);
            }
        }
        if (bccEmails.length > 0) {
            if (bccEmails[0].equals("") == false) {
                email.addBcc(bccEmails);
            }
        }
        email.setFrom(fromEmlNm.trim());
        email.setSubject(sbjct);
        // set the html message
        email.setHtmlMsg(htmlEmailTemplate);
        // set the alternative message
        email.setTextMsg("Your email client does not support HTML messages");
        // send the email
        if (Global.CheckForInternetConnection()) {
            email.send();
            return true;
        }
        errMsgs[0] += "No Internet Connection";
        return false;
    } catch (SQLException ex) {
        Global.errorLog = "\r\nFailed to send Email!\r\n" + ex.getMessage();
        Global.writeToLog();
        errMsgs[0] += "Failed to send Email!\r\n" + ex.getMessage();
        return false;
    } catch (MalformedURLException ex) {
        Global.errorLog = "\r\nFailed to send Email!\r\n" + ex.getMessage();
        Global.writeToLog();
        errMsgs[0] += "Failed to send Email!\r\n" + ex.getMessage();
        return false;
    } catch (EmailException ex) {
        Global.errorLog = "\r\nFailed to send Email!\r\n" + ex.getMessage();
        Global.writeToLog();
        errMsgs[0] += "Failed to send Email!\r\n" + ex.getMessage();
        return false;
    } catch (Exception ex) {
        Global.errorLog = "\r\nFailed to send Email!\r\n" + ex.getMessage();
        Global.writeToLog();
        errMsgs[0] += "Failed to send Email!\r\n" + ex.getMessage();
        return false;
    }
}