Example usage for org.apache.commons.mail MultiPartEmail addCc

List of usage examples for org.apache.commons.mail MultiPartEmail addCc

Introduction

In this page you can find the example usage for org.apache.commons.mail MultiPartEmail addCc.

Prototype

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

Source Link

Document

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

Usage

From source file:de.xirp.mail.MailManager.java

/**
 * Transports the given {@link de.xirp.mail.Mail} via
 * a SMTP server./* w  w w .j  a v  a  2  s. c o m*/
 * 
 * @param mail
 *            The mail to transport.
 * @throws EmailException
 *             if something is wrong with the given mail or the
 *             mail settings.
 * @see de.xirp.mail.Mail
 * @see de.xirp.mail.Contact
 * @see de.xirp.mail.Attachment
 */
private static void transport(Mail mail) throws EmailException {
    // Create the email message
    MultiPartEmail email = new MultiPartEmail();
    email.setHostName(PropertiesManager.getSmtpHost());
    email.setSmtpPort(PropertiesManager.getSmtpPort());

    if (PropertiesManager.isNeedsAuthentication()) {
        email.setAuthentication(PropertiesManager.getSmtpUser(),
                Util.decrypt(PropertiesManager.getSmtpPassword()));
    }

    for (Contact c : mail.getTo()) {
        email.addTo(c.getMail(), c.getFirstName() + " " + c.getLastName()); //$NON-NLS-1$
    }
    for (Contact c : mail.getCc()) {
        email.addCc(c.getMail(), c.getFirstName() + " " + c.getLastName()); //$NON-NLS-1$
    }

    email.setFrom(PropertiesManager.getNoReplyAddress(), Constants.APP_NAME);
    email.setSubject(mail.getSubject());
    email.setMsg(mail.getText() + I18n.getString(I18n.getString("MailManager.mail.footer", //$NON-NLS-1$
            Constants.LINE_SEPARATOR, Constants.BASE_NAME_MAJOR_VERSION)));

    // Create attachment
    for (Attachment a : mail.getAttachments()) {
        File file = a.getFile();
        EmailAttachment attachment = new EmailAttachment();

        if (file != null) {
            attachment.setPath(file.getPath());
        } else {
            File tmpFile = null;
            try {
                tmpFile = new File(Constants.TMP_DIR, a.getFileName());
                FileUtils.writeByteArrayToFile(tmpFile, a.getAttachmentFileContent());
                attachment.setPath(tmpFile.getPath());
            } catch (IOException e) {
                tmpFile = null;
            }
        }
        attachment.setDisposition(EmailAttachment.ATTACHMENT);
        attachment.setDescription(a.getFileName());
        attachment.setName(a.getFileName());
        email.attach(attachment);
    }

    try {
        email.send();
    } catch (EmailException e) {
        logClass.error("Error: " + e.getMessage() + Constants.LINE_SEPARATOR, e); //$NON-NLS-1$
        throw e;
    }
}

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

/**
 * @return/*from  w  w w  . j a v  a 2s.c o  m*/
 */
