Free Support Forum - aspose.com

Inconsistent MailMessage.TimeZoneOffset between sent and unset email

Inconsistent MailMessage.TimeZoneOffset between sent and unset email


Reproduction steps.
- Make a new mail and send to yourself.
- Save both copies, 1 from indbox, 1 from sent mail

My timezone offset is +2.
for the inbox mail the MailMessage.TimeZoneOffset is set to +2
for the sent mail the MailMessage.TimeZoneOffset is set to +0

The MailMessage.Date is correct local time in both cases.
when converting the mail to mhtml the time will differ by 2 hours, as the sent mail is rendered as UTC and the inbox mail is rendered as local time.

This is highly confusing.

Hi Nickolai,

Thank you for writing to Aspose Support team.

Datetime display in Mhtml file can be controlled as follows:

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.

Can you explain why the timezoneoffset is different depending on where the mail is taken from?


Both mails are generated in offset +2, but the one from sent folder has it set to zero. That behavior is not consistent.

Hi Nickolai,


I have repeated steps to re-produce the scenario by dragging same message from sent folder and inbox folder. I am afraid that no difference is observed in Timezone offset value of message retrieved from sent folder and inbox folder. In my case both the messages contain timezone offset value 5. Hence both the messages generate similar Mhtml file containing same date and time.

Could you please inform which version of Aspose.Email .NET are you using? I have used Aspose.Email for .NET 6.5.0 and Outlook 2016 for testing.

I have been using Outlook 2013 and Aspose.Email 6.2.0

Hi Nickolai,

Please try it with the latest version of the API i.e Aspose.Email for .NET 6.5.0 and let us know your feedback.

I have now tried with 6.5 using Outlook 2013


Unit test code
--------------------
[TestMethod]
public void Opg54564_MsgToPdf_TestAfKlokken_testMail2()
{
//var l = new LicenseActivator();
Debug.WriteLine($"{Aspose.Email.BuildVersionInfo.Product} {Aspose.Email.BuildVersionInfo.ProductMajor}.{Aspose.Email.BuildVersionInfo.ProductMinor} - {Aspose.Email.BuildVersionInfo.ReleaseDate}");

var fileNameInbox = @".\OutlookTestFiles\54564\from inbox - test mail sent approx 2016-05-31 1516.msg";
var filenameSent = @".\OutlookTestFiles\54564\from sent - test mail sent approx 2016-05-31 1516.msg";

var msgInbox = MailMessage.Load(fileNameInbox);
var msgSent = MailMessage.Load(filenameSent);

Debug.WriteLine($“inbox date:{msgInbox.Headers[“Date”]} (Header)”);
Debug.WriteLine($“inbox date:{msgInbox.Date} (msg.Date)”);
Debug.WriteLine($“inbox timezoneoffset:{msgInbox.TimeZoneOffset}”);

Debug.WriteLine($“sent date:{msgSent.Headers[“Date”]} (Header)”);
Debug.WriteLine($“sent date:{msgSent.Date} (msg.Date)”);
Debug.WriteLine($“sent timezoneoffset:{msgSent.TimeZoneOffset}”);

Assert.AreEqual(msgInbox.TimeZoneOffset, msgSent.TimeZoneOffset);
}

-----------------

output from test:

Assert.AreEqual failed. Expected:<02:00:00>. Actual:<00:00:00>.

ved ScratchPad.MsgTests.Opg54564_MsgToPdf_TestAfKlokken_testMail2() i .\MsgTests.cs:linje 166


Debug Trace:
Aspose.Email 6.5 - 04-05-2016 00:00:00
inbox date:Tue, 31 May 2016 15:16:10 +0200 (Header)
inbox date:31-05-2016 15:16:10 (msg.Date)
inbox timezoneoffset:02:00:00
sent date:Tue, 31 May 2016 13:16:09 +0000 (Header)
sent date:31-05-2016 15:16:09 (msg.Date)
sent timezoneoffset:00:00:00

Hi Nickolai,


I am afraid that issue is not re-produced at my end using your code and sample files. I have tested the properties using MFCMapi as well and got the similar date values. It is difficult to provide assistance until and unless this issue is not re-produced here. You may test this scenario on some other machine as well and share the feedback.

This issue is present on multiple machines.


My developer machine: Windows 8.1
Test server running Windows Server 2012 R2
On multiple customer machines.

It might have something to do with regional settings, all machines is using Danish.

Hi Nickolai,

We tested the issue on Windows 7 and didn’t find any problem with your sample files with our local regional settings. Could you please guide us towards setting Danish as regional settings? Meanwhile, we are establishing a test environment of Windows Server 2012 R2 for further investigation of this issue and your guidance will help us analyze the problem in more details. We appreciate your cooperation in this regard.

Maybe it’s the same problem as in https://www.aspose.com/community/forums/showthread.aspx?PostID=802494&Subj=inconsistent-timestamp-between-sent-and-unsent-email#

Hi,


You may please follow this as this issue is discussed here.