我正在开发一个 Outlook 加载项,该加载项将监视当前用户的日历并在收到特定类型的约会或 session 时向该用户发送电子邮件。我们有一个第三方应用/服务正在向 Outlook 中的用户发送新的 session 请求,但不会向登录 Outlook 的用户发出任何通知。在我们更换第三方应用程序之前,我的加载项是一种解决方法,因此可以在发送此 session 请求时向用户发出提醒。
我使用 ItemAdd 事件来监视何时添加约会/ session (即从第三方应用程序发送)。我看到的是该事件触发两次(即使我只声明了处理程序一次):当从不同用户收到约会时一次,当从其他用户收到约会时一次当前用户已接受或暂时接受预约。
我只需要在第一次收到预约时触发它,而不是在接受预约时触发。我可以监视用户的收件箱以查看他们是否已经收到通知,但我认为如果他们在单击“接受”之前尚未实际收到电子邮件(服务器延迟?),那么这不会很好地工作。
这是我的代码。任何想法将不胜感激。
public partial class ThisAddIn
{
Outlook.Items _Appointments = null;
Outlook.Folder _MyAppointmentsFolder = null;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
// Initialization.
_MyAppointmentsFolder = (Outlook.Folder)this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
_Appointments = _MyAppointmentsFolder.Items;
_Appointments.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(appointments_Add);
}
private void appointments_Add(object item)
{
// An appointment has been added. Read the title and send an email based on a condition.
Outlook.AppointmentItem meetingItem = item as Outlook.AppointmentItem;
if (meetingItem.Subject.Contains("Service Call"))
{
// Let's send ourselves an email.
string emailTo = string.Format("{0}@concurrency.com", Environment.UserName);
string subject = meetingItem.Subject;
string body = meetingItem.Body;
string startDate = meetingItem.Start.ToString();
string endDate = meetingItem.End.ToString();
SendEmailAlert(emailTo, subject, body, startDate, endDate);
}
}
....
最佳答案
如果您在发送电子邮件后将 meetingItem.GlobalAppointmentID
的值分配给类级别变量,并在发送前检查该值,这应该可以防止电子邮件发送两次。我对此方法进行了一些测试,似乎效果很好。这是我更新的代码:
...
string _PreviousMeetingId = string.Empty; // class-level variable
...
private void appointments_Add(object item)
{
// An appointment has been added. Read the title and send an email based on a condition.
Outlook.AppointmentItem meetingItem = item as Outlook.AppointmentItem;
if (meetingItem.Subject.Contains("Service Call") && _PreviousMeetingId != meetingItem.GlobalAppointmentID)
{
// Let's send ourselves an email.
string emailTo = string.Format("{0}@concurrency.com", Environment.UserName);
string subject = meetingItem.Subject;
string body = meetingItem.Body;
string startDate = meetingItem.Start.ToString();
string endDate = meetingItem.End.ToString();
SendEmailAlert(emailTo, subject, body, startDate, endDate);
// Save the ID of the meeting so we can check it later (above).
_PreviousMeetingId = meetingItem.GlobalAppointmentID;
}
}
关于c# - Outlook ItemAdd 事件针对新日历项目触发两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24150642/