Search code examples
emailoutlookvstooutlook-addinoutlook-web-addins

Outlook Add-In: how to simulate automatic reply with external service?


Background

I previously asked this question: Outlook Add-In: how to automatically reply based on custom logic?

As the answerers @EugeneAstafiev and @OutlookAdd-insTeam-MSFT noted, Outlook web add-ins can only run on a currently selected item. This does not work for an automatic reply use case, since automatic replies should send on receiving a new email, regardless of whether the user has selected it.

Question

Despite this, I know add-ins can categorize incoming mail, e.g. move/copy them to different folders. Would it be possible to detect incoming mail in that sense and then have the add-in call out to an external service to send an automatic reply in that way?

Example

For example:

  1. Add-in copies incoming email to folder.
  2. Add-in hits external API endpoint.
  3. External service sends auto reply email (perhaps using SMTP or similar?).

Clarifications

I'm not looking to use the built-in automatic replies feature in Outlook, since that has very limited options. (Roughly speaking, it can only send one message to all senders or one to contacts and another to non-contacts.) I want this simulated automatic reply to be sent based on custom logic, e.g. whether the external service has verified the sender's email.


Is this possible? If so, any recommendations on how best to implement it?

Thank you!


Solution

  • Yes, it is possible with COM based add-ins (VSTO based ones).

    The Application.NewMailEx event fires once for every received item that is processed by Microsoft Outlook. The item can be one of several different item types, for example, MailItem, MeetingItem, or SharingItem. The EntryIDsCollection string contains the Entry ID that corresponds to that item. Use the Entry ID specified in the EntryIDCollection string to call the NameSpace.GetItemFromID method and process the item.

    If you need to handle any specific folder in Outlook and process only items dropped to the folder you may use the Items.ItemAdd event which is fired when one or more items are added to the specified collection. Note, this event does not run when a large number of items are added to the folder at once (more than sixteen).

    You may consider your VSTO add-in as a regular windows .net application. So, standard mechanisms can be used to make a web call. For example, you may find the Calling Web Services with HttpWebRequest, WebClient and HttpClient article helpful.