public String exportResults() {
    if (competitionId != null) {
        Competition competitionTemplate = getCompetitionService().find(competitionId);
        if (competitionTemplate == null) {
            log.error("Could not load competition: {}", competitionId);
            return Action.ERROR;
        }
        setCompetition(competitionTemplate);

        try {
            // load WCA template from file
            InputStream is = ServletActionContext.getServletContext()
                    .getResourceAsStream(getSpreadSheetFilename());
            Workbook workBook;
            workBook = WorkbookFactory.create(is);
            is.close();

            // build special registration sheet
            generateRegistrationSheet(workBook, getCompetition());

            // build result sheets
            generateResultSheets(workBook, getCompetition());

            // set default selected sheet
            workBook.setActiveSheet(workBook.getSheetIndex(SHEET_TYPE_REGISTRATION));

            // email or just output to pdf?
            if (isSubmitResultsToWCA()) {
                // write workbook to temp file
                File temp = File.createTempFile(getCompetitionId(), ".xls");
                temp.deleteOnExit();
                OutputStream os = new FileOutputStream(temp);
                workBook.write(os);
                os.close();

                // Create the attachment
                EmailAttachment attachment = new EmailAttachment();
                attachment.setPath(temp.getPath());
                attachment.setDisposition(EmailAttachment.ATTACHMENT);
                attachment.setName(getCompetitionId() + ".xls");

                // send email
                MultiPartEmail email = new MultiPartEmail();
                email.setCharset(Email.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("Results from " + getCompetition().getName());
                email.setMsg(getText("admin.export.message",
                        new String[] { getCompetition().getName(), getCompetition().getOrganiser() }));
                email.setFrom(getCompetition().getOrganiserEmail(), getCompetition().getOrganiser());
                email.addTo(getText("admin.export.resultsteamEmail"), getText("admin.export.resultsteam"));
                email.addCc(getCompetition().getOrganiserEmail(), getCompetition().getOrganiser());
                email.addCc(getCompetition().getWcaDelegateEmail(), getCompetition().getWcaDelegate());
                email.attach(attachment);
                email.send();

                return Action.SUCCESS;
            } else {
                // output generated spreadsheet
                log.debug("Ouputting generated workbook");
                out = new ByteArrayOutputStream();
                workBook.write(out);
                out.close();

                return "spreadsheet";
            }
        } catch (InvalidFormatException e) {
            log.error("Spreadsheet template are using an unsupported format.", e);
        } catch (IOException e) {
            log.error("Error reading spreadsheet template.", e);
        } catch (EmailException e) {
            log.error(e.getMessage(), e);
        }
        return Action.ERROR;
    } else {
        return Action.INPUT;
    }
}

From source file:ninja.postoffice.commonsmail.CommonsmailHelperImpl.java

@Override
public void doPopulateMultipartMailWithContent(MultiPartEmail multiPartEmail, Mail mail)
        throws AddressException, EmailException {

    String charset = "utf-8";
    if (mail.getCharset() != null) {
        charset = mail.getCharset();//from   ww  w .  j a  v  a 2s .c o  m
    }

    multiPartEmail.setCharset(charset);

    String subject = "";
    if (mail.getSubject() != null) {
        subject = mail.getSubject();
    }

    multiPartEmail.setSubject(subject);

    if (mail.getFrom() != null) {

        Tuple<String, String> from = createValidEmailFromString(mail.getFrom());

        if (from.y != null) {
            multiPartEmail.setFrom(from.x, from.y);
        } else {
            multiPartEmail.setFrom(from.x);
        }

    }

    if (mail.getTos() != null) {
        if (!mail.getTos().isEmpty()) {
            List<Tuple<String, String>> emails = createListOfAddresses(mail.getTos());
            for (Tuple<String, String> email : emails) {

                if (email.y != null) {
                    multiPartEmail.addTo(email.x, email.y);
                } else {
                    multiPartEmail.addTo(email.x);
                }

            }
        }
    }

    if (mail.getReplyTo() != null) {
        if (!mail.getReplyTo().isEmpty()) {
            List<Tuple<String, String>> emails = createListOfAddresses(mail.getReplyTo());
            for (Tuple<String, String> email : emails) {
                multiPartEmail.addReplyTo(email.x, email.y);
            }
        }
    }

    if (mail.getCcs() != null) {
        if (!mail.getCcs().isEmpty()) {
            List<Tuple<String, String>> emails = createListOfAddresses(mail.getCcs());
            for (Tuple<String, String> email : emails) {
                multiPartEmail.addCc(email.x, email.y);
            }
        }
    }

    if (mail.getBccs() != null) {
        if (!mail.getBccs().isEmpty()) {
            List<Tuple<String, String>> emails = createListOfAddresses(mail.getBccs());
            for (Tuple<String, String> email : emails) {
                multiPartEmail.addBcc(email.x, email.y);
            }
        }
    }

    if (mail.getHeaders() != null) {
        multiPartEmail.setHeaders(mail.getHeaders());
    }

}