C#将上次发送的邮件保存到桌面(如.msg)

C#将上次发送的邮件保存到桌面(如.msg),c#,outlook,save,sendmail,C#,Outlook,Save,Sendmail,我正在尝试将上次从Outlook发送到桌面的邮件保存为.msg格式。 但我在代码的最后一行遇到了如下错误: ((Microsoft.Office.Interop.Outlook.MailItem)mail).SaveAs(mydesktop+ "\\Myapplication\\" + subject.Replace(":", "").Replace("/", "").Replace("|", "") + ".msg", Microsoft.Office.Interop.Outl

我正在尝试将上次从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:“项目已被移动或删除。”

此邮件对象在发送后被释放,因此您无权访问它。
      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);
您可能需要添加一个事件处理程序。像这样的东西可能有用

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

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

如果要在Outlook中保存上次发送的项目,则需要处理
项目
类的
项目添加
事件,该类来自
已发送项目
文件夹。通常,邮件一经发送就被放入
已发送邮件
文件夹。但是,用户或其他加载项可以设置属性,该属性设置一个布尔值,如果邮件在发送时未保存副本,则该值为True;如果邮件副本保存在“已发送邮件”文件夹中,则该值为False


或者在Outlook中提交项目之前(在
Send
方法之前)调用
SaveAs

哪一行引发异常?最后一行((Microsoft.Office.Interop.Outlook.MailItem)邮件)。SaveAs(mydesktop+“\\Myapplication\\\”+主题。替换(:,).Replace(“/”,).Replace(“/”).Replace(“,”)+”.msg”,Microsoft.Office.Interop.Outlook.OlSaveAsType.olMSG);您可以先保存邮件,然后再发送。我认为send方法将关闭/处理邮件项目,因此无法再访问它