Converting .eml to Outlook.MailItem and back loses inline images

I would like to create a new outlook Message from an .eml template. I have chosen to do this by converting .eml to .msg first and then creating a new Outlook.MailItem with the following code

FileName = "test.eml"

Dim MsgTemplateName As String = Path.GetTempFileName
Dim MsgTemplate As MailMessage = MailMessage.Load(FileName)

MsgTemplate.Save(MsgTemplateName, MessageFormat.Msg)

Dim Item as Outlook.MailItem = _OutlookApp.CreateItemFromTemplate(MsgTemplateName)

Now after this I capture the send event and convert the MailItem back to .eml Format. I need to do this because I want to send the message via a different SMTP Server, not the one the Outlook Application is connected to:

Dim MsgTemplateName As String = Path.GetTempFileName

Item.SaveAs(MsgTemplateName, Outlook.OlSaveAsType.olMSG)

Dim Msg As MapiMessage = MapiMessage.FromFile(MsgTemplateName)
Dim MsgInterpretor As MailMessageInterpretor = MailMessageInterpretorFactory.Instance.GetIntepretor(Msg.MessageClass)
Dim Eml As MailMessage = MsgInterpretor.Interpret(Msg)

Eml.Save(_TemplateFileName)

However, during this process the resulting .eml file seems to lose the MIME information of its inline images like so:

------=_NextPart_001_000C_01C22027.C6636010
Content-Type: text/html; charset=“iso-8859-1"
Content-Transfer-Encoding: quoted-printable


<img src=3D"cid:test” height=3D"451" width=3D"600">

------=NextPart_001_000C_01C22027.C6636010–

------=NextPart_000_000B_01C22027.C6636010
Content-Type: image/gif;
name="test.gif"
Content-Transfer-Encoding: base64
Content-ID: etc.

becomes:

Content-Type: application/octet-stream

Also, the subject =?iso-8859-1?Q?Fr=F6hliche_Weihnachten?= is replaced by:

Subject:
=?utf-32?Q?F=00=00=00r=00=00=00=F6=00=00=00h=00=00=00l=00=00=00i=00=00=00?=
=?utf-32?Q?c=00=00=00h=00=00=00e=00=00=00=00=00=00W=00=00=00e=00=00=00i?=
=?utf-32?Q?=00=00=00h=00=00=00n=00=00=00a=00=00=00c=00=00=00h=00=00=00t?=
=?utf-32?Q?=00=00=00e=00=00=00n=00=00=00?=Subject:
=?utf-32?Q?F=00=00=00r=00=00=00=F6=00=00=00h=00=00=00l=00=00=00i=00=00=00?=
=?utf-32?Q?c=00=00=00h=00=00=00e=00=00=00=00=00=00W=00=00=00e=00=00=00i?=
=?utf-32?Q?=00=00=00h=00=00=00n=00=00=00a=00=00=00c=00=00=00h=00=00=00t?=
=?utf-32?Q?=00=00=00e=00=00=00n=00=00=00?=

Is this because I am doing the conversion in the wrong way? Or does it have to do with Outlook and can’t be worked around? I’d be most thankful for any help, thanks in advance!

With regards,

Guido

Hi Guido,


Thank you for your inquiry and your sample code.

It seems that you are using the Outlook.MailItem object just to convert the message to MSG format (MapiMessage object). I think there is no need of this conversion, cause you can load a MailMessage object directly into an instance of MapiMessage. Please check the below source code for your reference,

[VB]
Dim mailMessage = New Aspose.Email.Mail.MailMessage()
Dim mapiMessage = Aspose.Email.Outlook.MapiMessage.FromMailMessage(mailMessage)

I also think that you do not have to convert your email message to EML format for transmitting it via SmptClient. Please check the below linked technical article for sending the messages using SmtpClient class of Aspose.Email for .NET API.
http://www.aspose.com/documentation/.net-components/aspose.email-for-.net/sending-emails.html

If conversion to EML format is mandatory in your application design then please share a sample message file for your review.

Hi Babar,

thanks for the quick response, I realize I have ommitted some important facts about the application I am trying to write. Firstly, the Outlook.MailItem is displayed to the user before converting it back to .eml, so that he can make changes - I was of the opinion that you can't do that with the Aspose MailMessage Object, is that right?

