Search in sources :

Example 1 with ContentType

use of jakarta.mail.internet.ContentType in project eclipselink by eclipse-ee4j.

the class XMLBinaryDataHelper method getBytesFromMultipart.

public EncodedData getBytesFromMultipart(MimeMultipart value, Marshaller marshaller) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try {
        ContentType contentType = new ContentType(value.getContentType());
        String boundary = contentType.getParameter("boundary");
        output.write(Constants.cr().getBytes());
        output.write(("Content-Type: " + contentType.getBaseType() + "; boundary=\"" + boundary + "\"\n").getBytes());
    } catch (Exception ex) {
        throw ConversionException.couldNotBeConverted(value, byte[].class, ex);
    }
    try {
        value.writeTo(output);
    } catch (Exception ex) {
        throw ConversionException.couldNotBeConverted(value, byte[].class, ex);
    }
    return new EncodedData(output.toByteArray(), value.getContentType());
}
Also used : ContentType(jakarta.mail.internet.ContentType) ByteArrayOutputStream(java.io.ByteArrayOutputStream) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException) IOException(java.io.IOException) ConversionException(org.eclipse.persistence.exceptions.ConversionException)

Example 2 with ContentType

use of jakarta.mail.internet.ContentType in project simple-java-mail by bbottema.

the class MimeMessageHelperTest method filenameWithSpaceEncoding.

@Test
public void filenameWithSpaceEncoding() throws IOException, MessagingException {
    final String fileName = "file name.txt";
    final Email email = EmailHelper.createDummyEmailBuilder(true, true, false, false, false, false).clearAttachments().withAttachment(fileName, "abc".getBytes(), "text/plain").buildEmail();
    final MimeMessage mimeMessage = EmailConverter.emailToMimeMessage(email);
    final BodyPart bodyPart = ((MimeMultipart) mimeMessage.getContent()).getBodyPart(1);
    ContentType ct = new ContentType(bodyPart.getHeader("Content-Type")[0]);
    assertThat(ct.getParameter("filename")).isEqualTo(fileName);
    assertThat(bodyPart.getFileName()).isEqualTo(fileName);
}
Also used : BodyPart(jakarta.mail.BodyPart) Email(org.simplejavamail.api.email.Email) ContentType(jakarta.mail.internet.ContentType) MimeMessage(jakarta.mail.internet.MimeMessage) MimeMultipart(jakarta.mail.internet.MimeMultipart) Test(org.junit.Test)

Example 3 with ContentType

use of jakarta.mail.internet.ContentType 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 jakarta.mail.Message}.
 *
 * Will create the respective {@link EmailFragment}s representing those attachments.
 *
 * Instances of {@link jakarta.mail.Message} are delegated to
 * {@link #handleMessage(File, jakarta.mail.Message, List)}. Further instances
 * of {@link Multipart} are delegated to
 * {@link #handleMultipart(File, Multipart, jakarta.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, jakarta.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 jakarta.mail.Message) {
            handleMessage(directory, (jakarta.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(jakarta.mail.internet.MimeBodyPart) BodyPart(jakarta.mail.BodyPart) Multipart(jakarta.mail.Multipart) ContentType(jakarta.mail.internet.ContentType) MessagingException(jakarta.mail.MessagingException) InputStream(java.io.InputStream) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ParseException(jakarta.mail.internet.ParseException) MimeBodyPart(jakarta.mail.internet.MimeBodyPart)

Example 4 with ContentType

use of jakarta.mail.internet.ContentType in project org.openntf.xsp.jakartaee by OpenNTF.

the class text_plain method getCharset.

private String getCharset(String type) {
    try {
        ContentType ct = new ContentType(type);
        String charset = ct.getParameter("charset");
        if (charset == null)
            // If the charset parameter is absent, use US-ASCII.
            charset = "us-ascii";
        return MimeUtility.javaCharset(charset);
    } catch (Exception ex) {
        return null;
    }
}
Also used : ContentType(jakarta.mail.internet.ContentType) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

ContentType (jakarta.mail.internet.ContentType)4 IOException (java.io.IOException)3 BodyPart (jakarta.mail.BodyPart)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 MessagingException (jakarta.mail.MessagingException)1 Multipart (jakarta.mail.Multipart)1 MimeBodyPart (jakarta.mail.internet.MimeBodyPart)1 MimeMessage (jakarta.mail.internet.MimeMessage)1 MimeMultipart (jakarta.mail.internet.MimeMultipart)1 ParseException (jakarta.mail.internet.ParseException)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ConversionException (org.eclipse.persistence.exceptions.ConversionException)1 XMLMarshalException (org.eclipse.persistence.exceptions.XMLMarshalException)1 Test (org.junit.Test)1 Email (org.simplejavamail.api.email.Email)1