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

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

Introduction

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

Prototype

MultiPartEmail

Source Link

Usage

From source file:org.activiti5.engine.impl.bpmn.behavior.MailActivityBehavior.java

protected MultiPartEmail createMultiPartEmail(String text) {
    MultiPartEmail email = new MultiPartEmail();
    try {/*from   w w w .j a  va 2s  . co m*/
        email.setMsg(text);
        return email;
    } catch (EmailException e) {
        throw new ActivitiException("Could not create text-only email", e);
    }
}

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

/**
 * Sends the attachment of an email./*from ww w .j a v  a  2 s  . c om*/
 */
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 {//  ww w.  j  a  v a 2  s . 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.nifi.processors.email.ConsumeEWS.java

public MimeMessage parseMessage(EmailMessage item) throws Exception {
    EmailMessage ewsMessage = item;/*from www.  j a va  2s .  co m*/
    final String bodyText = ewsMessage.getBody().toString();

    MultiPartEmail mm;

    if (ewsMessage.getBody().getBodyType() == BodyType.HTML) {
        mm = new HtmlEmail().setHtmlMsg(bodyText);
    } else {
        mm = new MultiPartEmail();
        mm.setMsg(bodyText);
    }
    mm.setHostName("NiFi-EWS");
    //from
    mm.setFrom(ewsMessage.getFrom().getAddress());
    //to recipients
    ewsMessage.getToRecipients().forEach(x -> {
        try {
            mm.addTo(x.getAddress());
        } catch (EmailException e) {
            throw new ProcessException("Failed to add TO recipient.", e);
        }
    });
    //cc recipients
    ewsMessage.getCcRecipients().forEach(x -> {
        try {
            mm.addCc(x.getAddress());
        } catch (EmailException e) {
            throw new ProcessException("Failed to add CC recipient.", e);
        }
    });
    //subject
    mm.setSubject(ewsMessage.getSubject());
    //sent date
    mm.setSentDate(ewsMessage.getDateTimeSent());
    //add message headers
    ewsMessage.getInternetMessageHeaders().forEach(x -> mm.addHeader(x.getName(), x.getValue()));

    //Any attachments
    if (ewsMessage.getHasAttachments()) {
        ewsMessage.getAttachments().forEach(x -> {
            try {
                FileAttachment file = (FileAttachment) x;
                file.load();

                ByteArrayDataSource bds = new ByteArrayDataSource(file.getContent(), file.getContentType());

                mm.attach(bds, file.getName(), "", EmailAttachment.ATTACHMENT);
            } catch (MessagingException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    mm.buildMimeMessage();
    return mm.getMimeMessage();
}

From source file:org.apache.nifi.processors.email.GenerateAttachment.java

public byte[] WithAttachments(int amount) {
    MultiPartEmail email = new MultiPartEmail();
    try {//from w w  w  . j  av  a 2s . com

        email.setFrom(from);
        email.addTo(to);
        email.setSubject(subject);
        email.setMsg(message);
        email.setHostName(hostName);

        int x = 1;
        while (x <= amount) {
            // Create an attachment with the pom.xml being used to compile (yay!!!)
            EmailAttachment attachment = new EmailAttachment();
            attachment.setPath("pom.xml");
            attachment.setDisposition(EmailAttachment.ATTACHMENT);
            attachment.setDescription("pom.xml");
            attachment.setName("pom.xml" + String.valueOf(x));
            //  attach
            email.attach(attachment);
            x++;
        }
        email.buildMimeMessage();
    } catch (EmailException e) {
        e.printStackTrace();
    }
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    MimeMessage mimeMessage = email.getMimeMessage();
    try {
        mimeMessage.writeTo(output);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (MessagingException e) {
        e.printStackTrace();
    }

    return output.toByteArray();
}

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);//w  ww. ja v  a2  s.  co 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();
}

From source file:org.betaomega.finalmaveninvitationfx.GmailProvider.java

public void send(String address, String subject, String body, String invitationName, String invitationPath,
        String invitationMimeType) throws InvitationNotFoundException, UnsupportedEncodingException

{
    EmailAttachment attachment = new EmailAttachment();
    attachment.setPath(invitationPath);/*from   www . j  a v  a2s  . c om*/
    attachment.setDisposition(EmailAttachment.ATTACHMENT);
    attachment.setName(invitationName);
    MultiPartEmail email = new MultiPartEmail();
    email.setHostName("smtp.gmail.com");
    email.setSmtpPort(587);
    email.setAuthenticator(new DefaultAuthenticator(this.fromAddress, this.password));
    email.setStartTLSEnabled(true);

    email.setTLS(true);
    try {
        email.addTo(address);
    } catch (EmailException ex) {
        Logger.getLogger(Email.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        email.setFrom(this.fromAddress, this.fromName);
    } catch (EmailException ex) {
        Logger.getLogger(Email.class.getName()).log(Level.SEVERE, null, ex);
    }
    email.setSubject(subject);
    try {
        email.setMsg(body);
    } catch (EmailException ex) {
        Logger.getLogger(Email.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        email.attach(attachment);
    } catch (EmailException ex) {
        Logger.getLogger(Email.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        email.send();
    } catch (EmailException ex) {
        Logger.getLogger(Email.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:org.chenillekit.mail.services.impl.MailServiceImpl.java

/**
 * send a plain text message.//w ww  . ja  v  a2s .  co  m
 *
 * @param headers    the mail headers
 * @param body      the mail body (text based)
 * @param dataSources array of data sources to attach at this mail
 *
 * @return true if mail successfull send
 */
public boolean sendPlainTextMail(MailMessageHeaders headers, String body, DataSource... dataSources) {
    try {
        Email email = new SimpleEmail();

        if (dataSources != null && dataSources.length > 0) {
            MultiPartEmail multiPart = new MultiPartEmail();

            for (DataSource dataSource : dataSources)
                multiPart.attach(dataSource, dataSource.getName(), dataSource.getName());

            email = multiPart;
        }

        setEmailStandardData(email);

        setMailMessageHeaders(email, headers);

        email.setCharset(headers.getCharset());
        try {
            email.setMsg(new String(body.getBytes(), headers.getCharset()));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }

        String msgId = email.send();

        return true;
    } catch (EmailException e) {
        // FIXME Handle gracefully
        throw new RuntimeException(e);
    }
}

From source file:org.chenillekit.mail.services.TestMailService.java

@Test
public void test_multipartemail_sending() throws EmailException, MessagingException {
    MultiPartEmail email = new MultiPartEmail();
    email.setSubject("Test Mail 2");
    email.addTo("homburgs@gmail.com");
    email.setFrom("homburgs@gmail.com");
    email.setMsg("This is a dummy message text!");
    email.addPart("This is a dummy message part 1!", "text/plain");

    MimeMultipart mmp = new MimeMultipart();
    MimeBodyPart mbp = new MimeBodyPart();

    mbp.setText("This is a dummy MimeBodyPart 1!");

    mmp.addBodyPart(mbp);/*from ww  w  .  j a v a2 s  .c o  m*/
    email.addPart(mmp);

    EmailAttachment attachment = new EmailAttachment();
    attachment.setDescription("dummy.txt");
    attachment.setURL(new ClasspathResource("dummy.txt").toURL());
    email.attach(attachment);

    MailService mailService = registry.getService(MailService.class);
    boolean sended = mailService.sendEmail(email);

    assertTrue(sended, "sended");
}

From source file:org.cobbzilla.mail.sender.SmtpMailSender.java

private Email constructEmail(SimpleEmailMessage message) throws EmailException {
    final Email email;
    if (message instanceof ICalEvent) {
        final MultiPartEmail multiPartEmail = new MultiPartEmail();

        ICalEvent iCalEvent = (ICalEvent) message;

        // Calendar iCalendar = new Calendar();
        Calendar iCalendar = ICalUtil.newCalendarEvent(iCalEvent.getProdId(), iCalEvent);
        byte[] attachmentData = ICalUtil.toBytes(iCalendar);

        String icsName = iCalEvent.getIcsName() + ".ics";
        String contentType = "text/calendar; icsName=\"" + icsName + "\"";
        try {// w  w  w . j  ava2 s  . com
            multiPartEmail.attach(new ByteArrayDataSource(attachmentData, contentType), icsName, "",
                    EmailAttachment.ATTACHMENT);
        } catch (IOException e) {
            throw new EmailException("constructEmail: couldn't attach: " + e, e);
        }
        email = multiPartEmail;

    } else if (message.getHasHtmlMessage()) {
        final HtmlEmail htmlEmail = new HtmlEmail();
        htmlEmail.setTextMsg(message.getMessage());
        htmlEmail.setHtmlMsg(message.getHtmlMessage());
        email = htmlEmail;

    } else {
        email = new SimpleEmail();
        email.setMsg(message.getMessage());
    }
    return email;
}