2013-02-26 200 views
0

我正在創建一個程序,發送文本消息,然後根據答覆我想要執行特定的操作。反正這裏是我的代碼:展望下載電子郵件正文

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作爲參考。

回答

0

一旦我收到我稱之爲發送和接收方法的電子郵件,稍等一會兒,然後我就可以看到身體。

outlook.Session.SendAndReceive(false);