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

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

Introduction

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

Prototype

public Email addBcc(final String email) throws EmailException 

Source Link

Document

Add a blind BCC recipient to the email.

Usage

From source file:frameworkcontentspeed.Utils.SendEmailAtachament.java

public static void SendEmail(String from, String to1, String to2, String subject, String filename)
        throws FileNotFoundException, IOException {

    try {/*from  w  w  w.j a  v a2s  .  c  om*/

        EmailAttachment attachment = new EmailAttachment();
        attachment.setPath(filename);
        attachment.setDisposition(EmailAttachment.ATTACHMENT);

        attachment.setDescription("rezultat TC-uri");
        attachment.setName("rezultat TC-uri");

        MultiPartEmail email = new MultiPartEmail();

        email.setHostName("smtp.gmail.com");
        email.setSmtpPort(465);
        //email.setAuthenticator(new DefaultAuthenticator("test@contentspeed.ro", "andaanda"));
        email.setAuthenticator(new DefaultAuthenticator("anda.cristea@contentspeed.ro", "anda.cristea"));
        email.setSSLOnConnect(true);

        email.addBcc(to1);
        email.addBcc(to2);
        email.setFrom(from, "Teste Automate");
        email.setSubject(subject);
        email.setMsg(subject);

        // add the attachment
        //email.attach(attachment);
        StringWriter writer = new StringWriter();
        IOUtils.copy(new FileInputStream(new File(filename)), writer);

        email.setContent(writer.toString(), "text/html");
        email.attach(attachment);

        // send the email
        email.send();
        writer.close();
    } catch (Exception ex) {
        System.out.println("eroare trimitere email-uri");
        System.out.println(ex.getMessage());

    }

}

From source file:eu.ggnet.dwoss.mandator.api.value.Mandator.java

/**
 * Prepares a eMail to be send direct over the mandator smtp configuration.
 * The email is missing: to, subject, message and optional attachments.
 *
 * @return the email//from  w w w. j  av  a  2 s .com
 * @throws EmailException if something is wrong in the subsystem.
 */
public MultiPartEmail prepareDirectMail() throws EmailException {
    MultiPartEmail email = new MultiPartEmail();
    email.setHostName(smtpConfiguration.getHostname());
    email.addBcc(company.getEmail());
    email.setFrom(company.getEmail(), company.getEmailName());
    email.setAuthentication(smtpConfiguration.getSmtpAuthenticationUser(),
            smtpConfiguration.getSmtpAuthenticationPass());
    email.setStartTLSEnabled(false);
    email.setSSLCheckServerIdentity(false);
    email.setSSLOnConnect(false);
    email.setCharset(smtpConfiguration.getCharset());
    return email;
}

From source file:de.softwareforge.pgpsigner.commands.MailCommand.java

@Override
public void executeInteractiveCommand(final String[] args) {

    SecretKey secretKey = getContext().getSignKey();

    String senderMail = secretKey.getMailAddress();
    String senderName = secretKey.getName();

    if (StringUtils.isEmpty(senderMail)) {
        System.out.println("Could not extract sender mail from sign key.");
        return;/*  w w  w .  j  ava2s.  co m*/
    }

    for (final PublicKey key : getContext().getPartyRing().getVisibleKeys().values()) {

        if (key.isSigned() && !key.isMailed()) {

            try {
                String recipient = getContext().isSimulation() ? senderMail : key.getMailAddress();

                if (StringUtils.isEmpty(recipient)) {
                    System.out.println("No mail address for key " + key.getKeyId() + ", skipping.");
                    continue;
                }

                System.out.println("Sending Key " + key.getKeyId() + " to " + recipient);

                MultiPartEmail mail = new MultiPartEmail();
                mail.setHostName(getContext().getMailServerHost());
                mail.setSmtpPort(getContext().getMailServerPort());
                mail.setFrom(senderMail, senderName);
                mail.addTo(recipient);

                if (!getContext().isSimulation()) {
                    mail.addBcc(senderMail);
                }

                mail.setSubject("Your signed PGP key - " + key.getKeyId());
                mail.setMsg("This is your signed PGP key " + key.getKeyId() + " from the "
                        + getContext().getSignEvent() + " key signing event.");

                final String name = key.getKeyId() + ".asc";

                mail.attach(new DataSource() {

                    public String getContentType() {
                        return "application/pgp-keys";
                    }

                    public InputStream getInputStream() throws IOException {
                        return new ByteArrayInputStream(key.getArmor());
                    }

                    public String getName() {
                        return name;
                    }

                    public OutputStream getOutputStream() throws IOException {
                        throw new UnsupportedOperationException();
                    }
                }, name, "Signed Key " + key.getKeyId());

                mail.send();
                key.setMailed(true);

            } catch (EmailException ee) {
                System.out.println("Could not send mail for Key " + key.getKeyId());
            }
        }
    }
}

