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, final String name) throws EmailException 

Source Link

Document

Set the FROM field of the email to use the specified address and the specified personal name.

Usage

From source file:com.hurence.logisland.processor.mailer.MailerProcessor.java

@Override
public Collection<Record> process(ProcessContext context, Collection<Record> records) {
    if (debug) {//from   ww w . j a va 2 s  .  com
        logger.info("Mailer Processor records input: " + records);
    }

    final Collection<Record> failedRecords = new ArrayList<>();

    /**
     * Transform the records into mails and send them
     */
    for (Record record : records) {
        String mailText = getStringField(record, FIELD_MAIL_TEXT);
        boolean text = (mailText != null);
        String mailHtml = getStringField(record, FIELD_MAIL_HTML);
        boolean html = (mailHtml != null);
        Field mailUseTemplate = record.getField(FIELD_MAIL_USE_TEMPLATE);
        boolean useTemplate = (mailUseTemplate != null);

        if (html || text || useTemplate) {
            // Ok, there is a mail to send. First retrieve some potential embedded overwritten configuration fields 

            String[] finalMailTos = mailTos;
            String finalMailFromAddress = mailFromAddress;
            String finalMailFromName = mailFromName;
            String finalMailBounceAddress = mailBounceAddress;
            String finalMailReplyToAddress = mailReplyToAddress;
            String finalMailSubject = mailSubject;

            /**
             * Overwrite some variables with special fields in the record if any and this is allowed
             */
            if (allowFieldsOverwriting) {
                String recordMailFromAddress = getStringField(record, FIELD_MAIL_FROM_ADDRESS);
                if (recordMailFromAddress != null) {
                    finalMailFromAddress = recordMailFromAddress;
                }

                String recordMailFromName = getStringField(record, FIELD_MAIL_FROM_NAME);
                if (recordMailFromName != null) {
                    finalMailFromName = recordMailFromName;
                }

                String recordMailBounceAddress = getStringField(record, FIELD_MAIL_BOUNCE_ADDRESS);
                if (recordMailBounceAddress != null) {
                    finalMailBounceAddress = recordMailBounceAddress;
                }

                String recordMailReplyToAddress = getStringField(record, FIELD_MAIL_REPLYTO_ADDRESS);
                if (recordMailReplyToAddress != null) {
                    finalMailReplyToAddress = recordMailReplyToAddress;
                }

                String recordMailSubject = getStringField(record, FIELD_MAIL_SUBJECT);
                if (recordMailSubject != null) {
                    finalMailSubject = recordMailSubject;
                }

                String recordMailTo = getStringField(record, FIELD_MAIL_TO);
                if (recordMailTo != null) {
                    finalMailTos = parseMailTo(recordMailTo);
                }
            }

            if (finalMailFromAddress == null) {
                record.addError(ProcessError.UNKNOWN_ERROR.getName(), "No From address defined");
                failedRecords.add(record);
                continue;
            }

            if (finalMailBounceAddress == null) {
                record.addError(ProcessError.UNKNOWN_ERROR.getName(), "No Bounce address defined");
                failedRecords.add(record);
                continue;
            }

            if (html || useTemplate) {
                if (html && useTemplate) {
                    record.addError(ProcessError.UNKNOWN_ERROR.getName(),
                            "Record has both " + FIELD_MAIL_USE_TEMPLATE + " and " + FIELD_MAIL_HTML
                                    + " fields. Only one of them is expected.");
                    failedRecords.add(record);
                    continue;
                }

                // HTML mail
                try {

                    /**
                     * Create and fill the mail
                     */

                    ImageHtmlEmail htmlEmail = new ImageHtmlEmail();

                    // Set From info
                    if (finalMailFromName != null) {
                        htmlEmail.setFrom(finalMailFromAddress, finalMailFromName);
                    } else {
                        htmlEmail.setFrom(finalMailFromAddress);
                    }

                    htmlEmail.setBounceAddress(finalMailBounceAddress);

                    if (finalMailReplyToAddress != null) {
                        htmlEmail.addReplyTo(finalMailReplyToAddress);
                    }

                    htmlEmail.setSubject(finalMailSubject);

                    // Allow to retrieve embedded images of the html template from the classpath (jar files)
                    htmlEmail.setDataSourceResolver(new DataSourceClassPathResolver());

                    // Compute final HTML body from template and record fields
                    String htmlBody = null;
                    if (useTemplate) {
                        htmlBody = createHtml(record);
                        if (htmlBody == null) {
                            // Error. Error message has already been set in createHtml: just add to failed record and
                            // continue with next record
                            failedRecords.add(record);
                            continue;
                        }
                    } else {
                        // html
                        htmlBody = mailHtml;
                    }

                    htmlEmail.setHtmlMsg(htmlBody);

                    // Add alternative text mail if any defined
                    if (text) {
                        htmlEmail.setTextMsg(mailText);
                    }

                    // Set To info
                    if (finalMailTos.length == 0) {
                        record.addError(ProcessError.UNKNOWN_ERROR.getName(), "No mail recipient.");
                        failedRecords.add(record);
                        continue;
                    }
                    for (String mailTo : finalMailTos) {
                        htmlEmail.addTo(mailTo);
                    }

                    /**
                     * Set sending parameters
                     */

                    htmlEmail.setHostName(smtpServer);
                    htmlEmail.setSmtpPort(smtpPort);
                    if ((smtpSecurityUsername != null) && (smtpSecurityPassword != null)) {
                        htmlEmail.setAuthentication(smtpSecurityUsername, smtpSecurityPassword);
                    }
                    htmlEmail.setSSLOnConnect(smtpSecuritySsl);

                    // Send the mail
                    htmlEmail.send();
                } catch (EmailException ex) {
                    record.addError(ProcessError.UNKNOWN_ERROR.getName(),
                            "Unable to send email: " + ex.getMessage());
                    failedRecords.add(record);
                }
            } else {
                // Only text mail
                try {

                    /**
                     * Create and fill the mail
                     */

                    SimpleEmail textEmail = new SimpleEmail();

                    // Set From info
                    if (finalMailFromName != null) {
                        textEmail.setFrom(finalMailFromAddress, finalMailFromName);
                    } else {
                        textEmail.setFrom(finalMailFromAddress);
                    }

                    textEmail.setBounceAddress(finalMailBounceAddress);
                    if (finalMailReplyToAddress != null) {
                        textEmail.addReplyTo(finalMailReplyToAddress);
                    }

                    textEmail.setSubject(finalMailSubject);
                    textEmail.setMsg(mailText);

                    // Set To info
                    if (finalMailTos.length == 0) {
                        record.addError(ProcessError.UNKNOWN_ERROR.getName(), "No mail recipient.");
                        failedRecords.add(record);
                        continue;
                    }
                    for (String mailTo : finalMailTos) {
                        textEmail.addTo(mailTo);
                    }

                    /**
                     * Set sending parameters
                     */

                    textEmail.setHostName(smtpServer);
                    textEmail.setSmtpPort(smtpPort);
                    if ((smtpSecurityUsername != null) && (smtpSecurityPassword != null)) {
                        textEmail.setAuthentication(smtpSecurityUsername, smtpSecurityPassword);
                    }
                    textEmail.setSSLOnConnect(smtpSecuritySsl);

                    // Send the mail
                    textEmail.send();
                } catch (EmailException ex) {
                    record.addError(ProcessError.UNKNOWN_ERROR.getName(),
                            "Unable to send email: " + ex.getMessage());
                    failedRecords.add(record);
                }
            }
        }
    }

    if (debug) {
        logger.info("Mailer Processor records output: " + records);
    }
    return failedRecords;
}

