Outlook:启动后检测“发送/接收”何时完成

问题描述

我正在用 C# 开发一个 Outlook 插件

我希望能够在启动后检测到 Outlook 何时完成了“发送/接收”选项,以便我可以对收到的邮件项目进行操作。

到目前为止我尝试过的:

  • 在启动时手动调用 Application.Session.SendAndReceive()。 这运行良好,但该方法在发送/接收完成之前返回,这与我想要的相反

  • 覆盖 Application.NewMail 和 Application.NewMailEx - 这些触发器在启动时都不可能希望触发(NewMailEx 根本不触发,NewMail 不可靠)

  • 调用 NameSpace.SyncObjects.AppFolders.Start();注册 SyncObjects.AppFolders.SyncEnd 事件 - 在 Outlook 完成下载邮件之前触发此事件

  • 遍历 NameSpace.SyncObjects调用 Start()注册 SyncEnd - 此方法根本不会触发。

这里有什么解决方案可以可靠地工作?

解决方法

似乎有一个hack可以检测同步完成时的情况;即按照 DWE 在 this SO answer here 中的建议覆盖 Application.Reminders.BeforeReminderShow 此事件(在我的测试中)总是在 Outlook 同步完成后触发。 然后,为了确保提醒窗口被触发,您在启动时添加一个新的提醒,然后在 Reminders_BeforeReminderShow 中再次隐藏该提醒

代码如下:

public partial class ThisAddIn
{

    private ReminderCollectionEvents_Event reminders; //don't delete,or else the GC will break things

    AppointmentItem hiddenReminder;

    private void ThisAddIn_Startup(object sender,EventArgs e)
    {
            //other stuff
            hiddenReminder = (AppointmentItem)Application.CreateItem(OlItemType.olAppointmentItem); //add a new silent reminder to trigger Reminders_BeforeReminderShow.This method will be triggered after send/receive is finished
            hiddenReminder.Start = DateTime.Now;
            hiddenReminder.Subject = "Autogenerated Outlook Plugin Reminder";
            hiddenReminder.Save();

            reminders = Application.Reminders;
            reminders.BeforeReminderShow += Reminders_BeforeReminderShow;
    }

    private void Reminders_BeforeReminderShow(ref bool Cancel)
    {
        if (hiddenReminder == null) return;

        bool anyVisibleReminders = false;
        for (int i = Application.Reminders.Count; i >= 1; i--)
        {
            if (Application.Reminders[i].Caption == "Autogenerated Outlook Plugin Reminder") //|| Application.Reminders[i].Item == privateReminder
            {
                Application.Reminders[i].Dismiss();
            }
            else
            {
                if (Application.Reminders[i].IsVisible)
                {
                    anyVisibleReminders = true;
                }
            }
        }

        Cancel = !anyVisibleReminders;
        hiddenReminder?.Delete();
        hiddenReminder = null;
            //your custom code here
    }

}

是的,这很笨拙,但这就是使用 Outlook 的本质,而且我还没有看到任何可以声称可靠工作的免费替代方案,而这适用于我尝试过的所有用例in。所以这是我愿意接受的一个打击以获得有效的解决方案。