From source file:org.sakaiproject.kernel.email.outgoing.OutgoingEmailMessageListener.java

private MultiPartEmail constructMessage(Node messageNode, List<String> recipients)
        throws EmailException, RepositoryException, PathNotFoundException, ValueFormatException {
    MultiPartEmail email = new MultiPartEmail();
    //TODO: the SAKAI_TO may make no sense in an email context
    // and there does not appear to be any distinction between Bcc and To in java mail.

    Set<String> toRecipients = new HashSet<String>();
    Set<String> bccRecipients = new HashSet<String>();
    for (String r : recipients) {
        bccRecipients.add(r.trim());//from  w w w .  j ava 2s .com
    }

    if (messageNode.hasProperty(MessageConstants.PROP_SAKAI_TO)) {
        String[] tor = StringUtils.split(messageNode.getProperty(MessageConstants.PROP_SAKAI_TO).getString(),
                ',');
        for (String r : tor) {
            r = r.trim();
            if (bccRecipients.contains(r)) {
                toRecipients.add(r);
                bccRecipients.remove(r);
            }
        }
    }
    for (String r : toRecipients) {
        email.addTo(r);
    }
    for (String r : bccRecipients) {
        email.addBcc(r);
    }

    if (messageNode.hasProperty(MessageConstants.PROP_SAKAI_FROM)) {
        String from = messageNode.getProperty(MessageConstants.PROP_SAKAI_FROM).getString();
        email.setFrom(from);
    } else {
        throw new EmailException("Must provide a 'from' address.");
    }

    if (messageNode.hasProperty(MessageConstants.PROP_SAKAI_BODY)) {
        email.setMsg(messageNode.getProperty(MessageConstants.PROP_SAKAI_BODY).getString());
    }

    if (messageNode.hasProperty(MessageConstants.PROP_SAKAI_SUBJECT)) {
        email.setSubject(messageNode.getProperty(MessageConstants.PROP_SAKAI_SUBJECT).getString());
    }

    if (messageNode.hasNodes()) {
        NodeIterator ni = messageNode.getNodes();
        while (ni.hasNext()) {
            Node childNode = ni.nextNode();
            String description = null;
            if (childNode.hasProperty(MessageConstants.PROP_SAKAI_ATTACHMENT_DESCRIPTION)) {
                description = childNode.getProperty(MessageConstants.PROP_SAKAI_ATTACHMENT_DESCRIPTION)
                        .getString();
            }
            JcrEmailDataSource ds = new JcrEmailDataSource(childNode);
            email.attach(ds, childNode.getName(), description);
        }
    }

    return email;
}

From source file:org.sakaiproject.nakamura.email.outgoing.LiteOutgoingEmailMessageListener.java

