【发布时间】:2014-07-11 07:43:24
【问题描述】:
我们编写了一个 Outlook 加载项,可在发送电子邮件时启动一项操作。仅当在撰写电子邮件期间设置了标志时,才应执行该操作。通过单击切换按钮设置标志。当发送电子邮件时,会触发一个事件,我们将电子邮件的 ID 存储在队列中。如果邮件出现在已发送的文件夹中,则应该触发一个事件,并且如果在队列中发现相同的 id,则应该执行一个操作。
下面我有两种方法。 Application_ItemSend 将在发送电子邮件时发生,并且在该方法中 EnQueue 在 SentItemsQueue 上被调用。 EnQueue 方法将一个事件附加到 Sent Items 文件夹,当添加一个项目时,它应该触发一个事件来启动我们的操作。
在 Outlook 中编写和发送电子邮件时,这一切正常。如果我们从外部程序(如 Word)中启动电子邮件,则会执行 Application_ItemSend,但永远不会触发 EMailFoundInSentItems(附加在 EnQueue 中)。为什么事件从未触发?
public partial class ThisAddIn {
void Application_ItemSend(object item, ref bool cancel)
{
try
{
Trace.TraceInformation("E-mail is being sent. Checking for archive flag.");
MailItem mail = item as MailItem;
bool? archive = mail.GetArchiveFlag();
if (archive == true)
{
Trace.TraceInformation("Archive flag was set, going to queue e-mail for archiving.");
this.SentItemsQueue.EnQueue(mail);
}
Marshal.ReleaseComObject(mail);
}
catch (System.Exception ex)
{
Trace.TraceError("An exception was thrown while trying to archive a sent mail item. Exception: {0}.", ex.ToString());
}
}
...
public class SentItemsArchiveQueue
{
public void EnQueue(MailItem mail)
{
// remove and re-add handler (remove first, so it's not registered twice)
mail.SaveSentMessageFolder.Items.ItemAdd -= new ItemsEvents_ItemAddEventHandler(EMailFoundInSentItems);
mail.SaveSentMessageFolder.Items.ItemAdd += new ItemsEvents_ItemAddEventHandler(EMailFoundInSentItems);
this.Queue.Add(mail.ConversationIndex);
Trace.TraceInformation("Queue ConversationIndex is {0}", mail.ConversationIndex);
}
...
【问题讨论】:
-
是的,但 Dmitry 已经在 6 年前回答了我的问题????
标签: c# office-interop outlook-addin