Search code examples
c#outlookcomexception

Outlook-addin throws COMException when saving an Email


The code fragment below gives me a "COMException" when the line

>mail.SaveAs(@"C:\Users\Michalczak\Desktop");

is reached. The Event is fired properly and triggers the Event-Handler.

>InboxFolder_ItemAdd(object o)

The Exception-Message that is thrown in the catch-block is something like:

> You do not have permission to perform this operation.

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    myTestInBox = (Outlook.Folder)this.Application.Session.GetDefaultFolder
                      (Outlook.OlDefaultFolders.olFolderInbox);
    stor.inBox = myTestInBox.Items;
    stor.inBox.ItemAdd += 
         new Outlook.ItemsEvents_ItemAddEventHandler
             (InboxFolder_ItemAdd);
}

private void InboxFolder_ItemAdd(object o)
{
    Outlook.MailItem mail = null;
    if ( o != null && o is Outlook.MailItem)
    {
        mail = (Outlook.MailItem)o;
    }
    try
    {
        mail.SaveAs(@"C:\Users\Michalczak\Desktop");
    }
    catch (System.Runtime.InteropServices.COMException ce)
    {
        MessageBox.Show(ce.Source);
        MessageBox.Show(ce.Message);
    }        
}

Solution

  • SaveAs requires the complete path with filename:

    mail.SaveAs(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\" + filename + ".msg");