Saving the current mailitem multiple times all within the same window

Add-in Express™ Support Service
That's what is more important than anything else

Saving the current mailitem multiple times all within the same window
 
Alexander de Man




Posts: 47
Joined: 2007-05-03
Hello,

I have an addin that, very simplified, saves the current mail item to disk. This works fine the first time, but when the user changes the mail later (within the same window) the version on disk doesn't contain the changes.

I'm using the following code (also simplified) to save the message to disk


var mailItem = OutlookApp.ActiveInspector().CurrentItem as Outlook.MailItem;

string fileName = string.Format(@"D:\{0}.msg", Guid.NewGuid().ToString("N"));

try
{
    mailItem.SaveAs(fileName);
}
finally
{
    if (mailItem != null)
        Marshal.ReleaseComObject(mailItem);
}


Steps to reproduce:
1. Create a new message
2. Change body text to 'v1'
3. Use above code to save the message to disk
4. Change the body text to 'v2'
5. Save the message again

Result is that the 2nd message still has the old body (v1).

If it is possible, I would rather not save the message itself with mailItem.Save() and just let Outlook do that when needed.

What do you suggest I do in this case?
Posted 30 Mar, 2016 10:24:45 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
Hello Alexander,

I suggest releasing COM objects. The code fragment above doesn't release two of them: 1) a COM object representing the active inspector, and 2) the COM object returned by Inspector.CurrentItem if a task, contact, etc. (= not a MailItem) is opened in that inspector window. Also, if you let exception propagate, your add-in may crash Outlook and Outlook will disable the add-in.

Here's a raw sketch:


Outlook.Inspector inspector = null;
object item = null;
try {
    inspector = OutlookApp.ActiveInspector();
    if (inspector != null) {
    item = inspector.CurrentItem; 
    if (item is Outlook.MailItem) {
        Outlook.MailItem mail = item as Outlook.MailItem;
        // do your stuff here
        string fileName = string.Format(@"D:{0}.msg", Guid.NewGuid().ToString("N")); 
        mailItem.SaveAs(fileName);
    }
} catch (Exception ex) {
    // log exception here
} finally { 
    if (item != null) Marshal.ReleaseComObject(item); // in .NET, item and mail refer to the same COM object 
    if (inspector != null) Marshal.ReleaseComObject(inspector); 
}




Andrei Smolin
Add-in Express Team Leader
Posted 31 Mar, 2016 01:46:17 Top
Alexander de Man




Posts: 47
Joined: 2007-05-03
Hello Andrei,

Thank you for your response. But it still doesn't work :)

The strange thing is, when I set a watch on the SaveAs-method, it shows the correct body, but Outlook still saves the previous version. I tried to copy the mail by using mailItem.Copy(), this works within Outlook, but when a user uses the explorer 'Send To Mail Receipient' the copy call fails with a 'outlook could not complete the operation because the service provider does not support it'

Regards,
Alexander
Posted 31 Mar, 2016 03:04:31 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
Alexander de Man writes:
Thank you for your response. But it still doesn't work :)


There are other parts of your code that may leave unreleased the COM object representing the inspector, the item or any child object of the item ( UserProperties, UserProperty, Attachments, Attachment, etc.). You can create a new add-in, and call that code from a Ribbon button; with all other COM add-ins are turned off, this code fragment shouldn't produce any side effects.



Andrei Smolin
Add-in Express Team Leader
Posted 31 Mar, 2016 03:14:33 Top
Alexander de Man




Posts: 47
Joined: 2007-05-03
Hello Andrei,

I'm already using a seperate add-in that only contains the code you provided.

Regards,
Alexander
Posted 31 Mar, 2016 03:36:44 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
Check if turning *all* other COM add-ins off helps.



Andrei Smolin
Add-in Express Team Leader
Posted 31 Mar, 2016 03:58:11 Top
Alexander de Man




Posts: 47
Joined: 2007-05-03
Hello Andrei,

I did some testing on 3 clean pc's (with 2010, 2013 and 2016) and those all worked instantly (without disabling the COM-addins). What is different is that my PC has 'Use Cache Exchange Mode' enabled. When I enabled this feature on the test machines, it stops working there too.

Regards,
Alexander
Posted 31 Mar, 2016 05:21:13 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
Hello Alexander,

The issue is reproducible even if you SaveAs the email from the UI. I assume this problem belongs to Outlook. I suggest that you call MailItem.Save before you call MailItem.SaveAs.


Andrei Smolin
Add-in Express Team Leader
Posted 01 Apr, 2016 04:01:11 Top
Alexander de Man




Posts: 47
Joined: 2007-05-03
Hello Andrei,

You are correct! Thanks again for your help and have a good weekend!

Regards,
Alexander
Posted 01 Apr, 2016 04:06:18 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
Alexander,

For the sake of completeness, I've posted a description of the issue on the Outlook for Developers forum at https://social.msdn.microsoft.com/Forums/office/en-US/b4a01fd3-3a0e-4fa8-9b75-9478f852bb81/a-problem-with-saveasing-a-mailitem?forum=outlookdev.

Have a nice weekend!


Andrei Smolin
Add-in Express Team Leader
Posted 01 Apr, 2016 04:40:07 Top