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());
}
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);
}
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());
}
}
}
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;
}
}
Aggregations