protected MultiPartEmail constructMessage(Content contentNode, List<String> recipients,
        javax.jcr.Session session, org.sakaiproject.nakamura.api.lite.Session sparseSession)
        throws EmailDeliveryException, StorageClientException, AccessDeniedException, PathNotFoundException,
        RepositoryException, EmailException {
    MultiPartEmail email = new MultiPartEmail();

    Set<String> toRecipients = new HashSet<String>();

    toRecipients = setRecipients(recipients, sparseSession);

    String to = null;// w w w .j a v a  2 s.  co m
    try {
        // set from: to the reply as address
        email.setFrom(replyAsAddress, replyAsName);

        if (toRecipients.size() == 1) {
            // set to: to the rcpt if sending to just one person
            to = convertToEmail(toRecipients.iterator().next(), sparseSession);

            email.setTo(Lists.newArrayList(new InternetAddress(to)));
        } else {
            // set to: to 'undisclosed recipients' when sending to a group of recipients
            // this mirrors what shows up in RFC's and most major MTAs
            // http://www.postfix.org/postconf.5.html#undisclosed_recipients_header
            email.addHeader("To", "undisclosed-recipients:;");
        }
    } catch (EmailException e) {
        LOGGER.error("Cannot send email. From: address as configured is not valid: {}", replyAsAddress);
    } catch (AddressException e) {
        LOGGER.error("Cannot send email. To: address is not valid: {}", to);
    }

    // if we're dealing with a group of recipients, add them to bcc: to hide email
    // addresses from the other recipients
    if (toRecipients.size() > 1) {
        for (String r : toRecipients) {
            try {
                // we don't need to copy the sender on the message
                if (r.equals(contentNode.getProperty(MessageConstants.PROP_SAKAI_FROM))) {
                    continue;
                }
                email.addBcc(convertToEmail(r, sparseSession));
            } catch (EmailException e) {
                throw new EmailDeliveryException(
                        "Invalid To Address [" + r + "], message is being dropped :" + e.getMessage(), e);
            }
        }
    }

    if (contentNode.hasProperty(MessageConstants.PROP_SAKAI_BODY)) {
        String messageBody = String.valueOf(contentNode.getProperty(MessageConstants.PROP_SAKAI_BODY));
        // if this message has a template, use it
        LOGGER.debug(
                "Checking for sakai:templatePath and sakai:templateParams properties on the outgoing message's node.");
        if (contentNode.hasProperty(MessageConstants.PROP_TEMPLATE_PATH)
                && contentNode.hasProperty(MessageConstants.PROP_TEMPLATE_PARAMS)) {
            Map<String, String> parameters = getTemplateProperties(
                    (String) contentNode.getProperty(MessageConstants.PROP_TEMPLATE_PARAMS));
            String templatePath = (String) contentNode.getProperty(MessageConstants.PROP_TEMPLATE_PATH);
            LOGGER.debug("Got the path '{0}' to the template for this outgoing message.", templatePath);
            Node templateNode = session.getNode(templatePath);
            if (templateNode.hasProperty("sakai:template")) {
                String template = templateNode.getProperty("sakai:template").getString();
                LOGGER.debug("Pulled the template body from the template node: {0}", template);
                messageBody = templateService.evaluateTemplate(parameters, template);
                LOGGER.debug("Performed parameter substitution in the template: {0}", messageBody);
            }
        } else {
            LOGGER.debug(
                    "Message node '{0}' does not have sakai:templatePath and sakai:templateParams properties",
                    contentNode.getPath());
        }
        try {
            email.setMsg(messageBody);
        } catch (EmailException e) {
            throw new EmailDeliveryException(
                    "Invalid Message Body, message is being dropped :" + e.getMessage(), e);
        }
    }

    if (contentNode.hasProperty(MessageConstants.PROP_SAKAI_SUBJECT)) {
        email.setSubject((String) contentNode.getProperty(MessageConstants.PROP_SAKAI_SUBJECT));
    }

    ContentManager contentManager = sparseSession.getContentManager();
    for (String streamId : contentNode.listStreams()) {
        String description = null;
        if (contentNode.hasProperty(
                StorageClientUtils.getAltField(MessageConstants.PROP_SAKAI_ATTACHMENT_DESCRIPTION, streamId))) {
            description = (String) contentNode.getProperty(StorageClientUtils
                    .getAltField(MessageConstants.PROP_SAKAI_ATTACHMENT_DESCRIPTION, streamId));
        }
        LiteEmailDataSource ds = new LiteEmailDataSource(contentManager, contentNode, streamId);
        try {
            email.attach(ds, streamId, description);
        } catch (EmailException e) {
            throw new EmailDeliveryException(
                    "Invalid Attachment [" + streamId + "] message is being dropped :" + e.getMessage(), e);
        }
    }
    return email;
}

