【问题标题】:Read an office 365 mailbox using Exchange web services使用 Exchange Web 服务读取 Office 365 邮箱
【发布时间】:2020-12-01 10:57:24
【问题描述】:

我正在使用以下代码,但无法从 C# 代码读取我的 Office 365 收件箱电子邮件。一旦我得到这个工作,那么稍后我需要阅读来自共享邮箱的电子邮件。

如果有人可以帮助我解决此问题或指导我在这里缺少什么,我将不胜感激?

如果我将 Url 用作 Office365,则会收到此错误:“请求失败。远程服务器返回错误 401。未经授权”

如果我将 Url 用作 casx16 之一(在公司的 Q/A 门户中找到),则会收到此错误:“没有具有此类 GUID 的邮箱”

using Microsoft.Exchange.WebServices.Data;

public static void ReadMyMailbox_2()

        {

ExchangeService exchangeService = new ExchangeService();

exchangeService.Credentials = new WebCredentials("ajain", "password ", "MS");    ///  ajain is my MSID

// exchangeService.AutodiscoverUrl("a_jain@xyz.com", RedirectionUrlValidationCallback);

exchangeService.Url = new Uri("https://casx16.xyz.com/ews/exchange.asmx");

// exchangeService.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");

 

FolderId mailbox = new FolderId(WellKnownFolderName.Inbox, "a_jain@xyz.com");

ItemView itemView = new ItemView(10);

 

FindItemsResults<Item> result = exchangeService.FindItems(mailbox, itemView);

foreach (var msg in result)

{

EmailMessage message = EmailMessage.Bind(exchangeService, msg.Id);

                  Console.WriteLine(message.Subject);

}

        }

【问题讨论】:

  • 我建议使用 EWSEditor github.com/dseph/EwsEditor/releases 对其进行测试,这提供了针对 Office365 使用基本身份验证或 oAuth 的选项,您应该使用后者,因为基本身份验证正在被贬值。但这将告诉您是否可以访问您的邮箱。此外,如果您有 Office365 邮箱,为什么不直接使用 Graph API,您可以使用 Graph Explorer developer.microsoft.com/en-us/graph/graph-explorer 进行测试
  • 那是新东西..一定会试一试

标签: c# office365 exchangewebservices office365api


【解决方案1】:

您似乎正确设置了 ExchangeService 对象。有几件事可以尝试:

  1. 尝试将完整的电子邮件 (a_jain@xyz.com) 传递给 WebCredentials
  2. 确保您传递给 WebCredentials 的域是正确的
  3. 尝试只接受用户名和密码的 WebCredentials 构造函数。

注意:我认为自动发现不适用于 O365

【讨论】:

  • 肯定会尝试的。让我们看看。
  • 好的.. 尝试了所有 3 .. 与问题中提到的网址相同的错误。
猜你喜欢
  • 1970-01-01
  • 2016-10-29
  • 2015-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多