I have a need to save Outlook emails to MHTML (MHT) format. I have a test application that finds one email by EntryID and saves it the MHT format successfully.
My goal is to specify the time zone in which the sent time is displayed. By default, the Outlook object model writes the time to the MHT file using the time zone on the computer that writes the MHT. I would like to specify an arbitrary time zone and daylight saving time observance.
I am using Outlook 2010, Windows XP SP2, Visual Studio 2008 Professional.
I would prefer not to change the time on the processing computer to avoid possible issues with other things that might be happening on the computer at the same time. However, I may have to go that route.
I had hoped to change the time zone of the Application object, but the property Application.TimeZones.CurrentTimeZone
is read-only. I have not found a Set() method. Attempting to assign to CurrentTimeZone
results in this error: Property or indexer 'Microsoft.Office.Interop.Outlook._TimeZones.CurrentTimeZone' cannot be assigned to -- it is read only
//strPst = @"D:\aaa.pst";
//strEntryId = "0000000007840E169496284E947388623A8A9F48C4012000";
//Prepare Session
Microsoft.Office.Interop.Outlook.Application objApplication = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook._NameSpace objNameSpace = null;
objNameSpace = objApplication.GetNamespace("MAPI");
objNameSpace.Logon(null, null, false, false);
objApplication.Session.AddStore(strPst);
//Get PST ID
string strPstId = "";
foreach (Store store in objNameSpace.Stores)
{
if (store.FilePath == strPst)
{
strPstId = store.StoreID;
}
}
try
{
//Get item
object xi = objNameSpace.GetItemFromID(strEntryId, strPstId);
//Get item as MailItem
MailItem mi = objNameSpace.GetItemFromID(strEntryId, strPstId) as MailItem;
if (mi != null)
{
//mi.Display(null);
//Get time zone UTC-1200
Microsoft.Office.Interop.Outlook.TimeZones tzs = objApplication.TimeZones;
Microsoft.Office.Interop.Outlook.TimeZone tz = tzs[1];
//Console.WriteLine(tz.Name);
//Set time zone
objApplication.TimeZones.CurrentTimeZone = tz;
//Save As MHT
mi.SaveAs(@"D:\test.mht", OlSaveAsType.olMHTML);
//...
}
}