IMAP帐户的MailItem.Save异常,“由于邮件已更改,无法执行该操作”

时间:2019-03-14 08:31:36

标签: c# vsto outlook-addin

如果我要在Outlook加载项中保存mailitem,则IMAP帐户会发生此异常。 当我在转发/回复邮件时更改链中先前消息的用户属性时,会发生这种情况。

我已经研究过https://answers.microsoft.com/en-us/msoffice/forum/msoffice_outlook-mso_win10-mso_2016/the-operation-cannot-be-performed-because-the/ba4fa59e-0813-4874-b122-10bcf6e586af?page=1,并尝试了@Vijay_Verma的解决方案,但对我而言不起作用。

这是我的代码:

 private void DraftItems_ItemAdd(object Item)
    {
        try
        {
            Outlook.MailItem mailItem = Item as Outlook.MailItem;

            Outlook.UserProperty mailUserProperty = mailItem.UserProperties?.Find("ABC");

            Outlook.Folder mailItemfolder = mailItem.Parent as Outlook.Folder;

            string emailId = mailItemfolder.Store.DisplayName;

            List<string> entryIdsInConvn = GetEmailsInConversation(mailItem.ConversationID, emailId);

            if (entryIdsInConvn != null && entryIdsInConvn.Count > 0)
            {
                Outlook.Folder inboxFolder = null;
                Outlook.NameSpace nameSpace = null;

                if (mailItemfolder != null)
                {
                    nameSpace = Application.GetNamespace("MAPI");
                    inboxFolder = mailItemfolder.Store.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder;

                    foreach (string entryId in entryIdsInConvn)
                    {
                        Outlook.MailItem mi = null;
                        try
                        {
                            mi = nameSpace.GetItemFromID(entryId, inboxFolder.StoreID) as Outlook.MailItem;
                            if (mi != null)
                            {
                                AddUserProperty(mi, mailUserProperty.Value);
                            }
                        }
                        finally
                        {
                            if (mi != null)
                                Marshal.ReleaseComObject(mi);
                        }
                    }
                    if (inboxFolder != null)
                        Marshal.ReleaseComObject(inboxFolder);
                    if (nameSpace != null)
                        Marshal.ReleaseComObject(nameSpace);
                }
            }

            if (mailUserProperty != null)
                Marshal.ReleaseComObject(mailUserProperty);
        }
        catch (Exception ex)
        { }
    }

    public void AddUserProperty(Outlook.MailItem mailItem, string Id)
    {
        Outlook.UserProperties mailUserProperties = null;
        Outlook.UserProperty mailUserProperty = null;

        try
        {
            mailUserProperties = mailItem.UserProperties;
            mailUserProperty = mailUserProperties.Find("ABC");

            if (null == mailUserProperty)
            {
                mailUserProperty = mailUserProperties.Add("ABC", Outlook.OlUserPropertyType.olText, false, 1);
            }

            mailUserProperty.Value = Id;
            mailItem.Save(); //Exception here
        }
        catch (Exception ex)
        {
             //The operation cannot be performed because the message has been changed
        }
        finally
        {
            if (mailUserProperty != null)
            {
                Marshal.ReleaseComObject(mailUserProperty);
            }

            if (mailUserProperties != null)
            {
                Marshal.ReleaseComObject(mailUserProperties);
            }
        }
    }

在这里我要回复邮件,然后添加用户属性并保存它,并保存在DraftItemAdd事件中,然后将用户属性添加到对话中的其他邮件中。

有人可以告诉我此异常的原因是什么? 预先感谢!

0 个答案:

没有答案