Outlook 插件在保存电子邮件时抛出 COMException

Outlook-addin throws COMException when saving an Email

下面的代码片段在第

行时给了我一个“COMException

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

达成。 事件被正确触发并触发事件处理程序。

>InboxFolder_ItemAdd(object o)

捕获块中抛出的异常消息类似于:

> 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);
    }        
}

另存为需要包含文件名的完整路径:

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