Example usage for org.apache.commons.mail SimpleEmail addReplyTo

List of usage examples for org.apache.commons.mail SimpleEmail addReplyTo

Introduction

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

Prototype

public Email addReplyTo(final String email) throws EmailException 

Source Link

Document

Add a reply to address to the email.

Usage

From source file:com.mycompany.apps.MailService.java

public void doSomeService() {
    HashMap map = new HashMap();
    map.put("name", "hoge");
    map.put("today", "2014/11/20");
    // ... ?map?????
    String msg = mailBuilder.buildMessage(map);
    System.out.println("?:" + msg);

    try {/*  ww w.  ja v a 2s. co m*/
        SimpleEmail email = new SimpleEmail();
        //??
        email.setHostName("localhost");
        //??
        List to = new ArrayList();
        InternetAddress to1 = new InternetAddress("to1@mail.myserver.com");
        to.add(to1);
        InternetAddress to2 = new InternetAddress("to2@mail.myserver.com");
        to.add(to2);
        email.setTo(to);

        //??
        email.setFrom("from@mail.myserver.com");
        //?
        email.addReplyTo("reply@mail.myserver.com");
        //??
        email.setSubject("");
        //??
        email.setMsg(msg);
        //??
        //            email.send();
    } catch (EmailException ex) {
        ex.printStackTrace();
    } catch (AddressException ex) {
        ex.printStackTrace();
    }
}

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

@Override
public Collection<Record> process(ProcessContext context, Collection<Record> records) {
    if (debug) {/*from w  w  w.  j  av  a2 s  . c  om*/
        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   ww w  .  j a va2  s.  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;
}

From source file:org.yestech.notify.deliver.TextEmailDelivery.java

protected void sendMessage(INotification notification, IRecipient recipient) throws EmailException {
    SimpleEmail email = new SimpleEmail();
    enableAuthenticator(email);//from  w  w  w .  j  a  v  a2  s  .  c  om
    email.setHostName(getEmailHost());
    ISender sender = notification.getSender();
    email.setFrom(sender.getEmailAddress(), sender.getDisplayName());
    if (StringUtils.isNotBlank(sender.getReplyAddress())) {
        email.addReplyTo(sender.getReplyAddress());
    }
    email.setSubject(notification.getMessage().getSubject());
    email.addTo(recipient.getEmailAddress(), recipient.getDisplayName());
    ITemplateLanguage template = notification.getTemplate();
    String appliedMessage = template.apply(notification.getMessage());
    email.setMsg(appliedMessage);
    email.send();
}