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

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

Introduction

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

Prototype

public List<InternetAddress> getToAddresses() 

Source Link

Document

Get the list of "To" addresses.

Usage

From source file:piecework.notification.concrete.SimpleEmailDispatcher.java

@Override
public boolean dispatch(String senderEmail, String senderName, List<User> recipients, List<User> bcc,
        String subject, String body) {
    try {/*  w  w w  . j  a  v a 2  s  .c  om*/
        SimpleEmail email = new SimpleEmail();
        email.setHostName(notificationSettings.getMailServerHost());
        email.setSmtpPort(notificationSettings.getMailServerPort());

        for (User u : recipients) {
            String emailAddr = u.getEmailAddress();
            if (emailAddr != null && !emailAddr.isEmpty()) {
                email.addTo(emailAddr, u.getDisplayName());
            }
        }
        List<InternetAddress> toList = email.getToAddresses();
        if (toList == null || toList.isEmpty()) {
            LOG.error("No email addresses were found for " + recipients + ". No emails were sent.");
            return false; // no recipients
        }

        if (bcc != null && !bcc.isEmpty()) {
            for (User u : bcc) {
                String emailAddr = u.getEmailAddress();
                if (emailAddr != null && !emailAddr.isEmpty()) {
                    email.addBcc(emailAddr, u.getDisplayName());
                }
            }
        }
        email.setFrom(senderEmail, senderName);
        email.setSubject(subject);
        email.setMsg(body);

        LOG.debug("Subject: " + email.getSubject());
        LOG.debug(email.getMimeMessage());
        email.send();
        return true;
    } catch (EmailException e) {
        LOG.error("Unable to send email with subject " + subject);
    }
    return false;
}

From source file:piecework.security.AccessTracker.java

public void alarm(final AlarmSeverity severity, final String message, final String entityId) {
    try {//from   w w  w  . j  av  a  2  s . c o  m
        SimpleEmail email = new SimpleEmail();
        email.setHostName(notificationSettings.getMailServerHost());
        email.setSmtpPort(notificationSettings.getMailServerPort());
        String adminEmail = notificationSettings.getAdminEmail();

        LOG.error("Alarming at severity " + severity + " : " + message);

        if (StringUtils.isEmpty(adminEmail)) {
            LOG.error("Unable to send alarm email, no admin email addressed configured.");
            return;
        }

        email.addTo(adminEmail);

        List<InternetAddress> toList = email.getToAddresses();
        if (toList == null || toList.isEmpty()) {
            LOG.error("No email addresses were found for " + adminEmail + ". No emails were sent.");
            return; // no recipients
        }

        String subject = notificationSettings.getApplicationName() + " is issuing " + severity + " alarm";
        StringBuilder body = new StringBuilder(message);

        if (StringUtils.isNotEmpty(entityId))
            body.append(crLf).append(crLf).append("Action attempted as principal ").append(entityId);
        else
            body.append(crLf).append(crLf).append("Action attempted by anonymous principal");

        email.setFrom(notificationSettings.getMailFromAddress(), notificationSettings.getMailFromLabel());
        email.setSubject(subject);
        email.setMsg(body.toString());

        LOG.debug("Subject: " + email.getSubject());
        LOG.debug(email.getMimeMessage());
        email.send();
    } catch (EmailException e) {
        LOG.error("Unable to send email with subject: Alarm from piecework: " + severity);
    }
}