【发布时间】:2011-01-19 02:36:43
【问题描述】:
我正在为 Outlook 2007 创建一个插件,它会在收到邮件时读取它,然后重写它。该插件效果很好,并为没有将它们移动到另一个文件夹的 Outlook 规则的项目重写邮件。如果有规则,大约 50% 的时间仍然可以。另外 50% 的时间,规则会在我的插件完成之前移动邮件项目。我收到以下错误:
“无法执行该操作,因为该对象已被删除。”
我正在使用 NewMailEx 事件来调用我的重写函数:
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.NewMailEx += new Outlook.ApplicationEvents_11_NewMailExEventHandler(olApp_NewMail);
}
在 Outlook 2007 中,NewMailEx 提供邮件的 entryID。此 entryID 最初用于确定要使用的邮件对象:
Outlook.NameSpace outlookNS = this.Application.GetNamespace("MAPI");
Outlook.MAPIFolder mFolder = this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
Outlook.MailItem mail;
try
{
mail = (Outlook.MailItem)outlookNS.GetItemFromID(entryIDCollection, Type.Missing);
}
catch (Exception e) { Debug.WriteLine("exception with non-mail item " + entryIDCollection + ": " + e.ToString()); return; }
我认为我可以使用这个 entryID(上面的代码可以使用),并遍历我的所有文件夹(在交换机上以及在我的计算机上)寻找相同的邮件 ID。当我最终迭代到邮件所在的位置时,移动邮件的 EntryID 与 entryIDCollection 非常不同。
也许我做错了。有谁知道如何在我完成之前阻止事件传播,或者如何追踪移动的电子邮件?
如果有人好奇,这是我遍历文件夹的代码:
try
{
mail.Subject = new_subj;
mail.Body = "";
mail.HTMLBody = text;
mail.ClearConversationIndex();
mail.Save();
}
catch (Exception ex)
{
//It wasn't caught in time, so we need to find the mail:
ArrayList unreadFolders = new ArrayList();
foreach (Outlook.Folder f in outlookNS.Folders) unreadFolders.Add(f);
while (unreadFolders.Count > 0)
{
Outlook.Folder currentFolder = unreadFolders[0] as Outlook.Folder;
Debug.WriteLine("reading folder: " + currentFolder.Name);
unreadFolders.RemoveAt(0);
foreach (Outlook.Folder f in currentFolder.Folders) unreadFolders.Add(f);
try
{
Outlook.Items items = currentFolder.Items.Restrict("[UnRead] = true");
for (int itemNum = 1; itemNum <= items.Count; itemNum++)
{
if (!(items[itemNum] is Outlook.MailItem)) continue;
Outlook.MailItem m = items[itemNum];
if (m.EntryID == entryIDCollection)
{
m.Subject = new_subj;
m.Body = "";
m.HTMLBody = text;
m.ClearConversationIndex();
m.Save();
return;
}
}
}
catch (Exception exc) { }
}
}
【问题讨论】: