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

Free Support Forum - aspose.com

How to get date in local format in mht from outlook event

Hi,
when i convert an email to mht with the code below, the date is in french :
mer. 31 oct. 2018 14:35:21 (as required in :
mhtOptions.FormatTemplates[MhtTemplateName.DateTime] = “ddd dd MMM yyyy HH:mm:ss”:wink:

But with the same parameter, when i convert a calendar event to mht, the date is in US format :
Wed 10/31/2018 2:35 PM.

it looks like if the DateTime parameter doesn’t apply to calendar event mht conversion.

attached are the sample files
event.zip (18.3 KB)

here is the code :

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

using Aspose.Words;
using Aspose.Words.Saving;
using Aspose.Email;

namespace MsgToPdf
{
class Program
{
static void Main(string[] args)
{
Aspose.Email.License licm = new Aspose.Email.License();
licm.SetLicense(“d:\temp\Aspose.Total.lic”);
Aspose.Words.License licw = new Aspose.Words.License();
licw.SetLicense(“d:\temp\Aspose.Total.lic”);
try
{
MailMessage message = MailMessage.Load(“d:\temp\samples_msg\event_ok.msg”);
// If mail date is to be printed according to the local system timezone,
// subtract the mail timezone offset from mail date and your local system timezone
TimeZone localZone = TimeZone.CurrentTimeZone;
TimeSpan ts = localZone.GetUtcOffset(DateTime.Now);
message.Date = message.Date - message.TimeZoneOffset + ts;

// MhtSaveOptions mhtOptions = new Aspose.Email.MhtSaveOptions();
// mhtOptions.MhtFormatOptions = MhtFormatOptions.WriteHeader | MhtFormatOptions.WriteCompleteEmailAddress;
MhtSaveOptions mhtOptions = Aspose.Email.SaveOptions.DefaultMhtml; //new Aspose.Email.MhtSaveOptions();
mhtOptions.MhtFormatOptions = MhtFormatOptions.WriteHeader | MhtFormatOptions.WriteCompleteEmailAddress | MhtFormatOptions.RenderCalendarEvent | MhtFormatOptions.RenderTaskFields | MhtFormatOptions.RenderVCardInfo;
mhtOptions.SkipInlineImages = true;

            mhtOptions.FormatTemplates[MhtTemplateName.From] = mhtOptions.FormatTemplates[MhtTemplateName.From].Replace("From", "De");
            mhtOptions.FormatTemplates[MhtTemplateName.Cc] = mhtOptions.FormatTemplates[MhtTemplateName.Cc].Replace("Cc", "Cc");
            mhtOptions.FormatTemplates[MhtTemplateName.Bcc] = mhtOptions.FormatTemplates[MhtTemplateName.Bcc].Replace("Bcc", "Bcc");
            mhtOptions.FormatTemplates[MhtTemplateName.To] = mhtOptions.FormatTemplates[MhtTemplateName.To].Replace("To", "A");
            mhtOptions.FormatTemplates[MhtTemplateName.Subject] = mhtOptions.FormatTemplates[MhtTemplateName.Subject].Replace("Subject", "Objet");
            mhtOptions.FormatTemplates[MhtTemplateName.Sent] = mhtOptions.FormatTemplates[MhtTemplateName.Sent].Replace("Sent", "Envoyé le");
            mhtOptions.FormatTemplates[MhtTemplateName.DateTime] = "ddd dd MMM yyyy HH:mm:ss";
            mhtOptions.FormatTemplates[MhtTemplateName.Attachments] = mhtOptions.FormatTemplates[MhtTemplateName.Attachments].Replace("Attachments", "Pièces jointes");
            mhtOptions.FormatTemplates[MhtTemplateName.ShowTimeAs] = mhtOptions.FormatTemplates[MhtTemplateName.ShowTimeAs].Replace("Show Time As", "Afficher la disponibilité");
            mhtOptions.FormatTemplates[MhtTemplateName.Location] = mhtOptions.FormatTemplates[MhtTemplateName.Location].Replace("Location", "Emplacement");
            mhtOptions.FormatTemplates[MhtTemplateName.Start] = mhtOptions.FormatTemplates[MhtTemplateName.Start].Replace("Start", "Début");
            mhtOptions.FormatTemplates[MhtTemplateName.End] = mhtOptions.FormatTemplates[MhtTemplateName.End].Replace("End", "Fin");
            mhtOptions.FormatTemplates[MhtTemplateName.Importance] = mhtOptions.FormatTemplates[MhtTemplateName.Importance].Replace("Importance", "Importance");
            mhtOptions.FormatTemplates[MhtTemplateName.Organizer] = mhtOptions.FormatTemplates[MhtTemplateName.Organizer].Replace("Organizer", "Organisateur");
            mhtOptions.FormatTemplates[MhtTemplateName.Recurrence] = mhtOptions.FormatTemplates[MhtTemplateName.Recurrence].Replace("Recurrence", "Périodicité");
            mhtOptions.FormatTemplates[MhtTemplateName.RecurrencePattern] = mhtOptions.FormatTemplates[MhtTemplateName.RecurrencePattern].Replace("Recurrence Pattern", "Critère de périodicité");
            mhtOptions.FormatTemplates[MhtTemplateName.RequiredAttendees] = mhtOptions.FormatTemplates[MhtTemplateName.RequiredAttendees].Replace("Required Attendees", "Participants obligatoires");


            message.Save("d:\\temp\\samples_msg\\event_ok.mht", mhtOptions);
            GC.Collect();
        }
        catch (System.Exception ex)
        {
            String errmsg = ex.Message + " | " + ex.StackTrace;
            GC.Collect();
        }

    }
}

}

@tparassin

You may achieve this by setting PreserveOriginalDate to false. It defines whether need to keep original date header string in mail message when saving or not (Default value is true) so if you set this option to false the dates will be shown in local datetime as given below:

mhtOptions.PreserveOriginalDate = false;

Besides, if you want to change default datetime format then you need to change FormatTemplates like as:

mhtOptions.FormatTemplates[MhtTemplateName.DateTime] = "ddd, d MMM yyyy hh:mm:ss a zzz";

Moreover, if you want to display datetime in French then you may need to apply culture settings like as:

CultureInfo culture = new CultureInfo("fr-FR"); 
mhtOptions.FormatTemplates[MhtTemplateName.DateTime] = String.Concat(culture.DateTimeFormat.LongDatePattern, " ", culture.DateTimeFormat.ShortTimePattern);

I’m sorry but i can’t find CultureInfo in Aspose.email ?!

@tparassin

CultureInfo is a part of System.Globalization. So you need add this namespace in you code.
Moreover, you may get the day name in French after applying culture settings using the following code sample:

var _day = culture.DateTimeFormat.GetAbbreviatedDayName(DateTime.Now.DayOfWeek);

thank you for your replies … BUT the problem is, as i said in the original ticket, that the mhtOptions.FormatTemplates[MhtTemplateName.DateTime] doesn’t apply for calendar event msg.
Could you test with the attached event_ok.msg ?event_ok.zip (13.5 KB)

@tparassin

We have logged this issue with ID “EMAILNET-39173” for further investigation. You will automatically be informed here once we have more information to share.

@tparassin

To show dates in French (or in any other language) you may need to set appropriate culture to current thread. You may follow the code sample given below:

var msgPath = Path.Combine(drivePath, "event_ok.msg");

CultureInfo currentCultureBackup = Thread.CurrentThread.CurrentCulture;
CultureInfo culture = new CultureInfo("fr-FR");
Thread.CurrentThread.CurrentCulture = culture;
 
MailMessage message = MailMessage.Load(msgPath);
MhtSaveOptions saveOptions = new MhtSaveOptions();
saveOptions.FormatTemplates[MhtTemplateName.DateTime] = culture.DateTimeFormat.FullDateTimePattern;
saveOptions.MhtFormatOptions = MhtFormatOptions.WriteHeader | MhtFormatOptions.WriteCompleteEmailAddress | MhtFormatOptions.RenderCalendarEvent | MhtFormatOptions.RenderTaskFields | MhtFormatOptions.RenderVCardInfo;
message.Save(msgPath + ".mhtml", saveOptions);

Thread.CurrentThread.CurrentCulture = currentCultureBackup; 

Please feel free to write back to us if you need additional information.

The issues you have found earlier (filed as EMAILNET-39173) have been fixed in this update. This message was posted using BugNotificationTool from Downloads module by MuzammilKhan