Search in sources :

Example 51 with Multipart

use of javax.mail.Multipart in project spring-integration-samples by spring-projects.

the class EmailParserUtils method handleMultipart.

/**
 * Parses any {@link Multipart} instances that contain text or Html attachments,
 * {@link InputStream} instances, additional instances of {@link Multipart}
 * or other attached instances of {@link javax.mail.Message}.
 *
 * Will create the respective {@link EmailFragment}s representing those attachments.
 *
 * Instances of {@link javax.mail.Message} are delegated to
 * {@link #handleMessage(File, javax.mail.Message, List)}. Further instances
 * of {@link Multipart} are delegated to
 * {@link #handleMultipart(File, Multipart, javax.mail.Message, List)}.
 *
 * @param directory Must not be null
 * @param multipart Must not be null
 * @param mailMessage Must not be null
 * @param emailFragments Must not be null
 */
public static void handleMultipart(File directory, Multipart multipart, javax.mail.Message mailMessage, List<EmailFragment> emailFragments) {
    Assert.notNull(directory, "The directory must not be null.");
    Assert.notNull(multipart, "The multipart object to be parsed must not be null.");
    Assert.notNull(mailMessage, "The mail message to be parsed must not be null.");
    Assert.notNull(emailFragments, "The collection of emailfragments must not be null.");
    final int count;
    try {
        count = multipart.getCount();
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info(String.format("Number of enclosed BodyPart objects: %s.", count));
        }
    } catch (MessagingException e) {
        throw new IllegalStateException("Error while retrieving the number of enclosed BodyPart objects.", e);
    }
    for (int i = 0; i < count; i++) {
        final BodyPart bp;
        try {
            bp = multipart.getBodyPart(i);
        } catch (MessagingException e) {
            throw new IllegalStateException("Error while retrieving body part.", e);
        }
        final String contentType;
        String filename;
        final String disposition;
        final String subject;
        try {
            contentType = bp.getContentType();
            filename = bp.getFileName();
            disposition = bp.getDisposition();
            subject = mailMessage.getSubject();
            if (filename == null && bp instanceof MimeBodyPart) {
                filename = ((MimeBodyPart) bp).getContentID();
            }
        } catch (MessagingException e) {
            throw new IllegalStateException("Unable to retrieve body part meta data.", e);
        }
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info(String.format("BodyPart - Content Type: '%s', filename: '%s', disposition: '%s', subject: '%s'", new Object[] { contentType, filename, disposition, subject }));
        }
        if (Part.ATTACHMENT.equalsIgnoreCase(disposition)) {
            LOGGER.info(String.format("Handdling attachment '%s', type: '%s'", filename, contentType));
        }
        final Object content;
        try {
            content = bp.getContent();
        } catch (IOException e) {
            throw new IllegalStateException("Error while retrieving the email contents.", e);
        } catch (MessagingException e) {
            throw new IllegalStateException("Error while retrieving the email contents.", e);
        }
        if (content instanceof String) {
            if (Part.ATTACHMENT.equalsIgnoreCase(disposition)) {
                emailFragments.add(new EmailFragment(directory, i + "-" + filename, content));
                LOGGER.info(String.format("Handdling attachment '%s', type: '%s'", filename, contentType));
            } else {
                final String textFilename;
                final ContentType ct;
                try {
                    ct = new ContentType(contentType);
                } catch (ParseException e) {
                    throw new IllegalStateException("Error while parsing content type '" + contentType + "'.", e);
                }
                if ("text/plain".equalsIgnoreCase(ct.getBaseType())) {
                    textFilename = "message.txt";
                } else if ("text/html".equalsIgnoreCase(ct.getBaseType())) {
                    textFilename = "message.html";
                } else {
                    textFilename = "message.other";
                }
                emailFragments.add(new EmailFragment(directory, textFilename, content));
            }
        } else if (content instanceof InputStream) {
            final InputStream inputStream = (InputStream) content;
            final ByteArrayOutputStream bis = new ByteArrayOutputStream();
            try {
                IOUtils.copy(inputStream, bis);
            } catch (IOException e) {
                throw new IllegalStateException("Error while copying input stream to the ByteArrayOutputStream.", e);
            }
            emailFragments.add(new EmailFragment(directory, filename, bis.toByteArray()));
        } else if (content instanceof javax.mail.Message) {
            handleMessage(directory, (javax.mail.Message) content, emailFragments);
        } else if (content instanceof Multipart) {
            final Multipart mp2 = (Multipart) content;
            handleMultipart(directory, mp2, mailMessage, emailFragments);
        } else {
            throw new IllegalStateException("Content type not handled: " + content.getClass().getSimpleName());
        }
    }
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) Multipart(javax.mail.Multipart) ContentType(javax.mail.internet.ContentType) MessagingException(javax.mail.MessagingException) InputStream(java.io.InputStream) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ParseException(javax.mail.internet.ParseException) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 52 with Multipart

use of javax.mail.Multipart in project spring-integration-samples by spring-projects.

the class EmailParserUtils method handleMessage.

/**
 * Parses a mail message. The respective message can either be the root message
 * or another message that is attached to another message.
 *
 * If the mail message is an instance of {@link String}, then a {@link EmailFragment}
 * is being created using the email message's subject line as the file name,
 * which will contain the mail message's content.
 *
 * If the mail message is an instance of {@link Multipart} then we delegate
 * to {@link #handleMultipart(File, Multipart, javax.mail.Message, List)}.
 *
 * @param directory The directory for storing the message. If null this is the root message.
 * @param mailMessage The mail message to be parsed. Must not be null.
 * @param emailFragments Must not be null.
 */
