Search in sources :

Example 46 with Multipart

use of javax.mail.Multipart in project scout.rt by eclipse.

the class MailHelperTest method createMimeMessageUsingUnknownEncoding.

protected MimeMessage createMimeMessageUsingUnknownEncoding() throws IOException, MessagingException {
    MailMessage definition = new MailMessage().withBodyPlainText("a");
    MimeMessage mimeMessage = BEANS.get(MailHelper.class).createMimeMessage(definition);
    Object multipart0 = mimeMessage.getContent();
    Assert.assertTrue(multipart0 instanceof Multipart);
    Multipart multipart = (Multipart) multipart0;
    BodyPart plaintextPart = multipart.getBodyPart(0);
    plaintextPart.setHeader(MailHelper.CONTENT_TYPE_ID, "text/plain; charset=\"" + RARE_UNKNOWN_CHARSET + "\"");
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    mimeMessage.writeTo(bos);
    mimeMessage = new MimeMessage(null, new ByteArrayInputStream(bos.toByteArray()));
    return mimeMessage;
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) MimeMessage(javax.mail.internet.MimeMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 47 with Multipart

use of javax.mail.Multipart in project scout.rt by eclipse.

the class MailHelper method createBodyPart.

protected BodyPart createBodyPart(String bodyTextPlain, String bodyTextHtml) throws MessagingException {
    if (!StringUtility.isNullOrEmpty(bodyTextPlain) && !StringUtility.isNullOrEmpty(bodyTextHtml)) {
        // multipart
        MimeBodyPart plainPart = createSingleBodyPart(bodyTextPlain, CONTENT_TYPE_TEXT_PLAIN);
        MimeBodyPart htmlPart = createSingleBodyPart(bodyTextHtml, CONTENT_TYPE_TEXT_HTML);
        Multipart multiPart = new MimeMultipart("alternative");
        multiPart.addBodyPart(plainPart);
        multiPart.addBodyPart(htmlPart);
        MimeBodyPart multiBodyPart = new MimeBodyPart();
        multiBodyPart.setContent(multiPart);
        return multiBodyPart;
    } else if (!StringUtility.isNullOrEmpty(bodyTextPlain)) {
        return createSingleBodyPart(bodyTextPlain, CONTENT_TYPE_TEXT_PLAIN);
    } else if (!StringUtility.isNullOrEmpty(bodyTextHtml)) {
        return createSingleBodyPart(bodyTextHtml, CONTENT_TYPE_TEXT_HTML);
    }
    return null;
}
Also used : Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Example 48 with Multipart

use of javax.mail.Multipart in project scout.rt by eclipse.

the class MailHelper method addAttachmentsToMimeMessage.

/**
 * Adds the provided attachments to the existing mime message.
 * <p>
 * When working with {@link BinaryResource}, use {@link #addResourcesAsAttachments(MimeMessage, List)} instead.
 *
 * @param msg
 *          Mime message to attach files to
 * @param attachments
 *          List of attachments (files).
 * @since 4.1
 */
public void addAttachmentsToMimeMessage(MimeMessage msg, List<File> attachments) {
    if (CollectionUtility.isEmpty(attachments)) {
        return;
    }
    try {
        Multipart multiPart = prepareMessageForAttachments(msg);
        for (File attachment : attachments) {
            MimeBodyPart bodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource(attachment);
            bodyPart.setDataHandler(new DataHandler(source));
            bodyPart.setFileName(MimeUtility.encodeText(attachment.getName(), "UTF-8", null));
            multiPart.addBodyPart(bodyPart);
        }
        msg.saveChanges();
    } catch (MessagingException e) {
        throw new ProcessingException("Failed to add attachment to existing mime message", e);
    } catch (IOException e) {
        throw new ProcessingException("Failed to add attachment to existing mime message", e);
    }
}
Also used : Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) MessagingException(javax.mail.MessagingException) FileDataSource(javax.activation.FileDataSource) DataHandler(javax.activation.DataHandler) IOException(java.io.IOException) MimeBodyPart(javax.mail.internet.MimeBodyPart) File(java.io.File) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) FileDataSource(javax.activation.FileDataSource) DataSource(javax.activation.DataSource) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Example 49 with Multipart

use of javax.mail.Multipart in project scout.rt by eclipse.

the class MailHelper method collectMailParts.

/**
 * Collects the body, attachment and inline attachment parts from the provided part.
 * <p>
 * A single collector can be null in order to collect only the relevant parts.
 *
 * @param part
 *          Part
 * @param bodyCollector
 *          Body collector (optional)
 * @param attachmentCollector
 *          Attachment collector (optional)
 * @param inlineAttachmentCollector
 *          Inline attachment collector (optional)
 */
