【发布时间】: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