Free Support Forum - aspose.com

MSG to MHTML conversion - inline images are not preserved

Hi,

we are using aspose.Email 3.3. when the attached msg file is converted into mhtml file, inline images/embedded images present in the msg source file, are not rendered in the target mhtml file.

Could you please let us know how should we preserve inline image in the target mhtml file …? we could see only see red cross mark in mhtml file instead of images. can you please let us know why these inline images are not preserved during msg to mhhtml conversion ?

Thanks & Regards,
Thirumurthy R

Hi Thirumurthy,

We regret the inconvenience caused to you in this regard.

After an initial investigation, I was able to observe this issue at my end using the latest version of Aspose.Email for Java 3.5.0 and have logged it as NETWRKJAVA-33326 in our bug tracking system for further investigation by the development team. I have also linked this thread with the logged ticket so that once the issue is fixed, you'll be notified here.


Hi Iqbal,

when we tried for msg (with attachments) to pdf conversion, the attachment contents are not rendered into the PDF. Only the
attachment file name is rendererd and I cannot find any option to render
the contents of the attachment into the PDF.

Could you please let us know how should we render the attachment contents into the PDF file ?

Thanks & Regards,
Thirumurthy R

Hi Thirumurthy,


Aspose.Email can include outline images of type images in the output MHTML file. If the attachment is another regular attachment (like word,excel, pdf, etc.), it will not become part of the output MHTML file. Could you please share more details about the attachment contents that you want to render to the output MHTML?

The issues you have found earlier (filed as NETWRKJAVA-33326) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.

Hi Iqbal,

During msg (with attachments) to pdf conversion, the attachment contents are not rendered into the PDF. mhtml is the intermediate format before generating the PDF. Only the
attachment file name is rendererd and I couldn’t find any option to render
the contents of the attachment into the PDF. Please see the attachment which explains you what exactly we are looking for.

Please let us know if there is a possibility to render
the contents of the attachment into the PDF ?

Thanks & Regards,
Thirumurthy R

Hi Thirumurthy,


I would like to share that PDF is generated using the intermediate mhtml created by Aspose.Email and also Aspose.Email can include only image type attachments in the output MHTML, as mentioned in my previous post.

Therefore, I am afraid to inform that DOCX attachment cannot be rendered to the MHTML and hence the PDF file.

Please feel free to write us back if you have any other query related to Aspose.Email.

Hi Iqbal,

Thanks for your reply. we would like to know that is there any other way to achieve the above said requirements. I meant using any other aspose component (aspose words, aspose PDF) , can we achieve the above said requirements during msg to PDF conversion ?

Thanks & Regards,
Thirumurthy R

Hi Thirumurthy,

I am afraid to share that such requirements can't be met individually by any other Aspose prodcut. However, you can use Aspose.Email and Aspose.Words (since the attachment in this case is a Word document) to achieve the desired results as shown in the code sample below. Please give it a try at your end and let us know your feedback then.

Sample Code:

MailMessage msg = MailMessage.load("RE fdg.msg");

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

msg.save(outputStream, MailMessageSaveType.getMHtmlFormat());

Document doc = new Document(new ByteArrayInputStream(outputStream.toByteArray()));

outputStream.reset();

//now extract the doc attachment from message

Attachment att = msg.getAttachments().get(0);

att.save(outputStream);

Document docAtt = new Document(new ByteArrayInputStream(outputStream.toByteArray()));

//now combine both documents

doc.appendDocument(docAtt, ImportFormatMode.KEEP_SOURCE_FORMATTING);

//and save as PDF

doc.save("Combined.pdf", SaveFormat.PDF);


Hi Iqbal,

Using the above sample code, i was able to render the email attachments(word) into PDF. There is no data loss but some fonts are not rendered properly in the output PDF. I am attaching the output PDF for your reference. please see the signature part on the PDF file. it is not same as in the msg file.

we have observed that above sample code only works if the attachment is word file. but attachments can be anything as word, pdf, msg ,etc., how should we handle if the attachment is PDF or some other format (ppt, excel,msg.,). Please advise how to achieve the desired results regardless of the attachment type and how should we make it work for all type of attachments ?

Thanks & Regards,
Thirumurthy R

Hi Thirumurthy,

Thank you for providing the feedback.

It seems that you are using some old version of either Aspose.Email or Aspose.Words as I am unable to reproduce the issue of garbage characters in the signature using the latest version of Aspose.Email for Java 3.7.1 and Aspose.Words for Java 13.12.0 (see attachment for your reference). Please use these versions in your sample code and let us know if you still face the same issues.

With respect to your concerns about handling different type of attachments such as MSG, Excel, PDF, Docx, etc, this can be achieved using similar technique as mentioned earlier, but will include other Aspose products as well i.e. Aspose.Cells and Aspose.Pdf. Aspose.Cells provides the facility to convert excel files to PDF, while Aspose.Pdf provides teh support of concatenating multiple PDFs from files or streams. So, in order to achieve your objective, you may proceed as follow:

  1. Convert MSG and attachment Docs to PDF stream as mentioned in the above code sample
  2. Convert Excel files to PDF using Aspsoe.Cells and get the output in stream
  3. Load any attachment PDF in streams using Aspose.Pdf
  4. Concatenate all the streams using Aspose.PDF and save the overall output
I hope this will help in fulfilling your requirements. Please feel free to contact us in case you have any additional query/inquiry in this regard.

