We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Email header timestamp format mismatch when converting Msg to Mht

Hi,


We’ve faced a problem when the output Mht with an email header has a different timestamp format than the original Msg. We use the following save option when converting to MHtml before pdf conversion:

var mailMsgStream = new MemoryStream();
mail.Save(mailMsgStream, new MhtSaveOptions { MhtFormatOptions = MhtFormatOptions.WriteHeader });

the output Mht header (and the resulting PDF, of course) looks like

Sent: 11 May 2016 10:19:02 +0200

while the original (mail.Date value) is:

5/11/2016 11:19:02 AM

or, as in the msg file : Wed 5/11/2016 11:19 AM

Is there any control over this or is it a bug (the conversion was run on the same machine that accepted the source email, i.e. the time zone/culture are the same)?

Thanks

Hi Ulrik,

Thank you for contacting Aspose support team.

Aspose.Email provides rich features to format the Mhtml file. You may please use following sample code to format the Mhtml file and hence the PDF file. Please give it a try and let us know the feedback.

MailMessage mail = MailMessage.Load("sample.eml");
MemoryStream msMHTML = new MemoryStream();
MhtMessageFormatter mhtlFormat = new MhtMessageFormatter();
mhtlFormat.DateTimeFormat = "MM/dd/yy H:mm:ss tt";
mhtlFormat.Format(mail);
MhtSaveOptions mhtSaveOptions = new MhtSaveOptions();
mhtSaveOptions.MhtFormatOptions = MhtFormatOptions.None;
mail.Save("Sample.mht", mhtSaveOptions);

hi, Kashif,


thanks a lot for the quick reply!
It’s got better, but the output time is still one hour less than the source for some reason…

Hi Ulrik,


This difference can be due to daylight saving setting on your system. You may please consider that as well.

Hi, Kashif,


I didn’t quite understand you, but I expect that when there’s an existing source value it should be converted just as is (or with some formatting applied), no other circumstances should be considered. There’s a timestamp on the source msg, and I just want it to be put to the resulting mht,

Thanks

Hi Ulrik,


Sometimes local timezone information can be used to adjust the time in source email for display purpose. It seems that your issue is not resolved yet. You may please send a sample EML file along with the output Mhtml file. Also please mention in detail w.r.t to the sample EML the expected output date. We will analyze it thoroughly and provide feedback.

Hi, Kashif,


please, find attached the source Outlook .msg file and the resulting pdf (which gets rendered from mht).

the source file’s Sent: value is “Thu 5/19/2016 4:23 PM”, whereas the mht (and thus pdf) have it as “19 May 2016 15:22:34 +0200”.

We expect that the timestamp is exactly the same as the source (Outlook).
This is very critical for our customers so we would appreciate a lot if you guys could fix it ASAP.

Please, let me know how much time could this fix possibly take (approximately).

Thanks

P.S. Please note that the Mail.txt is actually Mail.msg as I was not allowed to attach .msg

Hi Ulrik,

I have thoroughly analyzed the message and observed that actual sent time of mail is Thu 5/19/2016 1:23 PM as can be seen in MFCMapi.

