c# - Exchange Web 服务 – 尝试读取消息属性时出现 “You must load or assign this property before you can read its value” 错误消息

标签 c# exchangewebservices

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

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

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

当它调用这个时:

email.Load();
MessageBox.Show(email.TextBody.Text.ToString());

我收到以下错误:

您必须先加载或分配此属性,然后才能读取其值

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

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

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

最佳答案

抛出异常是因为您试图读取属性 Item.TextBody。此属性不是 first-class email property .

docs说:

Not all important email properties and elements are first-class properties and elements. To get the other properties or elements, you need to add them to your PropertySet if you're using the EWS Managed API, or use a property path to add them to your EWS operation call. For example, to retrieve the text body ... , create your PropertySet ...

在你的情况下:

email.Load(new PropertySet(EmailMessageSchema.ConversationTopic, ItemSchema.TextBody));

使用此请求,EWS 将加载并返回一个 EmailMessage,其中包含 PropertySet 中的两个属性。

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

关于c# - Exchange Web 服务 – 尝试读取消息属性时出现 “You must load or assign this property before you can read its value” 错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53044525/

相关文章:

c# - Wpf 统一网格项

java - 使用 IMAP 或 Exchange Web 服务从 GWT 应用程序的 Exchange Server 获取电子邮件的最快方法是什么?

c# - 复杂对象和模型绑定(bind)器 ASP.NET MVC

vb.net - Exchange Web 服务 API : Error Sending Message with Attachment

ms-office - 使用 Office 365 API 获取 CalendarView 时未返回 SeriesMaster

.net - 使用 CredentialCache.DefaultNetworkCredentials 代表当前用户发送电子邮件

php - 如何按对话对各个电子邮件进行分组,以便我确定(100%)知道一封电子邮件与另一封电子邮件在同一对话中?

javascript - 您好,我正在使用 ASP.NET 和 Javascript 计时器,如何防止在页面刷新时重置计时器

c# - .NET Core 中的 Crossgen 编译

c# - 如何获取 TeamExplorer Builds 的 ID