展望下载电子邮件正文

问题描述:

我正在创建一个程序,发送文本消息,然后根据答复我想要执行特定的操作。反正这里是我的代码:展望下载电子邮件正文

using Microsoft.Office.Interop.Outlook; 
using Outlook = Microsoft.Office.Interop.Outlook; 

static void Main() 
{       
    var outlook = new Microsoft.Office.Interop.Outlook.Application(); 

    // fire event when a new email arives 
    outlook.NewMailEx += new ApplicationEvents_11_NewMailExEventHandler(oApp_NewMailEx); 

    // etc 
} 

static void oApp_NewMailEx(string EntryIDCollection) 
{ 
    var outlook = new Microsoft.Office.Interop.Outlook.Application(); 
    MailItem temp = (MailItem)outlook.Session.GetItemFromID(EntryIDCollection, Missing.Value); 

    var body = temp.Body; // the body of the email is null! I tried waiting and it is null until I open it... 
    Console.WriteLine(body); 
} 

这部分并不重要,但我用这个函数发送“短信”:

// send text message "att.txt.net only works with at&t phones" 
public static int SendEmail(string recipeint = "[email protected]") 
{ 
     try 
     { 
      // Create the Outlook application by using inline initialization. 
      Outlook.Application oApp = new Outlook.Application(); 


      //Create the new message by using the simplest approach. 
      Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem); 



      Outlook.Accounts accounts = oMsg.Session.Accounts; 
      foreach (Outlook.Account account in accounts) 
      { 
       // When the e-mail address matches, send the mail. 
       if (account.SmtpAddress.Contains("gmail")) 
       { 
         oMsg.SendUsingAccount = account;        
         break; 
       } 
      } 

      // If you want to, display the message. 
      oMsg.Display(true); //modal 


      //Add a recipient.     
      Outlook.Recipient oRecip = (Outlook.Recipient)oMsg.Recipients.Add(recipeint); 
      oRecip.Resolve(); 

      //Set the basic properties. 
      oMsg.Subject = "This is the subject of the test message"; 
      oMsg.Body = "This is the text in the message."; 


      //Add an attachment. 
      // TODO: change file path where appropriate 
      //String sSource = "C:\\setupxlg.txt"; 
      //String sDisplayName = "MyFirstAttachment"; 
      //int iPosition = (int)oMsg.Body.Length + 1; 
      //int iAttachType = (int)Outlook.OlAttachmentType.olByValue; 
      //Outlook.Attachment oAttach = oMsg.Attachments.Add(sSource, iAttachType, iPosition, sDisplayName); 



      //Send the message. 
      oMsg.Save(); 
      oMsg.Send(); 

      //Explicitly release objects. 
      oRecip = null; 
      //oAttach = null; 
      oMsg = null; 
      oApp = null; 
     } 

      // Simple error handler. 
     catch (System.Exception e) 
     { 
      Console.WriteLine("{0} Exception caught: ", e); 
     } 

     //Default return value. 
     return 0; 
} 

所以我能与我的Gmail帐户发送电子邮件,当我回复短信时,函数oApp_NewMailEx会以新电子邮件的ID执行。我能够获得该主题,但直到我将鼠标悬停在电子邮件或打开电子邮件之前,身体才得到下载。我已经在另一个线程上等待了2分钟,然后尝试查看机体,它仍然为空。


编辑笔记,使这项工作,我有:

enter image description here

它看起来灰,因为我没有运行Outlook为管理员。如果您以管理员身份运行outlook,则可以更新安全设置。

我还导入了Microsoft Outlook 14.0 Object Library作为参考。

一旦我收到我称之为发送和接收方法的电子邮件,稍等一会儿,然后我就可以看到身体。

outlook.Session.SendAndReceive(false);