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

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

Introduction

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

Prototype

public Email setBounceAddress(final String email) 

Source Link

Document

Set the "bounce address" - the address to which undeliverable messages will be returned.

Usage

From source file:com.pushinginertia.commons.net.email.EmailUtils.java

/**
 * Populates a newly instantiated {@link MultiPartEmail} with the given arguments.
 * @param email email instance to populate
 * @param smtpHost SMTP host that the message will be sent to
 * @param msg container object for the email's headers and contents
 * @throws IllegalArgumentException if the inputs are not valid
 * @throws EmailException if a problem occurs constructing the email
 *///from w  ww . ja  v  a 2s. c o  m
public static void populateMultiPartEmail(final MultiPartEmail email, final String smtpHost,
        final EmailMessage msg) throws IllegalArgumentException, EmailException {
    ValidateAs.notNull(email, "email");
    ValidateAs.notNull(smtpHost, "smtpHost");
    ValidateAs.notNull(msg, "msg");

    email.setHostName(smtpHost);
    email.setCharset(UTF_8);
    email.setFrom(msg.getSender().getEmail(), msg.getSender().getName(), UTF_8);
    email.addTo(msg.getRecipient().getEmail(), msg.getRecipient().getName(), UTF_8);

    final NameEmail replyTo = msg.getReplyTo();
    if (replyTo != null) {
        email.addReplyTo(replyTo.getEmail(), replyTo.getName(), UTF_8);
    }

    final String bounceEmailAddress = msg.getBounceEmailAddress();
    if (bounceEmailAddress != null) {
        email.setBounceAddress(bounceEmailAddress);
    }

    email.setSubject(msg.getSubject());

    final String languageId = msg.getRecipient().getLanguage();
    if (languageId != null) {
        email.addHeader("Language", languageId);
        email.addHeader("Content-Language", languageId);
    }

    // add optional headers
    final EmailMessageHeaders headers = msg.getHeaders();
    if (headers != null) {
        for (final Map.Entry<String, String> header : headers.getHeaders().entrySet()) {
            email.addHeader(header.getKey(), header.getValue());
        }
    }

    // generate email body
    try {
        final MimeMultipart mm = new MimeMultipart("alternative; charset=UTF-8");

        final MimeBodyPart text = new MimeBodyPart();
        text.setContent(msg.getTextContent(), "text/plain; charset=UTF-8");
        mm.addBodyPart(text);

        final MimeBodyPart html = new MimeBodyPart();
        html.setContent(msg.getHtmlContent(), "text/html; charset=UTF-8");
        mm.addBodyPart(html);

        email.setContent(mm);
    } catch (MessagingException e) {
        throw new EmailException(e);
    }

}

From source file:org.agnitas.util.AgnUtils.java

/**
 * Sends the attachment of an email.//from   w ww  .  j a  v  a  2s  .com
 */
public static boolean sendEmailAttachment(String from, String to_adrList, String cc_adrList, String subject,
        String txt, byte[] att_data, String att_name, String att_type) {
    boolean result = true;

    try {
        // Create the email message
        MultiPartEmail email = new MultiPartEmail();
        email.setCharset("UTF-8");
        email.setHostName(getSmtpMailRelayHostname());
        email.setFrom(from);
        email.setSubject(subject);
        email.setMsg(txt);

        // bounces and reply forwarded to assistance@agnitas.de
        email.addReplyTo("assistance@agnitas.de");
        email.setBounceAddress("assistance@agnitas.de");

        // Set to-recipient email addresses
        InternetAddress[] toAddresses = getEmailAddressesFromList(to_adrList);
        if (toAddresses != null && toAddresses.length > 0) {
            for (InternetAddress singleAdr : toAddresses) {
                email.addTo(singleAdr.getAddress());
            }
        }

        // Set cc-recipient email addresses
        InternetAddress[] ccAddresses = getEmailAddressesFromList(cc_adrList);
        if (ccAddresses != null && ccAddresses.length > 0) {
            for (InternetAddress singleAdr : ccAddresses) {
                email.addCc(singleAdr.getAddress());
            }
        }

        // Create and add the attachment
        ByteArrayDataSource attachment = new ByteArrayDataSource(att_data, att_type);
        email.attach(attachment, att_name, "EMM-Report");

        // send the email
        email.send();
    } catch (Exception e) {
        logger.error("sendEmailAttachment: " + e.getMessage(), e);
        result = false;
    }

    return result;
}

From source file:org.agnitas.util.ImportUtils.java

public static boolean sendEmailWithAttachments(String from, String fromName, String to, String subject,
        String message, EmailAttachment[] attachments) {
    boolean result = true;
    try {/*from   www  .  ja  v  a2s.  c o  m*/
        // Create the email message
        MultiPartEmail email = new MultiPartEmail();
        email.setCharset("UTF-8");
        email.setHostName(AgnUtils.getDefaultValue("system.mail.host"));
        email.addTo(to);

        if (fromName == null || fromName.equals(""))
            email.setFrom(from);
        else
            email.setFrom(from, fromName);

        email.setSubject(subject);
        email.setMsg(message);

        //bounces and reply forwarded to support@agnitas.de
        String replyName = AgnUtils.getDefaultValue("import.report.replyTo.name");
        if (replyName == null || replyName.equals(""))
            email.addReplyTo(AgnUtils.getDefaultValue("import.report.replyTo.address"));
        else
            email.addReplyTo(AgnUtils.getDefaultValue("import.report.replyTo.address"), replyName);

        email.setBounceAddress(AgnUtils.getDefaultValue("import.report.bounce"));

        // Create and attach attachments
        for (EmailAttachment attachment : attachments) {
            ByteArrayDataSource dataSource = new ByteArrayDataSource(attachment.getData(),
                    attachment.getType());
            email.attach(dataSource, attachment.getName(), attachment.getDescription());
        }

        // send the email
        email.send();
    } catch (Exception e) {
        AgnUtils.logger().error("sendEmailAttachment: " + e.getMessage());
        result = false;
    }
    return result;
}

From source file:org.apache.niolex.common.mail.MultiPartMail.java

public static void main(String[] args) throws IOException, EmailException {
    // Create the attachment
    EmailAttachment attachment = new EmailAttachment();
    attachment.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif"));
    attachment.setDisposition(EmailAttachment.ATTACHMENT);
    attachment.setDescription("Apache logo");
    attachment.setName("Apache_logo.gif");

    // Create the email message
    MultiPartEmail email = new MultiPartEmail();
    email.setDebug(true);//  www . ja  v  a2 s .  c  o m
    // Set email host
    email.setHostName("10.7.2.18");
    //        email.setAuthentication("nice_to_meet", "asf_logo_me");
    //        email.setSSL(true);
    // Set email from
    email.setFrom("lei.gao@renren-inc.com", "Commons Email");
    email.setBounceAddress("lei.gao@renren-inc.com");
    // Set email content
    email.addTo("jiyun.xie@renren-inc.com", "Jiyun Xie");
    email.addTo("lei.gao@renren-inc.com", "Lei Gao");
    email.setSubject("Foll Alert The Git test");
    email.setMsg("Here is Apache's logo, please enjoy it!");

    // add the attachment
    email.attach(attachment);

    // send the email
    email.send();
}