Hi Iqbal,

Thanks for your detailed explanation. It would be great if you share the sample code for supporting all the attachment type. I am attaching the input msg file which has 2 pdf files and one msg file as its attachment. could you please share the sample code for converting the attached msg file into PDF and help us to achieve the desired results ?

Thanks & Regards,
Thirumurthy R

Hi Thirumurthy,


Please have a look at the following sample code for your requirements. Please note that you will need to have Aspose.Email for Java, Aspose.Words for Java, Aspose.Cells for Java and Aspose.PDF for Java included in your application. Please give it a try at your end and let us know your feedback.

Sample Code:

public static void main(String[] args) throws Exception {<o:p></o:p>

// TODO Auto-generated method stub

String strLicenseFileName = "Aspose.Total.Product.Family.EMC.lic"; //customer license

strLicenseFileName = "Aspose.Total.Java.lic"; //own license

com.aspose.email.License lic = new com.aspose.email.License();

lic.setLicense("C:\\Users\\Kashif\\Desktop\\Aspose Licences\\" + strLicenseFileName);

com.aspose.pdf.License lic2 = new com.aspose.pdf.License();

lic2.setLicense("C:\\Users\\Kashif\\Desktop\\Aspose Licences\\" + strLicenseFileName);

com.aspose.words.License lic3 = new com.aspose.words.License();

lic3.setLicense("C:\\Users\\Kashif\\Desktop\\Aspose Licences\\" + strLicenseFileName);

com.aspose.cells.License lic4 = new com.aspose.cells.License();

lic4.setLicense("C:\\Users\\Kashif\\Desktop\\Aspose Licences\\" + strLicenseFileName);

com.aspose.pdf.Document pFinalDoc = new com.aspose.pdf.Document();

ParseMessage(MailMessage.load("msg to pdf converssion_need sample code .msg"), pFinalDoc);

pFinalDoc.save("Final.pdf");

System.out.println("Done..");

}

private static void ParseMessage(MailMessage message, com.aspose.pdf.Document FinalPdf) throws Exception

{

System.out.println("Subject: " + message.getSubject());

System.out.println("Extracting attachments....");

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

message.save(outputStream, MailMessageSaveType.getMHtmlFormat());

com.aspose.words.Document doc = new com.aspose.words.Document(new ByteArrayInputStream(outputStream.toByteArray()));

outputStream.reset();

doc.save(outputStream, SaveFormat.PDF);

com.aspose.pdf.Document savedPdf = new com.aspose.pdf.Document(new ByteArrayInputStream(outputStream.toByteArray()));

FinalPdf.getPages().add(savedPdf.getPages());

for (int i = 0; i < message.getAttachments().size(); i++)

{

Attachment att = (Attachment) message.getAttachments().get(i);

System.out.println("Attachment Name: " + att.getName());

if (att.getContentType().getMediaType().equals("message/rfc822"))

{

MailMessage msg = MailMessage.load(att.getContentStream());

//recursively parse the MSG attachments
ParseMessage(msg, FinalPdf);

}

else if (att.getName().contains(".docx")||att.getName().contains(".doc"))

{

com.aspose.words.Document wDoc = new com.aspose.words.Document(att.getContentStream());

ByteArrayOutputStream ostream = new ByteArrayOutputStream();

wDoc.save(ostream, SaveFormat.PDF); //save word as PDF

//now load this PDF in Aspose.PDF and append to the Final PDF

com.aspose.pdf.Document docPdf = new com.aspose.pdf.Document(new ByteArrayInputStream(ostream.toByteArray()));

FinalPdf.getPages().add(docPdf.getPages());

}

else if (att.getName().contains(".pdf"))

{

com.aspose.pdf.Document attPdf = new com.aspose.pdf.Document(att.getContentStream());

FinalPdf.getPages().add(attPdf.getPages());

}

else if (att.getName().contains(".xls") || att.getName().contains(".xlsx"))

{

Workbook workbook = new Workbook(att.getContentStream());

ByteArrayOutputStream ostream = new ByteArrayOutputStream();

workbook.save(ostream, SaveFormat.PDF); //save Excel as PDF

//now load this PDF in Aspose.PDF and append to the Final PDF

com.aspose.pdf.Document wbPdf = new com.aspose.pdf.Document(new ByteArrayInputStream(ostream.toByteArray()));

FinalPdf.getPages().add(wbPdf.getPages());

}

}

}


Hi Iqbal,

Thanks for your prompt response. I have tested with above sample code and it works.By using above sample code, we are adding/merging all the attachment contents into PDF. similarly is there any way to add attachments into PDF ? I meant embedding all the attachments into the PDF. I think, its possible with aspose PDF. can you please let us know whats needs to be modified in the above sample code to add all the attachments into PDF ? output PDF file should looks like.same as the one attached here. Please have a look onto the attached PDF and let us know how to generate the desired output PDF ?

Thanks & Regards,
Thirumurthy R

Hi Thirumurthy,


I have analyzed your PDF and am afraid to inform that Aspose.Email cannot create customized PDF as per your requirements. However, this can be achieved by using Aspose.PDF where a PDF can be created from scratch with required formatting and contents. Could you please post your query at Aspose.Pdf Product Family forum, where our concerned support team will assist you to create required PDF?

Please feel free to write us back if you have any other query related to Aspose.Email. We will be glad to provide you assistance as soon as possible.