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