Create .msg file - cant set date

Hi,

I am migrating emails stored in various formats (.htm, rtf, .txt) from a legacy document management system, so I am reconstituting them into .msg files. The emails are not stored on an exchange server, only in the legacy system. The emails will be loaded directly into Sharepoint, so not going into exchange at all.

When I try the following trivial test, the Date field (i.e. sent date) is not being set, so when I open the .msg file (with an outlook client) there is no sent date information. Also pressing ALT-ENTER to show the properties of the email shows no sent date.

Can anyone please help?

MailMessage mailMsg = new MailMessage();
mailMsg.From = "FromMe@demo.com";
mailMsg.To = "ToYou@demo.com";
mailMsg.Subject = “my subject”;
mailMsg.TextBody = “my body”;
mailMsg.Date = DateTime.Today;

MapiMessage outlookMsg = MapiMessage.FromMailMessage(mailMsg);
outlookMsg.SetMessageFlags(MapiMessageFlags.MSGFLAG_UNMODIFIED);

string strMsgFile = @“E:\tmp\sample.msg”;
outlookMsg.Save(strMsgFile);

Thanks.


Hi Lyndon,

Thank you for writing to Aspose support team.

Aspose.Email provides facility to set different date/time properties of MapiMessage which are used by Outlook to display the dates. Please give a try to the following sample code, which sets properties like PR_CLIENT_SUBMIT_TIME and PR_MESSAGE_DELIVERY_TIME and let us know your feedback.

private static void Email_626617()
{
    MailMessage mailMsg = new MailMessage();
    mailMsg.From = "FromMe@demo.com";
    mailMsg.To = "ToYou@demo.com";
    mailMsg.Subject = "my subject";
    mailMsg.TextBody = "my body";
    mailMsg.Date = DateTime.Today;
    MapiMessage outlookMsg = MapiMessage.FromMailMessage(mailMsg);
    MapiProperty ClientSubmitTime = new MapiProperty(MapiPropertyTag.PR_CLIENT_SUBMIT_TIME, convertDateTime(new DateTime(2013, 9, 11)));
    outlookMsg.SetProperty(ClientSubmitTime);
    MapiProperty DeliveryTime = new MapiProperty(MapiPropertyTag.PR_MESSAGE_DELIVERY_TIME, convertDateTime(new DateTime(2013, 9, 11)));
    outlookMsg.SetProperty(DeliveryTime);
    outlookMsg.SetMessageFlags(MapiMessageFlags.MSGFLAG_UNMODIFIED);
    string strMsgFile = "sample.msg";
    outlookMsg.Save(strMsgFile);
}

private static byte[] convertDateTime(DateTime t)
{
    long filetime = t.ToFileTime();
    byte[] d = new byte[8];
    d[0] = (byte)(filetime & 0xFF);
    d[1] = (byte)((filetime & 0xFF00) >> 8);
    d[2] = (byte)((filetime & 0xFF0000) >> 16);
    d[3] = (byte)((filetime & 0xFF000000) >> 24);
    d[4] = (byte)((filetime & 0xFF00000000) >> 32);
    d[5] = (byte)((filetime & 0xFF0000000000) >> 40);
    d[6] = (byte)((filetime & 0xFF000000000000) >> 48);
    d[7] = (byte)(((ulong)filetime & 0xFF00000000000000) >> 56);
    return d;
}

Hi Muhammad,

The MapiMessage properties you advised worked perfectly!

Thankyou very much for your help.


Hi Lyndon,

Thank you for sharing your feedback. Please feel free to write to us if you have any other query related to Aspose.Email API. We’ll be glad to assist you further.

1 Like

Thank you very much for your solution.It works perfectly also for me.
I just have a question about convertDateTime function, why we have to convert datetime to array[8] ?
Why the length is exactly 8 ?

@nawara,

I like to inform that PR_CLIENT_SUBMIT_TIME has type PT_SYSTIME. PT_SYSTIME is 8 byte type. Please visit this link for more details. Also please visit this article link as well for detail explanation.