use of microsoft.exchange.webservices.data.property.complex.FileAttachment in project nifi by apache.
the class ConsumeEWS method parseMessage.
public MimeMessage parseMessage(EmailMessage item, List<String> hdrIncludeList, List<String> hdrExcludeList) throws Exception {
EmailMessage ewsMessage = item;
final String bodyText = ewsMessage.getBody().toString();
MultiPartEmail mm;
if (ewsMessage.getBody().getBodyType() == BodyType.HTML) {
mm = new HtmlEmail().setHtmlMsg(bodyText);
} else {
mm = new MultiPartEmail();
mm.setMsg(bodyText);
}
mm.setHostName("NiFi-EWS");
// from
mm.setFrom(ewsMessage.getFrom().getAddress());
// to recipients
ewsMessage.getToRecipients().forEach(x -> {
try {
mm.addTo(x.getAddress());
} catch (EmailException e) {
throw new ProcessException("Failed to add TO recipient.", e);
}
});
// cc recipients
ewsMessage.getCcRecipients().forEach(x -> {
try {
mm.addCc(x.getAddress());
} catch (EmailException e) {
throw new ProcessException("Failed to add CC recipient.", e);
}
});
// subject
mm.setSubject(ewsMessage.getSubject());
// sent date
mm.setSentDate(ewsMessage.getDateTimeSent());
// add message headers
ewsMessage.getInternetMessageHeaders().getItems().stream().filter(x -> (hdrIncludeList == null || hdrIncludeList.isEmpty() || hdrIncludeList.contains(x.getName())) && (hdrExcludeList == null || hdrExcludeList.isEmpty() || !hdrExcludeList.contains(x.getName()))).forEach(x -> mm.addHeader(x.getName(), x.getValue()));
// Any attachments
if (ewsMessage.getHasAttachments()) {
ewsMessage.getAttachments().forEach(x -> {
try {
FileAttachment file = (FileAttachment) x;
file.load();
ByteArrayDataSource bds = new ByteArrayDataSource(file.getContent(), file.getContentType());
mm.attach(bds, file.getName(), "", EmailAttachment.ATTACHMENT);
} catch (MessagingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
});
}
mm.buildMimeMessage();
return mm.getMimeMessage();
}
Aggregations