我正在尝试使用服务帐户访问“通知”电子邮件地址的收件箱。此服务帐户具有与我的帐户相同的访问权限,但没有自己的电子邮件地址,它只是一个服务帐户。当我使用我的帐户时,以下测试代码有效,但是当我使用服务帐户时,我收到以下错误:
当您将请求作为没有邮箱的帐户发出时,您就是 必须为任何可分辨的邮箱主SMTP地址指定 文件夹ID。
此代码目前应该只显示收件箱中有多少封电子邮件为'test'。我已尝试过本网站上许多帖子的建议答案,但没有一个有效。谢谢你的帮助。以下是代码:
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
service.Credentials = new WebCredentials("ServiceAccount", "Password");
service.AutodiscoverUrl("ScriptNotifications@domain.com", RedirectionUrlValidationCallback);
ItemView mView = new ItemView(10);
mView.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Descending);
string querystring = "subject:\"test\"";
Console.WriteLine("Total emails whos subject is 'test':");
FindItemsResults<Item> results = service.FindItems(new FolderId(WellKnownFolderName.Inbox, new Mailbox("ScriptNotifications@domain.com")), querystring, mView);
Console.WriteLine(results.Items.Count.ToString());
更新:
我想添加Noonand的代码,因为这给了我一个不同的错误。编辑以适应一个块而不是使用方法,这里是代码:
ImpersonatedUserId impersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "smtp.mysmtpaddress.com");
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
service.Credentials = new NetworkCredential("ServiceAccount", "Password");
service.ImpersonatedUserId = impersonatedUserId;
service.AutodiscoverUrl("ScriptNotifications@domain.com", RedirectionUrlValidationCallback);
try
{
AlternateIdBase response =
service.ConvertId(new AlternateId(IdFormat.EwsId, "Placeholder", "ScriptNotifications@domain.com"), IdFormat.EwsId);
}
catch (ServiceResponseException)
{
// Expected exception, see EWS documentation
// Nonetheless, the credentials provided can authenticate successfully against this Exchange box
}
ItemView mView = new ItemView(10);
mView.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Descending);
string querystring = "subject:\"test\"";
Console.WriteLine("Total emails whos subject is 'test':");
FindItemsResults<Item> results = service.FindItems(new FolderId(WellKnownFolderName.Inbox, new Mailbox("ScriptNotifications@domain.com")), querystring, mView);
Console.WriteLine(results.Items.Count.ToString());
当我运行该代码时,我在“FindItemResults”行中收到以下错误:
SMTP地址没有与之关联的邮箱。
更新2:
感谢Noonand,我相信错误是Exchange Server 2013本身的一个错误。请参阅下面的链接:
https://support.microsoft.com/en-us/kb/3006861
我必须等到改变过程在我的公司进行之后再进行此更新,这可能需要很长时间,但我想把它放在那里,所以其他有同样问题的人可能会看到这个并得到帮助
答案 0 :(得分:0)
这样的事情对你有用。这是键入记事本(请原谅任何拼写错误)而不是编译,因为它只是为了让你沿着正确的道路前进。
public static ExchangeService ConnectToExchangeWithImpersonation(string userName, SecureString password, string impersonatedUserSmtpAddress, Uri serverUrl)
{
ImpersonatedUserId impersonatedUserId =
new ImpersonatedUserId(ConnectingIdType.SmtpAddress, impersonatedUserSmtpAddress);
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2) // Change as appropriate
{
Credentials = new NetworkCredential(userName, password);
ImpersonatedUserId = impersonatedUserId;
Url = serverUrl;
}
// Connected, now authenticate
try
{
AlternateIdBase response =
exchangeService.ConvertId(new AlternateId(IdFormat.EwsId, "Placeholder", userInformation.Username), IdFormat.EwsId);
}
catch (ServiceResponseException)
{
// Expected exception, see EWS documentation
// Nonetheless, the credentials provided can authenticate successfully against this Exchange box
}
return service;
}