c# - 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
——这个方法根本不会触发。
这里有什么解决方案可以可靠地工作?
解决方案
似乎有一个黑客可以检测同步何时完成;即Application.Reminders.BeforeReminderShow
按照 DWE 在此 SO 答案中的建议覆盖
此事件(在我的测试中)总是在 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 的本质,我还没有看到任何可以声称可靠工作的免费替代方案,而这适用于我尝试过的所有用例。所以这是我愿意为获得可行的解决方案而付出的努力。
推荐阅读
- perl - Perl 脚本为 awk 命令引发语法错误
- java - 在应用订单之前操作存储在 Firebase 文档字段中的值
- android - Oculus Quest 的麦克风权限
- scala - 如何使用 spark/scala 从一个数据帧中获取不在另一个数据帧中的行
- java - 使用 Google Cloud Messaging 时无法解析方法 addOnCompleteListener
- django - 根据 Django Rest Framework 中的用户组路由到不同的数据库
- php - 无法在cakephp中显示上标
- python - 在 Django 模板中使用会话身份验证在 Django 中实现 Vue RESTful API 调用
- reactjs - 从父 React Navigation V5 打开子抽屉
- reactjs - 如何在 reactjs 中使用框架显示搜索输入?