使用 Exchange Web 服务读取 Office 365 邮箱

问题描述 投票:0回答:2

我正在使用下面的代码,但无法从 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("[email protected]", 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, "[email protected]");

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

}

        }
c# office365 exchangewebservices office365api
2个回答
0
投票

看起来您正确设置了 ExchangeService 对象。有几件事可以尝试:

  1. 尝试将完整的电子邮件([电子邮件受保护])传递到 WebCredentials
  2. 确保您传递给 WebCredentials 的域正确
  3. 尝试仅接受用户名和密码的 WebCredentials 构造函数。

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


0
投票

您能否尝试在凭据中也传递您的域名? 代替: ExchangeService.Credentials = new WebCredentials("ajain", "密码", "MS");

大概是这样的: ExchangeService.Credentials = new WebCredentials("XYZ jain", "密码", "MS");

在cmd上运行“whoami”命令可以找到域名

© www.soinside.com 2019 - 2024. All rights reserved.