I want to process incoming mails coming from an exchange server and save it in my mail box. As of now I can get an alert for every incoming mail.
How can I get the body of the email 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 the email and then just store it in the inbox folder?
To get to the actual mailItem, use the entryID passed in the newMailEx event. Your response to other posts suggests this doesn't work for you somehow, but I'll assume we'll get that sorted out and provide you some example 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, once you get hold of the mail item through this event it has already been saved into your inbox, so no need to do anything there. You'd save it to disk using MailItem.SaveAs.
Instead of Application.NewMail
event, try Application.NewMailEx
with gives you a parameter EntryIDCollection
(A string representing an Entry ID of an item received in the Inbox) with which you should be able to retrieve the new email. MSDN page has a simple example.
Here you have the answer for Outlook 2010. One line of code in your 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...
}
精彩评论