Outlook下载电子邮件正文 [英] outlook download email body

查看:227
本文介绍了Outlook下载电子邮件正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个发送文本消息的程序,然后根据我想执行特定操作的答复。无论如何,这是我的代码:

I am creating a program that sends a text message and then depending on the reply I want to perform a specific action. Anyways here is my code:

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);
}

这部分并不重要,但是我使用此功能发送短信 :

This part is not important but I send the "text message" with this function:

// 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分钟,然后尝试查看该主体,但它仍然为null。

So I am able to send an email with my gmail account and when I reply to the text message the function oApp_NewMailEx gets executed with the id of the new email. I am able to get the subject but the body does not get downloaded until I hover my mouse over the email or open the email. I already waitied 2 minutes on another thread and then tried to see the body and it was still null.

编辑注释以使这项工作有效:

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

It looks gray out because I did not run outlook as an administrator. If You run outlook as an admin you will be able to update the security settings.

我还导入了Microsoft Outlook 14.0对象库作为参考。

I also imported the Microsoft Outlook 14.0 Object library as a reference.

推荐答案

一旦收到电子邮件,我便称为send and receive方法。稍等片刻,然后我就可以读取正文了。

Once I received the email I called the send and receive method. wait a little and then I was able to read the body.

  outlook.Session.SendAndReceive(false);

这篇关于Outlook下载电子邮件正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