From source file:com.hurence.logisland.processor.SendMail.java

@Override
public Collection<Record> process(ProcessContext context, Collection<Record> records) {
    if (debug) {/*from   www  .  j  a  va2s.  c  o m*/
        logger.info("SendMail Processor records input: " + records);
    }

    final Collection<Record> failedRecords = new ArrayList<>();

    /**
     * Transform the records into mails and send them
     */
    for (Record record : records) {
        String mailText = getStringField(record, FIELD_MAIL_TEXT);
        boolean text = (mailText != null);
        String mailHtml = getStringField(record, FIELD_MAIL_HTML);
        boolean html = (mailHtml != null);
        Field mailUseTemplate = record.getField(FIELD_MAIL_USE_TEMPLATE);
        boolean useTemplate = (mailUseTemplate != null);

        if (html || text || useTemplate) {
            // Ok, there is a mail to send. First retrieve some potential embedded overwritten configuration fields 

            String[] finalMailTos = mailTos;
            String finalMailFromAddress = mailFromAddress;
            String finalMailFromName = mailFromName;
            String finalMailBounceAddress = mailBounceAddress;
            String finalMailReplyToAddress = mailReplyToAddress;
            String finalMailSubject = mailSubject;

            /**
             * Overwrite some variables with special fields in the record if any and this is allowed
             */
            if (allowFieldsOverwriting) {
                String recordMailFromAddress = getStringField(record, FIELD_MAIL_FROM_ADDRESS);
                if (recordMailFromAddress != null) {
                    finalMailFromAddress = recordMailFromAddress;
                }

                String recordMailFromName = getStringField(record, FIELD_MAIL_FROM_NAME);
                if (recordMailFromName != null) {
                    finalMailFromName = recordMailFromName;
                }

                String recordMailBounceAddress = getStringField(record, FIELD_MAIL_BOUNCE_ADDRESS);
                if (recordMailBounceAddress != null) {
                    finalMailBounceAddress = recordMailBounceAddress;
                }

                String recordMailReplyToAddress = getStringField(record, FIELD_MAIL_REPLYTO_ADDRESS);
                if (recordMailReplyToAddress != null) {
                    finalMailReplyToAddress = recordMailReplyToAddress;
                }

                String recordMailSubject = getStringField(record, FIELD_MAIL_SUBJECT);
                if (recordMailSubject != null) {
                    finalMailSubject = recordMailSubject;
                }

                String recordMailTo = getStringField(record, FIELD_MAIL_TO);
                if (recordMailTo != null) {
                    finalMailTos = parseMailTo(recordMailTo);
                }
            }

            if (finalMailFromAddress == null) {
                record.addError(ProcessError.UNKNOWN_ERROR.getName(), "No From address defined");
                failedRecords.add(record);
                continue;
            }

            if (finalMailBounceAddress == null) {
                record.addError(ProcessError.UNKNOWN_ERROR.getName(), "No Bounce address defined");
                failedRecords.add(record);
                continue;
            }

            if (html || useTemplate) {
                if (html && useTemplate) {
                    record.addError(ProcessError.UNKNOWN_ERROR.getName(),
                            "Record has both " + FIELD_MAIL_USE_TEMPLATE + " and " + FIELD_MAIL_HTML
                                    + " fields. Only one of them is expected.");
                    failedRecords.add(record);
                    continue;
                }

                // HTML mail
                try {

                    /**
                     * Create and fill the mail
                     */

                    ImageHtmlEmail htmlEmail = new ImageHtmlEmail();

                    // Set From info
                    if (finalMailFromName != null) {
                        htmlEmail.setFrom(finalMailFromAddress, finalMailFromName);
                    } else {
                        htmlEmail.setFrom(finalMailFromAddress);
                    }

                    htmlEmail.setBounceAddress(finalMailBounceAddress);

                    if (finalMailReplyToAddress != null) {
                        htmlEmail.addReplyTo(finalMailReplyToAddress);
                    }

                    htmlEmail.setSubject(finalMailSubject);

                    // Allow to retrieve embedded images of the html template from the classpath (jar files)
                    htmlEmail.setDataSourceResolver(new DataSourceClassPathResolver());

                    // Compute final HTML body from template and record fields
                    String htmlBody = null;
                    if (useTemplate) {
                        htmlBody = createHtml(record);
                        if (htmlBody == null) {
                            // Error. Error message has already been set in createHtml: just add to failed record and
                            // continue with next record
                            failedRecords.add(record);
                            continue;
                        }
                    } else {
                        // html
                        htmlBody = mailHtml;
                    }

                    htmlEmail.setHtmlMsg(htmlBody);

                    // Add alternative text mail if any defined
                    if (text) {
                        htmlEmail.setTextMsg(mailText);
                    }

                    // Set To info
                    if (finalMailTos.length == 0) {
                        record.addError(ProcessError.UNKNOWN_ERROR.getName(), "No mail recipient.");
                        failedRecords.add(record);
                        continue;
                    }
                    for (String mailTo : finalMailTos) {
                        htmlEmail.addTo(mailTo);
                    }

                    /**
                     * Set sending parameters
                     */

                    htmlEmail.setHostName(smtpServer);
                    htmlEmail.setSmtpPort(smtpPort);
                    if ((smtpSecurityUsername != null) && (smtpSecurityPassword != null)) {
                        htmlEmail.setAuthentication(smtpSecurityUsername, smtpSecurityPassword);
                    }
                    htmlEmail.setSSLOnConnect(smtpSecuritySsl);

                    // Send the mail
                    htmlEmail.send();
                } catch (EmailException ex) {
                    record.addError(ProcessError.UNKNOWN_ERROR.getName(),
                            "Unable to send email: " + ex.getMessage());
                    failedRecords.add(record);
                }
            } else {
                // Only text mail
                try {

                    /**
                     * Create and fill the mail
                     */

                    SimpleEmail textEmail = new SimpleEmail();

                    // Set From info
                    if (finalMailFromName != null) {
                        textEmail.setFrom(finalMailFromAddress, finalMailFromName);
                    } else {
                        textEmail.setFrom(finalMailFromAddress);
                    }

                    textEmail.setBounceAddress(finalMailBounceAddress);
                    if (finalMailReplyToAddress != null) {
                        textEmail.addReplyTo(finalMailReplyToAddress);
                    }

                    textEmail.setSubject(finalMailSubject);
                    textEmail.setMsg(mailText);

                    // Set To info
                    if (finalMailTos.length == 0) {
                        record.addError(ProcessError.UNKNOWN_ERROR.getName(), "No mail recipient.");
                        failedRecords.add(record);
                        continue;
                    }
                    for (String mailTo : finalMailTos) {
                        textEmail.addTo(mailTo);
                    }

                    /**
                     * Set sending parameters
                     */

                    textEmail.setHostName(smtpServer);
                    textEmail.setSmtpPort(smtpPort);
                    if ((smtpSecurityUsername != null) && (smtpSecurityPassword != null)) {
                        textEmail.setAuthentication(smtpSecurityUsername, smtpSecurityPassword);
                    }
                    textEmail.setSSLOnConnect(smtpSecuritySsl);

                    // Send the mail
                    textEmail.send();
                } catch (EmailException ex) {
                    record.addError(ProcessError.UNKNOWN_ERROR.getName(),
                            "Unable to send email: " + ex.getMessage());
                    failedRecords.add(record);
                }
            }
        }
    }

    if (debug) {
        logger.info("SendMail Processor records output: " + records);
    }
    return failedRecords;
}