1

I recently posted a question about saving an email once it's sent - I have just about everything working, except for one small detail. Basically, I am able to catch an email right before it sends, and do whatever I want with it - in my case, save it. However, if you try to access that email's CreationTime attribute, it returns January 1st 4501 at 12AM. This is most likely because it hasn't actually been 'created' yet, in that it will be created in the Sent items folder as soon as my code finishes executing and it actually sends.

I'd like to leave this MailItem, which is about to be sent, untouched. I would like to duplicate it, change the CreationTime attribute of the duplicate to DateTime.Now, then save the duplicate, then allow Outlook to continue sending the original. However, when I attempt to modify the CreationTime, I get an error that that attribute is read-only. Is there any way to 'break into' it? Or any way to force a write or something?

Community
  • 1
  • 1
Jake
  • 3,142
  • 4
  • 30
  • 48
  • is there a reason why you need to change the creation time..? – MethodMan Aug 20 '12 at 17:51
  • Yes, when I save it to disk the creation time of the file is set to the creation time in the MailItem. I guess I could save the email first and then modify the CreationTime of the resulting file instead of doing it in the code, but that seems messy...although apparently using reflection to change a read-only property is messier? – Jake Aug 20 '12 at 18:05
  • well read-only properties are just that.. and unless you understand Reflection, looking at MUG4N's link I can't see where the mess is .. you need to change the example in that link to fit your UseCase – MethodMan Aug 20 '12 at 18:07

1 Answers1

1

A better approach is attaching to the sent items Folder.ItemAdd so you can save messages after they have been sent instead of before - that way your MailItem.CreationTime should be accurate. This may or may not be an option for you but could alleviate the issue.

Outlook.Folder sentItems = ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail) as Outlook.Folder;
sentItems.Items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(sentItems_ItemAdd);
// ...
void sentItems_ItemAdd(object Item)
{
  var msg = Item as Outlook.MailItem;
  msg.SaveAs(yourPath, Outlook.OlSaveAsType.olMSG);
}

Note: You need to handle proper COM resource disposal and error handling.

Jake
  • 3,142
  • 4
  • 30
  • 48
SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
  • 1
    Hmm, I like your approach better. When I implement it, though, I get the following error - "An object reference is required for the non-static field, method, or property 'add-in-name.ThisAddIn.Application' – Jake Aug 20 '12 at 18:29
  • Try using `Globals.ThisAddIn` or `this` if you are placing this code inside `ThisAddIn.cs`. – SliverNinja - MSFT Aug 21 '12 at 12:34
  • Ok, that moved the error down slightly, still a small issue though (thanks for all your help thus far!), basically first Visual Studio told me that there was no .ItemAdd, so I messed around with other options that Intellisense was giving me and found that SentItems.Items.ItemAdd works fine. I edited your code to reflect that. Anyway, now I get the error that ItemEvents_ItemAddEventHandler doesn't exist in Microsoft.Office.Interop.Outlook. Is there another place it's supposed to exist? – Jake Aug 21 '12 at 13:43
  • This page: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.itemsevents_event.itemadd.aspx seems to say I should be able to use ItemEvents_Event.ItemAdd, although using that I get an error saying that ItemAdd doesn't exist in type Microsoft.Office.Interop.Outlook.ItemEvents_Event – Jake Aug 21 '12 at 13:46
  • Looks like you have it misspelled..should be `ItemsEvents_Event.ItemAdd`. `ItemsEvents_ItemAddEventHandler` is within `Microsoft.Office.Interop.Outlook.dll` – SliverNinja - MSFT Aug 21 '12 at 14:01
  • You're right...sorry about that! Everything works fine, thanks so much! – Jake Aug 21 '12 at 14:41