首页 > 解决方案 > C# 保存上次发送邮件到桌面(作为 .msg)

问题描述

我正在尝试将最后一次从 Outlook 发送到我的桌面的邮件保存为 .msg 格式。但是我在代码的最后一行中的代码出现错误,如下所示:

        ((Microsoft.Office.Interop.Outlook.MailItem)mail).SaveAs(mydesktop+ "\\Myapplication\\" + subject.Replace(":", "").Replace("/", "").Replace("|", "") + ".msg", Microsoft.Office.Interop.Outlook.OlSaveAsType.olMSG);

错误:System.Runtime.InteropServices.COMException:“项目已被移动或删除。”

      string mailto = labelControl53.Text + ";" + labelControl56.Text ;
        string cc = "myaccount@mymail.com";
        string subject= labelControl7.Text + "-" + comboBoxEdit1.Text + "-" + textEdit6.Text + " Yüklemesi hk.";

        string mydesktop= Environment.GetFolderPath(Environment.SpecialFolder.Desktop);


        Microsoft.Office.Interop.Outlook.Application mailat = new Microsoft.Office.Interop.Outlook.Application();
        Microsoft.Office.Interop.Outlook.MailItem mail = (Microsoft.Office.Interop.Outlook.MailItem)mailat.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
         mail.To = mailto;
        mail.CC = cc;
        mail.Subject = subject;
        mail.Importance = Microsoft.Office.Interop.Outlook.OlImportance.olImportanceHigh;
        mail.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;
        mail.HTMLBody = getHTMLupload();
        ((Microsoft.Office.Interop.Outlook.MailItem)mail).Send();
        ((Microsoft.Office.Interop.Outlook.MailItem)mail).SaveAs(mydesktop+ "\\Myapplication\\" + subject.Replace(":", "").Replace("/", "").Replace("|", "") + ".msg", Microsoft.Office.Interop.Outlook.OlSaveAsType.olMSG);

System.Runtime.InteropServices.COMException:“项目已被移动或删除。”

标签: c#outlooksavesendmail

解决方案


此邮件对象在发送后被释放,因此您无权访问它。您可能必须添加一个事件处理程序。这样的事情可能会奏效。

    ((Outlook.ItemEvents_10_Event)mail).Send += new Microsoft.Office.Interop.Outlook.ItemEvents_10_SendEventHandler(SaveSentMail);

    static void SaveSentMail(ref bool Cancel)
    {
       mail.SaveAs(mydesktop+ ....);
    }

推荐阅读