public static void handleMessage(final File directory, final javax.mail.Message mailMessage, final List<EmailFragment> emailFragments) {
    Assert.notNull(mailMessage, "The mail message to be parsed must not be null.");
    Assert.notNull(emailFragments, "The collection of emailfragments must not be null.");
    final Object content;
    final String subject;
    try {
        content = mailMessage.getContent();
        subject = mailMessage.getSubject();
    } catch (IOException e) {
        throw new IllegalStateException("Error while retrieving the email contents.", e);
    } catch (MessagingException e) {
        throw new IllegalStateException("Error while retrieving the email contents.", e);
    }
    final File directoryToUse;
    if (directory == null) {
        directoryToUse = new File(subject);
    } else {
        directoryToUse = new File(directory, subject);
    }
    if (content instanceof String) {
        emailFragments.add(new EmailFragment(new File(subject), "message.txt", content));
    } else if (content instanceof Multipart) {
        Multipart multipart = (Multipart) content;
        handleMultipart(directoryToUse, multipart, mailMessage, emailFragments);
    } else {
        throw new IllegalStateException("This content type is not handled - " + content.getClass().getSimpleName());
    }
}
Also used : Multipart(javax.mail.Multipart) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) File(java.io.File)

Example 53 with Multipart

use of javax.mail.Multipart in project TranskribusCore by Transkribus.

the class SendMail method createMultiPartMessage.

public static Multipart createMultiPartMessage(String message, File[] atts) throws MessagingException {
    MimeBodyPart messageBodyPart = new MimeBodyPart();
    // fill message
    messageBodyPart.setText(message);
    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);
    // Part two is attachment
    messageBodyPart = new MimeBodyPart();
    for (File f : atts) {
        DataSource source = new FileDataSource(f);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(f.getName());
        multipart.addBodyPart(messageBodyPart);
    }
    return multipart;
}
Also used : MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) FileDataSource(javax.activation.FileDataSource) DataHandler(javax.activation.DataHandler) MimeBodyPart(javax.mail.internet.MimeBodyPart) File(java.io.File) FileDataSource(javax.activation.FileDataSource) DataSource(javax.activation.DataSource)

Example 54 with Multipart

use of javax.mail.Multipart in project common by zenlunatics.

the class MailLists method insertHeader.

private int insertHeader(Part part, String name, int type_flags) {
    try {
        String content_type = part.getContentType();
        if (content_type.startsWith("text/plain")) {
            if ((type_flags & 1) == 0) {
                String text = "posted by " + name;
                String line = "\n-----------\n";
                part.setContent(m_header_before ? text + line + part.getContent() : part.getContent() + "\n" + line + text, content_type);
            }
            return 1;
        }
        if (content_type.startsWith("text/html")) {
            if ((type_flags & 2) == 0) {
                String div = "<div style=\"background-color:#eee;border:solid 1px #ccc;margin-bottom:10px;padding:5px;width:100%;\">posted by " + name + "</div>";
                String html = (String) part.getContent();
                if (m_header_before) {
                    int i = html.indexOf("<body");
                    if (i != -1) {
                        i = html.indexOf(">", i + 5) + 1;
                        html = html.substring(0, i) + div + html.substring(i);
                    } else
                        html = div + html;
                } else {
                    int i = html.indexOf("</body");
                    if (i == -1)
                        html += div;
                    else
                        html = html.substring(0, i) + div + html.substring(i);
                }
                part.setContent(html, content_type);
            }
            return 2;
        }
        if (content_type.startsWith("multipart/")) {
            Multipart content = (Multipart) part.getContent();
            for (int i = 0; i < content.getCount() && type_flags != 3; i++) type_flags |= insertHeader(content.getBodyPart(i), name, type_flags);
            return type_flags;
        }
    } catch (IOException | MessagingException e) {
        e.printStackTrace();
    }
    return 0;
}
Also used : MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException)

Example 55 with Multipart

use of javax.mail.Multipart in project common by zenlunatics.

the class Mail method getHTMLMultipart.

// --------------------------------------------------------------------------
private Multipart getHTMLMultipart() throws MessagingException {
    if (m_content_html == null || m_html_images == null)
        return null;
    Multipart multipart = new MimeMultipart("related");
    MimeBodyPart body_part = new MimeBodyPart();
    body_part.setContent(m_content_html, "text/html");
    multipart.addBodyPart(body_part);
    for (File file : m_html_images) {
        body_part = new MimeBodyPart();
        body_part.setDataHandler(new DataHandler(new FileDataSource(file)));
        body_part.setHeader("Content-ID", file.getName());
        multipart.addBodyPart(body_part);
    }
    return multipart;
}
Also used : MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) FileDataSource(javax.activation.FileDataSource) DataHandler(javax.activation.DataHandler) MimeBodyPart(javax.mail.internet.MimeBodyPart) File(java.io.File)

Aggregations

Multipart (javax.mail.Multipart)148 MimeMultipart (javax.mail.internet.MimeMultipart)107 MimeBodyPart (javax.mail.internet.MimeBodyPart)92 MimeMessage (javax.mail.internet.MimeMessage)82 BodyPart (javax.mail.BodyPart)62 InternetAddress (javax.mail.internet.InternetAddress)61 MessagingException (javax.mail.MessagingException)58 Session (javax.mail.Session)44 DataHandler (javax.activation.DataHandler)42 IOException (java.io.IOException)37 Properties (java.util.Properties)37 Date (java.util.Date)30 Message (javax.mail.Message)30 FileDataSource (javax.activation.FileDataSource)27 DataSource (javax.activation.DataSource)24 InputStream (java.io.InputStream)23 File (java.io.File)21 Part (javax.mail.Part)21 Test (org.junit.Test)16 UnsupportedEncodingException (java.io.UnsupportedEncodingException)11