use of com.aspose.email.Attachment in project iaf by ibissource.
the class MailConvertor method convert.
@Override
public void convert(MediaType mediaType, Message message, CisConversionResult result, String charset) throws Exception {
MailMessage eml = null;
try (InputStream inputStream = message.asInputStream(charset)) {
eml = MailMessage.load(inputStream, MEDIA_TYPE_LOAD_FORMAT_MAPPING.get(mediaType));
AttachmentCollection attachments = eml.getAttachments();
LOGGER.debug("cc : " + toString(eml.getCC()));
LOGGER.debug("bcc : " + toString(eml.getBcc()));
LOGGER.debug("sender : " + toString(eml.getSender()));
LOGGER.debug("from : " + toString(eml.getFrom()));
LOGGER.debug("to : " + toString(eml.getTo()));
LOGGER.debug("reversePath : " + toString(eml.getReversePath()));
LOGGER.debug("subject : " + eml.getSubject());
MhtSaveOptions options = MhtSaveOptions.getDefaultMhtml();
options.setMhtFormatOptions(MhtFormatOptions.HideExtraPrintHeader | MhtFormatOptions.WriteHeader | MhtFormatOptions.WriteCompleteBccEmailAddress | MhtFormatOptions.WriteCompleteCcEmailAddress | MhtFormatOptions.WriteCompleteEmailAddress | MhtFormatOptions.WriteCompleteFromEmailAddress | MhtFormatOptions.WriteCompleteToEmailAddress);
options.setPreserveOriginalDate(true);
// Overrules the default documentname.
result.setDocumentName(ConvertorUtil.createTidyNameWithoutExtension(eml.getSubject()));
File tempMHtmlFile = UniqueFileGenerator.getUniqueFile(getPdfOutputlocation(), this.getClass().getSimpleName(), null);
eml.getHeaders().set_Item("Date", new SimpleDateFormat(eMailHeaderDateFormat).format(eml.getDate()));
eml.save(tempMHtmlFile.getAbsolutePath(), options);
// Load the stream in Word document
HtmlLoadOptions loadOptions = new HtmlLoadOptions();
loadOptions.setLoadFormat(LoadFormat.MHTML);
loadOptions.setWebRequestTimeout(0);
Long startTime = new Date().getTime();
try (FileInputStream fis = new FileInputStream(tempMHtmlFile)) {
Document doc = new Document(fis, loadOptions);
new Fontsetter(cisConversionService.getFontsDirectory()).setFontSettings(doc);
resizeInlineImages(doc);
doc.save(result.getPdfResultFile().getAbsolutePath(), SaveFormat.PDF);
result.setNumberOfPages(getNumberOfPages(result.getPdfResultFile()));
Long endTime = new Date().getTime();
LOGGER.info("Conversion completed in " + (endTime - startTime) + "ms");
} finally {
Files.delete(tempMHtmlFile.toPath());
}
// Convert and (optional add) any attachment of the mail.
for (int index = 0; index < attachments.size(); index++) {
// Initialize Attachment object and Get the indexed Attachment reference
Attachment attachment = attachments.get_Item(index);
// Convert the attachment.
CisConversionResult cisConversionResultAttachment = convertAttachmentInPdf(attachment, result.getConversionOption());
// If it is an singlepdf add the file to the the current pdf.
if (ConversionOption.SINGLEPDF.equals(result.getConversionOption()) && cisConversionResultAttachment.isConversionSuccessfull()) {
try {
PdfAttachmentUtil pdfAttachmentUtil = new PdfAttachmentUtil(cisConversionResultAttachment, result.getPdfResultFile());
pdfAttachmentUtil.addAttachmentInSinglePdf();
} finally {
deleteFile(cisConversionResultAttachment.getPdfResultFile());
// Clear the file because it is now incorporated in the file it self.
cisConversionResultAttachment.setPdfResultFile(null);
cisConversionResultAttachment.setResultFilePath(null);
}
}
result.addAttachment(cisConversionResultAttachment);
}
}
}
Aggregations