Outlook ItemAdd事件针对新的日历项目触发两次 [英] Outlook ItemAdd event fires twice for new Calendar items

查看:220
本文介绍了Outlook ItemAdd事件针对新的日历项目触发两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Outlook加载项,它将监视当前用户的日历并在收到特定类型的约会或会议时向该用户发送电子邮件.我们有一个第三方应用程序/服务正在向Outlook中的用户发送新的会议请求,但是没有通知登录到Outlook的用户.在我替换第三方应用程序之前,我的外接程序是一种解决方法,因此在发送此会议​​请求时可以提醒用户.

I'm working on an Outlook add-in that will monitor the current user's calendar and send that user an email when a specific type of appointment or meeting is received. We have a third party app/service that is sending new meeting requests to user in Outlook, but no notification is given to the user logged into Outlook. My add-in is a workaround until we replace the third party app, so users can be alerted when this meeting request is sent.

我正在使用ItemAdd事件来监视何时添加约会/会议(即从第三方应用发送).我看到的是,该事件触发两次(即使我只声明了一次处理程序):一次(当收到来自其他用户的约会时),一次()时该约会已被当前用户接受或暂时接受.

I'm using the ItemAdd event to monitor when an appointment/meeting is added (i.e. sent from the third party app). What I'm seeing is that the event fires twice (even though I declared the handler only once): once when the appointment is received from a different user, and once when the appointment is accepted or tentatively accepted by the current user.

我仅在第一次收到约会时才需要触发它,而不是在接受约会时才触发它.我可以监视用户的收件箱以查看他们是否已收到通知,但是如果他们在单击接受(服务器延迟?)之前尚未真正收到电子邮件,我认为这样做不会很好.

I need it to fire only when the appointment is at first received, not when it is accepted. I could monitor the user's inbox to see if they already received the notification, but I don't think this would work well if they haven't actually received the email before they click Accept (server latency?)

这是我的代码.任何想法将不胜感激.

Here is my code. Any ideas would be greatly appreciated.

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的值分配给类级别的变量,并在发送之前检查该值,则应该防止电子邮件被发送发送两次.我已经对该方法进行了一些测试,并且看来效果很好.这是我更新的代码:

If you assign the value for meetingItem.GlobalAppointmentID to a class level variable after sending the email, and check that value before sending, this should prevent the email from being sent twice. I've tested this method a bit and it seems to work well. Here is my updated code:

...

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;
        }

    }

这篇关于Outlook ItemAdd事件针对新的日历项目触发两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