You may also observe that the MailMessage.TimeZoneOffset value of this message is 2. That is why the PDF file contains the time 05/19/16 15:22:34 PM (i.e. 05/19/16 15:23 PM which is mail sent time as UTC + timezone offset.

MailMessage mail = MailMessage.Load(path + "Mail.msg");
Console.WriteLine(mail.Date.ToString() + ", Kind = " + mail.Date.Kind);

MemoryStream msMHTML = new MemoryStream();
MhtMessageFormatter mhtlFormat = new MhtMessageFormatter();
mhtlFormat.DateTimeFormat = "MM/dd/yy H:mm:ss tt";
mhtlFormat.Format(mail);
MhtSaveOptions mhtSaveOptions = new MhtSaveOptions();
mhtSaveOptions.MhtFormatOptions = MhtFormatOptions.None;
mail.Save(path + “Mail.mht”, mhtSaveOptions);

I display mail date and Kind, it shows 5/19/2016 6:22:34 PM, Kind = Local (as in the above code sample). This shows that when we check mail.Date, it is in local system timezone. Here you may get different time according to your system timezone when above code is executed in your environment.

If you want to print the UTC time in the Mhtml, you may subtract the mail timezone offset from the date as shown in the following sample code:

MailMessage mail = MailMessage.Load(path + "Mail.msg");
Console.WriteLine(mail.Date.ToString() + ", Kind = " + mail.Date.Kind);
Console.WriteLine("TimeZone offset = " + mail.TimeZoneOffset);
Console.WriteLine("Actual UTC Time is " + mail.Date.ToUniversalTime().ToString());
Console.WriteLine((mail.Date.ToUniversalTime() + mail.TimeZoneOffset).ToString());

mail.Date = mail.Date - mail.TimeZoneOffset ;
MemoryStream msMHTML = new MemoryStream();
MhtMessageFormatter mhtlFormat = new MhtMessageFormatter();
mhtlFormat.DateTimeFormat = "MM/dd/yy H:mm:ss tt";
mhtlFormat.Format(mail);
MhtSaveOptions mhtSaveOptions = new MhtSaveOptions();
mhtSaveOptions.MhtFormatOptions = MhtFormatOptions.None;
mail.Save(path + "Mail.mht", mhtSaveOptions);

If mail date is to be printed according to the local system timezone, you may subtract the mail timezone offset from mail date and add your local system timezone as shown in the following sample code.

MailMessage mail = MailMessage.Load(path + "Mail.msg");
Console.WriteLine(mail.Date.ToString() + ", Kind = " + mail.Date.Kind);
Console.WriteLine("TimeZone offset = " + mail.TimeZoneOffset);
Console.WriteLine("Actual UTC Time is " + mail.Date.ToUniversalTime().ToString());
Console.WriteLine((mail.Date.ToUniversalTime() + mail.TimeZoneOffset).ToString());

TimeZone localZone = TimeZone.CurrentTimeZone;
TimeSpan ts = localZone.GetUtcOffset(DateTime.Now);
mail.Date = mail.Date - mail.TimeZoneOffset + ts;
MemoryStream msMHTML = new MemoryStream();
MhtMessageFormatter mhtlFormat = new MhtMessageFormatter();
mhtlFormat.DateTimeFormat = "MM/dd/yy H:mm:ss tt";
mhtlFormat.Format(mail);
MhtSaveOptions mhtSaveOptions = new MhtSaveOptions();
mhtSaveOptions.MhtFormatOptions = MhtFormatOptions.None;
mail.Save(path + "Mail.mht", mhtSaveOptions);

Hope this clarifies the issue in detail and please feel free to write us back if you have any other query in this regard.

Hi, Kashif,


getting back to our conversation. I’m still having confusion about the msg timestamp, but now of a bit different nature. My problem is a bit similar to the one discussed here regarding the timezone - Email sent time showed by receiver's time zone.

My expectation was that the TimeZoneOffset field of MailMessage holds the timezone of the sender. E.g. if I received a mail sent out from, say, London, I would expect the TimeZoneOffset be +00 00, yet it seems that the value is actually the timezone of my own PC where I read the mail. My expectation was to have the timestamp be the local time of the sender and the TimeZoneOffset be also relevant to the sender. So, when I see a PDF rendition I can see that the mail was sent at, say, 12:00 PM GMT +00 00 (i.e. 12 pm, London time).

Is it possible to achieve or I’m getting it wrong? Some articles over the net mention that the .msg timestamp can actually contain time data from the intermediary mail servers through which it goes, but I couldn’t find a clear explanation yet.

I’m using MhtMessageFormatter to add a header to the rendered PDF, and the timestamp is marked as “Sent”, whereas it seems that it should be “Received” (I do not see any other explanation to this).


One of the tests I diid is as follows:

my PC is originally in GMT +02 00, so,

1) I change my TimeZone to GMT +00 00, reboot, and call some system functions to verify that I’m actually running in the timezone selected.

2) Send myself a mail and save it

3) Convert it to PDF using Aspose.Email + MhtMessageFormatter to show a header

4) Under debug I see that the MailMessage’s <span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>TimeZoneOffset is actually + 02 00
<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>5) In PDF I see "Sent: " [Time in GMT +02 00]
<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>So, it looks like these values are picked up on the exchange server and do not depict the sender’s time.
<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>Am I right?

Hi,


Yes, you are right. The values are picked up on the Exchange server and local time is not considered in this case. Please feel free to write to us if you have further query in this regard.

when using above with UTC date.

If you want to print the UTC time in the Mhtml, you may subtract the mail timezone offset from the date as shown in the following sample code:

MailMessage mail = MailMessage.Load(path + "Mail.msg");
Console.WriteLine(mail.Date.ToString() + ", Kind = " + mail.Date.Kind);
Console.WriteLine("TimeZone offset = " + mail.TimeZoneOffset);
Console.WriteLine("Actual UTC Time is " + mail.Date.ToUniversalTime().ToString());
Console.WriteLine((mail.Date.ToUniversalTime() + mail.TimeZoneOffset).ToString());
mail.Date = mail.Date - mail.TimeZoneOffset;

the final date is 1 hour out!

How to recreate: I create a local msg email, I then add that to another email as an attachment.
I extract the attachment using aspose, this creates a new msg file but has UTC date instead of Local, then run above and the local checks out ok but the utc code above i.e. mail.Date = mail.Date - mail.TimeZoneOffset ;
is -1 hour out.
with GMT settings.

Hi Lee,

I have tried to re-produce the scenario but afraid to share that no such issue is observed. The final date after subtracting the timezone offset, the time is still UTC without any difference. Following sample code is used for testing:

MailMessage parent = MailMessage.Load("Parent Mail.msg");
Attachment att = parent.Attachments[0];
MemoryStream str = new MemoryStream();
att.Save(str);

MailMessage mail = MailMessage.Load(str);
Console.WriteLine(mail.Date.ToString() + ", Kind = " + mail.Date.Kind);
Console.WriteLine("TimeZone offset = " + mail.TimeZoneOffset);
Console.WriteLine("Actual UTC Time is " + mail.Date.ToUniversalTime().ToString());
Console.WriteLine((mail.Date.ToUniversalTime() + mail.TimeZoneOffset).ToString());

mail.Date = mail.Date - mail.TimeZoneOffset;

Could you please send us the message file which is used for testing? Also mention the timezone setting of your system during the testing. It will help us to observe the issue and provide assistance.