c# - 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:“项目已被移动或删除。”
解决方案
此邮件对象在发送后被释放,因此您无权访问它。您可能必须添加一个事件处理程序。这样的事情可能会奏效。
((Outlook.ItemEvents_10_Event)mail).Send += new Microsoft.Office.Interop.Outlook.ItemEvents_10_SendEventHandler(SaveSentMail);
static void SaveSentMail(ref bool Cancel)
{
mail.SaveAs(mydesktop+ ....);
}
推荐阅读
- ios - 在 Nativescript 应用程序中修改 Podfile
- sql - 如果数据在表中持续超过 12 小时,则发送电子邮件,SQL
- c++ - 有没有办法为 char 数组分配堆栈内存?但是,鉴于长度是已知的,但是稍后会添加内容?
- tfs - 添加nuget包时不会自动安装依赖项
- python - 如何排序字典的键
- python-3.x - 使用相同的 credentials.json 在 python 中访问 google drive/google sheet api
- c# - 如何在不使用 C# 中的 JSchema nuget 的情况下针对 JSON 模式验证 JSON 对象?
- css - 如何在 CSS(移动版)中安排类别布局?
- database - 有人可以解释 arrayJoin() 函数在 Clickhouse 数据库中的工作原理吗?
- python - 不会调用函数