Example usage for org.apache.commons.mail Email SENDER_EMAIL

List of usage examples for org.apache.commons.mail Email SENDER_EMAIL

Introduction

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

Prototype

String SENDER_EMAIL

To view the source code for org.apache.commons.mail Email SENDER_EMAIL.

Click Source Link

Usage

From source file:org.onehippo.forge.blog.components.Detail.java

/**
 * <p>Sends mail notification for newly added comment.</p>
 * <p>If component configuration for <code>receiver.email</code> is missing, no mails will be sent.</p>
 * @param commentBean {@link CommentBean}
 * @param request {@link HstRequest}//from   w w w.  j  a  va  2 s .  c o  m
 */
protected void sendNotificationMail(final CommentBean commentBean, final HstRequest request) {
    String mailhost = getParameter(Email.MAIL_HOST, request);
    String senderEmail = getParameter(Email.SENDER_EMAIL, request);
    String receiverEmail = getParameter(Email.RECEIVER_EMAIL, request);

    if (StringUtils.isBlank(mailhost)) {
        log.info("No value for mail.smtp.host in component configuration, trying localhost");
        mailhost = LOCALHOST;
    }
    if (StringUtils.isBlank(senderEmail)) {
        log.info("No value for sender.email in component configuration, trying noreply@example.com");
        senderEmail = DEFAULT_SENDER_MAIL;
    }
    if (StringUtils.isBlank(receiverEmail)) {
        log.warn(
                "No value for receiver.email in component configuration. Will not try to send mail notification.");
        return;
    }

    SimpleEmail email = new SimpleEmail();
    StringBuffer subject = new StringBuffer("New comment: ");
    subject.append(commentBean.getTitle());

    StringBuffer msg = new StringBuffer("The following comment has been added:\n");
    msg.append(commentBean.getSummary());

    email.setHostName(mailhost);
    try {
        email.addTo(receiverEmail);
        email.setFrom(senderEmail);
        email.setSubject(subject.toString());
        email.setMsg(msg.toString());
        email.send();
    } catch (EmailException e) {
        log.error("Error sending notification for added comment", e);
    }
}