异常:"The operation cannot be performed because the message has been changed."

Exception : "The operation cannot be performed because the message has been changed."

我有代码:

public void ExportEmail(Microsoft.Office.Core.IRibbonControl control)
{
   MailItem mailItem = this.extractor.ExtractMailItem(control);
   mailItem.MessageClass = "Bla bla";
   mailItem.Save();//Exception after second call
   //edit code////
   System.Runtime.InteropServices.Marshal.ReleaseComObject(mailItem);
   mailItem = null;
   ///////////////
   this.eventAggregator.GetEvent<ExportEmailEvent>().Publish(mailItem);
}

当我第一次按下按钮时,这段代码工作正常。但是第二次我得到异常:

System.Runtime.InteropServices.COMException{"The operation cannot be performed because the message has been changed."}

异常发生在 mailItem.Save();.

首先,我建议立即释放底层 COM 对象。使用 System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. You can read more about that in the Systematically Releasing Objects 篇文章。

消息 class 在您第一次通话后是否发生了变化?您是否尝试调试代码?有什么不同吗?是否是新消息(尚未保存)?