Exchange Web 服务 - 尝试读取消息属性时“您必须加载或分配此属性才能读取其值”错误消息

Jam*_*ish 3 c# exchangewebservices

我有一个邮箱,每 5 分钟从远程站收到一封自动电子邮件。该电子邮件中包含一个字符串,需要与前一封电子邮件中的相同字符串进行比较。

出于显而易见的原因,我正在尝试自动化此过程。

到目前为止,我能够阅读ConversationTopic电子邮件,但是,我似乎无法弄清楚如何阅读电子邮件的内容。

当它调用这个时:

email.Load();
MessageBox.Show(email.TextBody.Text.ToString());
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

You must load or assign this property before you can read its value

我有一个谷歌,我找不到任何与我的实例相关的东西,所以任何帮助都会很棒。

到目前为止,这是我的完整代码:

private void Form1_Load(object sender, EventArgs e)
{
    try
    {
        //MessageBox.Show("Registering Exchange connection");

        _service = new ExchangeService
        {
            Credentials = new WebCredentials("myaddy@domain.com", "*****")
        };
    }
    catch
    {
        MessageBox.Show("new ExchangeService failed.");
        return;
    }

    // This is the office365 webservice URL
    _service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");

    // Prepare seperate class for writing email to the database
    try
    {
        //MessageBox.Show("Reading mail");

        // Read 100 mails
        foreach (EmailMessage email in _service.FindItems(WellKnownFolderName.Inbox, new ItemView(10)))
        {
            if (email.ConversationTopic.ToString().Contains("from RockBLOCK 300234066454740"))
            {
                email.Load();
                MessageBox.Show(email.TextBody.Text.ToString());
            }
        }

        MessageBox.Show("Exiting");
    }
    catch (Exception ex)
    {
        MessageBox.Show("An error has occured. \n:" + ex.Message);
    }
}
Run Code Online (Sandbox Code Playgroud)

LuC*_*Cio 7

抛出异常是因为您正在尝试读取属性Item.TextBody。此属性不是一流的电子邮件属性

文件说:

并非所有重要的电子邮件属性和元素都是一流的属性和元素。若要获取其他属性或元素,您需要将它们添加到您的(PropertySet如果您使用 EWS 托管 API),或者使用属性路径将它们添加到您的 EWS 操作调用。例如,要检索文本正文 ... ,请创建您的PropertySet...

在你的情况下:

email.Load(new PropertySet(EmailMessageSchema.ConversationTopic, ItemSchema.TextBody));
Run Code Online (Sandbox Code Playgroud)

使用此请求EWS将LAOD,并返回一个EmailMessage与来自两个属性PropertySet

注意:
通过指定PropertySet您需要使用的属性,EWS 可以更快地处理您的请求,因为它不必搜索所有一流的电子邮件属性。此外,您不会遇到这样的错误,即您试图读取一个属性,该属性不是一流电子邮件属性的成员。