在Outlook Addin中获取传入电子邮件的正文

时间:2011-06-29 18:11:47

标签: c# email outlook

我想处理来自Exchange服务器的传入邮件并将其保存在我的邮箱中。截至目前,我可以收到每封收到邮件的提醒。

如何获取电子邮件的正文以进行处理?

   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
    }

另外,如何保存电子邮件,然后将其存储在收件箱文件夹中?

3 个答案:

答案 0 :(得分:5)

要获取实际的mailItem,请使用newMailEx事件中传递的entryID。您对其他帖子的回复表明这对您不起作用,但我会假设我们会解决这个问题并为您提供一些示例代码:

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
}

要回答问题的第二部分,一旦您通过此事件获取邮件项目,已经保存到收件箱中,因此无需执行任何操作。您可以使用MailItem.SaveAs将其保存到磁盘。

答案 1 :(得分:2)

而不是Application.NewMail事件,请尝试使用Application.NewMailEx为您提供参数EntryIDCollection(表示收件箱中收到的商品的条目ID的字符串),您应该可以使用该参数检索新电子邮件。 MSDN页面有一个简单的例子。

答案 2 :(得分:2)

这里有Outlook 2010的答案.NewMailEx事件中的一行代码:

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