Save EmailMessage to filesystem as .eml

Hi folks,

I use the EWS Managed API 2.0 assembly (Microsoft.Exchange.WebServices.dll) to retrieve mails from an specified user in AX2012 (R2) - application. This works fine.

Now i want to save the mails to filesystem as .eml.

I found this working C# function:

message.Load(new PropertySet(ItemSchema.MimeContent)); // message is type of Microsoft.Exchange.WebServices.Data.EmailMessage
MimeContent mimcon = message.MimeContent;
FileStream fStream = new FileStream("c:\temp\test.eml", FileMode.Create);
fStream.Write(mimcon.Content, 0, mimcon.Content.Length);
fStream.Close();

can anyone help me please to convert this into x++ code?

thanks!
best regards.

You can use .NET types directly in X++ with the help of .NET Interop from X++. There are some differences, such as that you have to use fully-qualified names and that AX doesn’t understand propertied (for example, use have to use message.get_MimeContent() instead of message.MimeContent).

Also don’t forget to catch CLR exceptions.

Thanks for the quick reply.

How can i handle the C# Statement ItemSchema.MimeContent to get the MimeContent-Property in x++?

There is no constructor for this Microsoft.Exchange.WebServices.Data.ItemSchema-Class to get an object of this type.

I see it’s a static property, so stop looking for a constructor. You need code like this: Microsoft.Exchange.WebServices.ItemSchema::get_MimeContent().

I have tried several combinations (like yours) but i always got the “syntax error” or “The method … has not been declared” in ax.

It seems that I can´t use this static property in x++.

There is no problem with static properties - I use them all the time. Please show us your code so we can see what’s wrong with it.

private Filename saveMailToFileSystem(Microsoft.Exchange.WebServices.Data.EmailMessage _emailMessage)
{
Microsoft.Exchange.WebServices.Data.MimeContent mimeContent;
Microsoft.Exchange.WebServices.Data.PropertySet propertySet;
System.IO.FileStream fileStream;
System.Byte[] byteArray;
Filename filename;
;

filename = @"c:\temp\" + guid2str(newGuid()) + ".eml";
propertySet = new Microsoft.Exchange.WebServices.Data.PropertySet(**Microsoft.Exchange.WebServices.Data.ItemSchema::get_MimeContent()**);
_emailMessage.Load(propertySet);

mimeContent = _emailMessage.get_MimeContent();
byteArray = mimeContent.get_Content();

fileStream = new System.IO.FileStream(filename, System.IO.FileMode::Create);
fileStream.Write(byteArray, 0, byteArray.get_Length());
fileStream.Close();<br>
return Filename;
}

The bold statement returns an error. (Method not declared)

I believe I see the problem - it seems that it’s a static field, not a property, and that’s really not supported. But nothing is lost - simply wrap your C# code in a class library, add the project to AOT, deploy it and call your library from X++ using .NET Interop.

Hey Martin, the solution with the class library works fine. Thank you!