Convert .msg files to .pdf files

Hi, I'm trying to convert .msg files to pdf. I have done with converting an email, but I want to loop through all the attachments in the .msg file and if mails are attached, I would like to convert them to pdf also.

Here is my code so far:

Sub Main()
Dim attFileName As New List(Of String)()
'Convert MSG to MHTML
Dim myDir As String = "C:\2013 email\"
Dim mailMsg As Aspose.Email.Mail.MailMessage = Aspose.Email.Mail.MailMessage.Load(myDir + "email.msg")
Dim ms As New MemoryStream()
'Save as MHTML
mailMsg.Save(ms, Aspose.Email.Mail.SaveOptions.DefaultMhtml)
'Convert MHTML to PDF
'create an instance of LoadOptions and set the LoadFormat to Mhtml
Dim loadOptions = New Aspose.Words.LoadOptions()
loadOptions.LoadFormat = Aspose.Words.LoadFormat.Mhtml
'create an instance of Document and load the MTHML from MemoryStream
Dim document = New Aspose.Words.Document(ms, loadOptions)
'create an instance of HtmlSaveOptions and set the SaveFormat to Html
Dim saveOptions = New Aspose.Words.Saving.PdfSaveOptions()
'save the document to Html file
document.Save(myDir + "email.pdf", saveOptions)
'Extract attachments from MSG

Dim mailMsg1 As Aspose.Email.Mail.MailMessage = Aspose.Email.Mail.MailMessage.Load(myDir + "email.msg")
Dim attachment As Aspose.Email.Mail.Attachment

For Each attachment In mailMsg1.Attachments
If attachment.Name.Contains("*.xls*") Then
End If

If Not (attachment.Name.Contains("*.msg")) Then
attachment.Save(myDir + attachment.Name)
attFileName.Add(myDir + attachment.Name)
End If

Next
End Sub

The problem I'm facing is that the .msg files attached in the emails does not contain extension, so its not able to recognise if the attached file is .msg or not. Pls find the attached main email pdf with attachments name.

Any suggestion to handle this case?

Thanks in advance.

Hi Bharrgavi,


We are sorry for a delayed response.

This is an expected behavior where the message attachment’s extension is not available. But you can detect the media content of the attachment like follow to check if the attachment is a message file. Please try it at your end and share your feedback with us if you need further assistance.

Code:

if (msg.Attachments[0].ContentType.MediaType.Equals(“message/rfc822”))