public void collectMailParts(Part part, List<Part> bodyCollector, List<Part> attachmentCollector, List<Part> inlineAttachmentCollector) {
    if (part == null) {
        return;
    }
    try {
        String disp = part.getDisposition();
        if (disp != null && disp.equalsIgnoreCase(Part.ATTACHMENT)) {
            if (attachmentCollector != null) {
                attachmentCollector.add(part);
            }
        } else {
            Object content = null;
            try {
                // NOSONAR
                checkValidCharset(part);
                // getContent might throw a MessagingException for legitimate parts (e.g. some images end up in a javax.imageio.IIOException for example).
                content = part.getContent();
            } catch (MessagingException | IOException e) {
                LOG.info("Unable to mime part content", e);
            }
            if (content instanceof Multipart) {
                Multipart multiPart = (Multipart) part.getContent();
                for (int i = 0; i < multiPart.getCount(); i++) {
                    collectMailParts(multiPart.getBodyPart(i), bodyCollector, attachmentCollector, inlineAttachmentCollector);
                }
            } else {
                if (part.isMimeType(CONTENT_TYPE_TEXT_PLAIN)) {
                    if (bodyCollector != null) {
                        bodyCollector.add(part);
                    }
                } else if (part.isMimeType(CONTENT_TYPE_TEXT_HTML)) {
                    if (bodyCollector != null) {
                        bodyCollector.add(part);
                    }
                } else if (part.isMimeType(CONTENT_TYPE_MESSAGE_RFC822) && part.getContent() instanceof MimeMessage) {
                    // its a MIME message in rfc822 format as attachment therefore we have to set the filename for the attachment correctly.
                    if (attachmentCollector != null) {
                        MimeMessage msg = (MimeMessage) part.getContent();
                        String filteredSubjectText = StringUtility.filterText(msg.getSubject(), "a-zA-Z0-9_-", "");
                        String fileName = (StringUtility.hasText(filteredSubjectText) ? filteredSubjectText : "originalMessage") + ".eml";
                        RFCWrapperPart wrapperPart = new RFCWrapperPart(part, fileName);
                        attachmentCollector.add(wrapperPart);
                    }
                } else if (disp != null && disp.equals(Part.INLINE)) {
                    if (inlineAttachmentCollector != null) {
                        inlineAttachmentCollector.add(part);
                    }
                } else {
                    String[] headerContentId = part.getHeader(CONTENT_ID_ID);
                    if (headerContentId != null && headerContentId.length > 0 && StringUtility.hasText(headerContentId[0]) && part.getContentType() != null && part.getContentType().startsWith(CONTENT_TYPE_IMAGE_PREFIX)) {
                        if (inlineAttachmentCollector != null) {
                            inlineAttachmentCollector.add(part);
                        }
                    } else if (part.getFileName() != null) /* assumption: file name = attachment (last resort) */
                    {
                        if (attachmentCollector != null) {
                            attachmentCollector.add(part);
                        }
                    } else {
                        LOG.debug("Unknown mail message part, headers: [{}]", part.getAllHeaders());
                    }
                }
            }
        }
    } catch (MessagingException | IOException e) {
        throw new ProcessingException("Unexpected: ", e);
    }
}
Also used : Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) MessagingException(javax.mail.MessagingException) MimeMessage(javax.mail.internet.MimeMessage) IOException(java.io.IOException) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Example 50 with Multipart

use of javax.mail.Multipart in project scout.rt by eclipse.

the class MailHelper method addResourcesAsAttachments.

/**
 * Adds the provided attachments to the existing mime message.
 * <p>
 * When working with {@link File}, use {@link #addAttachmentsToMimeMessage(MimeMessage, List)} instead.
 *
 * @param msg
 *          Mime message to attach files to
 * @param attachments
 *          List of attachments (binary resources).
 * @since 6.0
 */
public void addResourcesAsAttachments(MimeMessage msg, List<BinaryResource> attachments) {
    if (CollectionUtility.isEmpty(attachments)) {
        return;
    }
    try {
        Multipart multiPart = prepareMessageForAttachments(msg);
        for (BinaryResource attachment : attachments) {
            MimeBodyPart bodyPart = new MimeBodyPart();
            DataSource source = new BinaryResourceDataSource(attachment);
            bodyPart.setDataHandler(new DataHandler(source));
            bodyPart.setFileName(MimeUtility.encodeText(attachment.getFilename(), "UTF-8", null));
            multiPart.addBodyPart(bodyPart);
        }
        msg.saveChanges();
    } catch (MessagingException e) {
        throw new ProcessingException("Failed to add attachment to existing mime message", e);
    } catch (IOException e) {
        throw new ProcessingException("Failed to add attachment to existing mime message", e);
    }
}
Also used : Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) BinaryResource(org.eclipse.scout.rt.platform.resource.BinaryResource) MessagingException(javax.mail.MessagingException) DataHandler(javax.activation.DataHandler) IOException(java.io.IOException) MimeBodyPart(javax.mail.internet.MimeBodyPart) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) FileDataSource(javax.activation.FileDataSource) DataSource(javax.activation.DataSource) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

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