Search in sources :

Example 1 with MailMessage

use of com.aspose.email.MailMessage 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);
        }
    }
}
Also used : MailMessage(com.aspose.email.MailMessage) MhtSaveOptions(com.aspose.email.MhtSaveOptions) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Attachment(com.aspose.email.Attachment) AttachmentCollection(com.aspose.email.AttachmentCollection) Document(com.aspose.words.Document) Date(java.util.Date) FileInputStream(java.io.FileInputStream) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) HtmlLoadOptions(com.aspose.words.HtmlLoadOptions) CisConversionResult(nl.nn.adapterframework.extensions.aspose.services.conv.CisConversionResult)

Aggregations

Attachment (com.aspose.email.Attachment)1 AttachmentCollection (com.aspose.email.AttachmentCollection)1 MailMessage (com.aspose.email.MailMessage)1 MhtSaveOptions (com.aspose.email.MhtSaveOptions)1 Document (com.aspose.words.Document)1 HtmlLoadOptions (com.aspose.words.HtmlLoadOptions)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 CisConversionResult (nl.nn.adapterframework.extensions.aspose.services.conv.CisConversionResult)1