【问题标题】:Outlook: detect when "Send/Receive" has finished after startupOutlook:启动后检测“发送/接收”何时完成
【发布时间】:2021-06-14 14:00:47
【问题描述】:

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

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

到目前为止我所尝试的:

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

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

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

  • 遍历NameSpace.SyncObjects,调用Start(),注册SyncEnd——这个方法根本不会触发。

什么是可靠的解决方案?

【问题讨论】:

    标签: c# outlook office-interop


    【解决方案1】:

    似乎有一个黑客可以检测同步何时完成;即按照 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 的本质,我还没有看到任何可以真正可靠地工作的免费替代方案,而这适用于我尝试过的所有用例中。所以这是我愿意接受的打击,以获得有效的解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-06
      • 1970-01-01
      • 2012-11-01
      • 1970-01-01
      相关资源
      最近更新 更多