Using SmtpClient Class is a good idea, I will look into this option, however, the mail sending part of my application is a fairly large bit of code and it is in fact working quite satisfactory - replacing this could result in much work and I would prefer not to change a running system ;-)

As you say - maybe the conversion problem really simply has to do with my source .eml file. This file is generated from plain html, and it could be possible that this process isn't 100% correct. Would you be so kind as to have a look at the files that I have attached to this post?

The file Test.eml is a minimal example, the other file contains a more sophisticated message. Both source files get changed during the conversion. The resulting emails then cannot be viewed correctly for instance on GMX, because the MIME type isnt correct.

Thanks very much

Guido

Hi Guido,


Thank you for the elaboration.

I have tried converting the attached EML files to MHTML using the Aspose.Email for .NET API and the resultant message came out just fine (attached for your reference). If the EML files that you have shared are the output of your conversion process then please share the input files. I will test them with Aspose.Email for .NET API as well.

Please note that you can save a MailMessage instance as a Draft, so that the user can edit the message on their end. Check out the below source code for your reference.

[VB.NET]
Dim msg As New MailMessage()
msg.Subject = “SUBJECT”
msg.HtmlBody = “BODY”

'save the message to stream in OutlookMessageFormatUnicode
msg.Save(stream, MailMessageSaveType.OutlookMessageFormatUnicode)

'reload the previosuly saved message into an instance of MapiMessage
Dim mapiMsg As MapiMessage = MapiMessage.FromStream(stream)

‘set message flag to un-sent (draft status)
mapiMsg.SetMessageFlags(MapiMessageFlags.MSGFLAG_UNSENT)

’ save it
mapiMsg.Save(“output.msg”)

Hi Babar,

Thanks for checking my .eml templates, so they seem to be ok. However, I still don't know why opening and saving the .msg file with the .NET method Outlook.CreateItemFromTemplate changes the inline images.

I have tried your code, however, now the image doesn't display in the outlook item at all, but only when it is opened via CreateItemFromTemplate:

Dim MsgTemplateName As String = Path.GetTempFileName
Dim MsgTemplate As MailMessage = MailMessage.Load(FileName)
Dim MsgStream As New MemoryStream

MsgTemplate.Save(MsgStream, MailMessageSaveType.OutlookMessageFormatUnicode)

Dim MAPIMessage As MapiMessage = MAPIMessage.FromStream(MsgStream)

MAPIMessage.SetMessageFlags(MapiMessageFlags.MSGFLAG_UNSENT)
MAPIMessage.Save(MsgTemplateName)

Item = _OutlookApp.CreateItemFromTemplate(MsgTemplateName)

So I now suspect that the problem actually lies in the outlook method, which does not interpret the .msg file correctly. Do you propose any different way of opening the .msg file in an outlook inspector window so that the user can edit it? OLE isn't sufficient here because I need a handle on the inspector window...

Thanks in advance,

Guido

Hi,


Thank you for your feedback.

I am afraid that I am not aware of “Outlook Inspector” otherwise I would have assisted you or at-least suggested you some workaround for your situation. I will try to work with Outlook Inspector window in some future, to get the know how of this control.

Please let us know if there is something else that we can help you with, regarding the Aspose.Email for .NET component,

Regards,

Hi Babar,

Ah, yes, I have in fact omitted this information - sorry! I want to display the Mail to the user in an outlook window so that he can make his changes there and then convert it back to .eml. You will probably need to see this additional declaration code to understand:

Dim _OutlookApp As Outlook.Application = GetObject(, "Outlook.Application")

Dim _OutlookNamespace As Outlook.NameSpace = _OutlookApp.GetNamespace("MAPI")

Dim Item As Outlook.MailItem

_OutlookNamespace.Logon()

Later on, I call Item.Display(), let the user make his changes and then I capture the item_send event in order to get back the message. I am using the reference Microsoft.Office.Interop.Outlook Version 12.0.0.0.

Instead of CreateItemFromTemplate I have in the mean time also tried to open the .msg file with

_OutlookNamespace.OpenSharedItem()

which also doesnt seem to work, in this case the Image is missing, too. If you have any workaround for me, this would be really great!

Thanks in advance,

Guido