outlook下载邮件正文

时间:2013-02-26 03:02:42

标签: c# com-interop outlook-2010

我正在创建一个发送短信的程序,然后根据我要执行特定操作的回复。无论如何,这是我的代码:

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 = "9546543930@att.txt.net")
{
        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对象库作为参考。

1 个答案:

答案 0 :(得分:0)

收到电子邮件后,我调用了发送和接收方法。稍等一下,然后我才能读到身体。

  outlook.Session.SendAndReceive(false);