如何在发送后立即保存已发送的电子邮件?

How to save a sent email immediately after Send?

我有一个来自 Outlook 的单独 .Net 应用程序。我想使用 Office.Interop(避免使用 redmeption.dll)来发送和保存从该应用程序发送的电子邮件。但是当我尝试在 Send() 函数之后保存电子邮件时,出现异常:"The item has been moved or deleted."

Outlook 似乎在发送后移动了电子邮件项目。

所以,谁能建议我将 "Sent" 电子邮件项目保存到磁盘的最佳方法?

我需要保存的消息文件具有状态 'sent'。因此,保存 Item_Send 事件不适用于我的情况。谢谢!

这是我的代码

     public bool SendAndSavAs(Outlook.Account emailAccount, string toEmailAddress)
    {
        Outlook.MailItem mailItem = null;
        try
        {
            mailItem = OutlookApp.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
            mailItem.Subject = "Test Subject";
            mailItem.BodyFormat = Outlook.OlBodyFormat.olFormatPlain;
            mailItem.Body = "Test Body";

            // Add recipient using display name, alias, or smtp address
            mailItem.Recipients.Add(toEmailAddress);
            mailItem.Recipients.ResolveAll();
            mailItem.SendUsingAccount = emailAccount;

            // send email
            mailItem.Send();

            // save the Sent mail to local disk  (****but have exception occurs ****)
            mailItem.SaveAs(@"c:\temp\test.msg");

            return true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            ReleaseComObject(mailItem);
        }
    }

您需要处理 Sent Items 文件夹上的 ItemAdd 事件。在事件处理程序中,您可以调用 SaveAs 方法。

您也可以考虑使用 MailItem class 的 SaveSentMessageFolder 属性,它允许设置一个 Folder 对象,该对象表示电子邮件副本所在的文件夹发送后保存

调用 Send 后,您唯一可以对该消息执行的操作就是释放您对它的引用 - 它现在属于传输提供商。

另请注意,发送是异步的 - 消息被发送并且其发送者相关属性仅在发送后才会填充。当 Items.ItemAdd 事件在“已发送邮件”文件夹中触发时,您第一次有机会访问该状态的邮件。