Hi,
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,
Hi Ulrik,
Hi, Kashif,
Hi Ulrik,
Hi, Kashif,
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,
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).
Hi,
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.