Outlook ItemAdd event fires twice for new Calendar items

谁说胖子不能爱 提交于 2019-12-24 16:18:38

问题


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.

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

    }
    ....

回答1:


When a meeting request is received, Outlook creates a temporary tentative appointment. After you accept it, the first appointment is deleted and a new one is added, so it is no wonder the event fires twice.

Do you see the same behavior in OutlookSpy if you go to the Calendar folder, click Folder button, select Items property, click Browse, go to the Events tab and look at the log at the bottom of the tab?




回答2:


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

    }


来源:https://stackoverflow.com/questions/24150642/outlook-itemadd-event-fires-twice-for-new-calendar-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!