From source file:org.sakaiproject.nakamura.email.outgoing.OutgoingEmailMessageListener.java

private MultiPartEmail constructMessage(Node messageNode, List<String> recipients)
        throws EmailDeliveryException, RepositoryException, PathNotFoundException, ValueFormatException {
    MultiPartEmail email = new MultiPartEmail();
    javax.jcr.Session session = messageNode.getSession();
    //TODO: the SAKAI_TO may make no sense in an email context
    // and there does not appear to be any distinction between Bcc and To in java mail.

    Set<String> toRecipients = new HashSet<String>();
    Set<String> bccRecipients = new HashSet<String>();
    for (String r : recipients) {
        bccRecipients.add(convertToEmail(r.trim(), session));
    }//from  ww w .j  a  va  2  s.c o  m

    if (messageNode.hasProperty(MessageConstants.PROP_SAKAI_TO)) {
        String[] tor = StringUtils.split(messageNode.getProperty(MessageConstants.PROP_SAKAI_TO).getString(),
                ',');
        for (String r : tor) {
            r = convertToEmail(r.trim(), session);
            if (bccRecipients.contains(r)) {
                toRecipients.add(r);
                bccRecipients.remove(r);
            }
        }
    }
    for (String r : toRecipients) {
        try {
            email.addTo(convertToEmail(r, session));
        } catch (EmailException e) {
            throw new EmailDeliveryException(
                    "Invalid To Address [" + r + "], message is being dropped :" + e.getMessage(), e);
        }
    }
    for (String r : bccRecipients) {
        try {
            email.addBcc(convertToEmail(r, session));
        } catch (EmailException e) {
            throw new EmailDeliveryException(
                    "Invalid Bcc Address [" + r + "], message is being dropped :" + e.getMessage(), e);
        }
    }

    if (messageNode.hasProperty(MessageConstants.PROP_SAKAI_FROM)) {
        String from = messageNode.getProperty(MessageConstants.PROP_SAKAI_FROM).getString();
        try {
            email.setFrom(convertToEmail(from, session));
        } catch (EmailException e) {
            throw new EmailDeliveryException(
                    "Invalid From Address [" + from + "], message is being dropped :" + e.getMessage(), e);
        }
    } else {
        throw new EmailDeliveryException("Must provide a 'from' address.");
    }

    if (messageNode.hasProperty(MessageConstants.PROP_SAKAI_BODY)) {
        try {
            email.setMsg(messageNode.getProperty(MessageConstants.PROP_SAKAI_BODY).getString());
        } catch (EmailException e) {
            throw new EmailDeliveryException(
                    "Invalid Message Body, message is being dropped :" + e.getMessage(), e);
        }
    }

    if (messageNode.hasProperty(MessageConstants.PROP_SAKAI_SUBJECT)) {
        email.setSubject(messageNode.getProperty(MessageConstants.PROP_SAKAI_SUBJECT).getString());
    }

    if (messageNode.hasNodes()) {
        NodeIterator ni = messageNode.getNodes();
        while (ni.hasNext()) {
            Node childNode = ni.nextNode();
            String description = null;
            if (childNode.hasProperty(MessageConstants.PROP_SAKAI_ATTACHMENT_DESCRIPTION)) {
                description = childNode.getProperty(MessageConstants.PROP_SAKAI_ATTACHMENT_DESCRIPTION)
                        .getString();
            }
            JcrEmailDataSource ds = new JcrEmailDataSource(childNode);
            try {
                email.attach(ds, childNode.getName(), description);
            } catch (EmailException e) {
                throw new EmailDeliveryException("Invalid Attachment [" + childNode.getName()
                        + "] message is being dropped :" + e.getMessage(), e);
            }
        }
    }

    return email;
}