Example usage for org.apache.commons.mail.resolver DataSourceUrlResolver DataSourceUrlResolver

List of usage examples for org.apache.commons.mail.resolver DataSourceUrlResolver DataSourceUrlResolver

Introduction

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

Prototype

public DataSourceUrlResolver(final URL baseUrl) 

Source Link

Document

Constructor.

Usage

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 {//w w w .  j  a  v a2 s  .  co 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;
    }
}