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

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

Introduction

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

Prototype

public Email addBcc(final String email, final String name) throws EmailException 

Source Link

Document

Add a blind BCC recipient to the email using the specified address and the specified personal name.

Usage

From source file:dk.cubing.liveresults.action.admin.EmailAction.java

/**
 * @return/*ww  w  . j  a v a  2  s  . c  om*/
 */
public String sendEmail() {
    if (getCompetition() != null) {
        try {
            SimpleEmail email = new SimpleEmail();
            email.setCharset(SimpleEmail.ISO_8859_1);
            email.setHostName(getText("email.smtp.server"));
            if (!getText("email.username").isEmpty() && !getText("email.password").isEmpty()) {
                email.setAuthentication(getText("email.username"), getText("email.password"));
            }
            email.setSSL("true".equals(getText("email.ssl")));
            email.setSubject(getSubject());
            email.setMsg(getBody());
            email.setFrom(getCompetition().getOrganiserEmail(), getCompetition().getOrganiser());
            email.addBcc(getCompetition().getWcaDelegateEmail(), getCompetition().getWcaDelegate());
            if (isSendToAccepted()) {
                for (String toAddress : getAcceptedCompetitors()) {
                    email.addBcc(toAddress);
                }
            }
            if (isSendToPending()) {
                for (String toAddress : getPendingCompetitors()) {
                    email.addBcc(toAddress);
                }
            }
            email.send();
            return Action.SUCCESS;
        } catch (Exception e) {
            log.error("Could not send email upon competition creation!", e);
            return Action.ERROR;
        }
    } else {
        log.error("Could not load competition!");
        return Action.ERROR;
    }
}

From source file:dk.cubing.liveresults.action.admin.CompetitionAction.java

/**
 * @return//from  w w  w  .  j a va 2s. co  m
 * @throws Exception
 */
public String save() throws Exception {
    Competition competition = getCompetition();
    if (competition.getCountry().length() != 2) {
        competition.setCountry(getCountryUtil().getCountryCodeByName(competition.getCountry()));
    } else if (getCountryUtil().getCountryByCode(competition.getCountry()) != null) {
        competition.setCountry(competition.getCountry().toUpperCase());
    }
    if ("".equals(competition.getWebsite())) {
        competition.setWebsite(null);
    }
    User user = new User(competition.getCompetitionId(),
            getPasswordEncoder().encodePassword(getPassword(), null), isEnabled(), true, true, true,
            new GrantedAuthority[] { new GrantedAuthorityImpl("ROLE_USER") });
    if (getUserDetailsManager().userExists(competition.getCompetitionId())) {
        log.info("Updating user: {}", competition.getCompetitionId());
        getUserDetailsManager().updateUser(user);
        getCompetitionService().update(competition);
    } else {
        log.info("Creating user: {}", competition.getCompetitionId());
        getUserDetailsManager().createUser(user);
        getCompetitionService().create(competition);
        // send mail to organiser.
        if (competition.getOrganiserEmail() != null && !competition.getOrganiserEmail().isEmpty()) {
            log.info("Sending login information to new user: {}", competition.getCompetitionId());
            try {
                SimpleEmail email = new SimpleEmail();
                email.setCharset(SimpleEmail.ISO_8859_1);
                email.setHostName(getText("email.smtp.server"));
                if (!getText("email.username").isEmpty() && !getText("email.password").isEmpty()) {
                    email.setAuthentication(getText("email.username"), getText("email.password"));
                }
                email.setSSL("true".equals(getText("email.ssl")));
                email.setSubject(getText("competitions.email.subject", new String[] { competition.getName() }));
                email.setMsg(getText("competitions.email.message",
                        new String[] { competition.getCompetitionId(), getPassword() }));
                email.setFrom(getText("competitions.email.senderEmail"), getText("competitions.email.sender"));
                email.addBcc(getText("competitions.email.senderEmail"), getText("competitions.email.sender"));
                email.addTo(competition.getOrganiserEmail(), competition.getOrganiser());
                email.send();
            } catch (Exception e) {
                log.error("Could not send email upon competition creation!", e);
            }
        }
    }
    return Action.SUCCESS;
}

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 . ja va  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;
}