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

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

Introduction

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

Prototype

public MultiPartEmail attach(final DataSource ds, String name, final String description,
        final String disposition) throws EmailException 

Source Link

Document

Attach a file specified as a DataSource interface.

Usage

From source file:com.webbfontaine.valuewebb.irms.action.mail.MailActionHandler.java

private static void attach(MultiPartEmail email, Attachable attachment) throws EmailException {
    try {//from  w w w.j  ava  2s. com
        DataSource source = attachment.getDataSource();
        InputStream inputStream = source.getInputStream();

        if (inputStream != null) {
            email.attach(dataSource(inputStream, source.getContentType()), attachment.getName(), "",
                    EmailAttachment.ATTACHMENT);
        } else {
            LOGGER.warn("Attached doc: {} content is empty, nothing to attach...", attachment.getName());
        }
    } catch (IOException e) {
        throw new EmailException(e);
    }
}

From source file:com.googlecode.fascinator.portal.process.EmailNotifier.java

public boolean emailAttachment(String from, String recipient, String subject, String body, byte[] attachData,
        String attachDataType, String attachFileName, String attachDesc) throws Exception {
    MultiPartEmail email = new MultiPartEmail();
    email.attach(new ByteArrayDataSource(attachData, attachDataType), attachFileName, attachDesc,
            EmailAttachment.ATTACHMENT);
    log.debug("Email host: " + host);
    log.debug("Email port: " + port);
    log.debug("Email username: " + username);
    log.debug("Email from: " + from);
    log.debug("Email to: " + recipient);
    log.debug("Email Subject is: " + subject);
    log.debug("Email Body is: " + body);
    email.setHostName(host);//from   w  w w  .j av  a 2 s. c om
    email.setSmtpPort(Integer.parseInt(port));
    email.setAuthenticator(new DefaultAuthenticator(username, password));
    // the method setSSL is deprecated on the newer versions of commons
    // email...
    email.setSSL("true".equalsIgnoreCase(ssl));
    email.setTLS("true".equalsIgnoreCase(tls));
    email.setFrom(from);
    email.setSubject(subject);
    email.setMsg(body);
    if (recipient.indexOf(",") >= 0) {
        String[] recs = recipient.split(",");
        for (String rec : recs) {
            email.addTo(rec);
        }
    } else {
        email.addTo(recipient);
    }
    email.send();
    return true;
}

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

public MimeMessage parseMessage(EmailMessage item) throws Exception {
    EmailMessage ewsMessage = item;//from  ww  w  .  j  ava 2  s . com
    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.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 {/*ww w .  j a  v  a2  s . c om*/
            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;
}