Get the body of an incoming message in Outlook Addin

I want to process incoming messages from the exchange server and store them in my inbox. At the moment, I can get an alert for every incoming mail.

How can I get the body of a letter to process it?

public partial class ThisAddIn { private void ThisAddIn_Startup(object sender, System.EventArgs e) { this.Application.NewMail += new ApplicationEvents_11_NewMailEventHandler(AlertWhenNewMail); } void AlertWhenNewMail() { MessageBox.Show("New Email Recieved"); } private void ThisAddIn_Shutdown(object sender, System.EventArgs e) { } #region VSTO generated code private void InternalStartup() { this.Startup += new System.EventHandler(ThisAddIn_Startup); this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown); } #endregion } 

Also, how to save email and then just save it in your inbox?

+4
source share
3 answers

To navigate to the actual mailItem element, use the entry identifier passed in the newMailEx event. Your response to other posts suggests that this does not work for you in any way, but I assume we figured it out and provide you with sample code:

 void MyApplication_NewMailEx(string anEntryID) { Outlook.NameSpace namespace = this.GetNamespace("MAPI"); Outlook.MAPIFolder folder = this.Session.GetDefaultFolder( Outlook.OlDefaultFolders.olFolderInbox ); Outlook.MailItem mailItem = (Outlook.MailItem) outlookNS.GetItemFromID( anEntryID, folder.StoreID ); // ... process the mail item } 

To answer the second part of your question, as soon as you receive mail through this event, it has already been stored in your mailbox, so nothing is needed there. You saved it to disk using MailItem.SaveAs .

+5
source

Instead of the Application.NewMail event, try Application.NewMailEx with the EntryIDCollection parameter (A string representing the record identifier of the item received in the Inbox), with which you can receive a new email. The MSDN page has a simple example.

+2
source

Here you have the answer for Outlook 2010. One line of code in the NewMailEx event:

  void Application_NewMailEx(string EntryIDCollection) { Outlook.MailItem newMail = (Outlook.MailItem) Application.Session.GetItemFromID(EntryIDCollection, System.Reflection.Missing.Value); // do whatever you want with the new email... } 
+2
source

All Articles