使用 C# 中的 Exchange Web 服务托管 API 检索到错误的邮箱项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9220900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wrong mailbox items being retrieved using Exchange Web Services managed API in C#
提问by communista
I'm trying to retrieve Inbox items from a specific mailbox (in which i have permissions), using Exchange Web Services managed API. I've tested the code first using my own email address via AutodiscoverUrl, and it works fine. However when i tried using the other email address, EWS still retrieves my owninbox items. Is this due to a cache or something?
我正在尝试使用 Exchange Web 服务托管 API 从特定邮箱(我有权限)检索收件箱项目。我首先通过 AutodiscoverUrl 使用我自己的电子邮件地址测试了代码,它运行良好。但是,当我尝试使用其他电子邮件地址时,EWS 仍会检索我自己的收件箱项目。这是由于缓存还是什么?
My code is as follows:
我的代码如下:
ExchangeService ex = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
ex.AutodiscoverUrl("[email protected]");
FindItemsResults<Item> findResults = ex.FindItems(WellKnownFolderName.Inbox, new ItemView(10));
foreach (Item item in findResults.Items)
Console.WriteLine(item.Subject);
采纳答案by Jakob Christensen
The e-mail address given to AutodiscoverUrlhas nothing to do with which mailbox you are binding to.
提供给的电子邮件地址AutodiscoverUrl与您绑定的邮箱无关。
There are (at least) two ways to get the inbox items from another users mailbox: Delegate access and impersonation.
有(至少)两种方法可以从另一个用户邮箱中获取收件箱项目:委派访问和模拟。
If you have delegate access to the other users mailbox, you can specify the mailbox as a parameter in the call to FindItems:
如果您具有对其他用户邮箱的委托访问权限,则可以在调用中将邮箱指定为参数FindItems:
FindItemsResults<Item> findResults = ex.FindItems(
new FolderId(WellKnownFolderName.Inbox, new Mailbox("[email protected]")),
new ItemView(10));
If you have the permissions to impersonatethe other user, you can impersonate the other user when connecting to the EWS and the following call to FindItemwill work on the inbox of the impersonated user:
如果您有模拟其他用户的权限,您可以在连接到 EWS 时模拟其他用户,并且以下调用FindItem将在模拟用户的收件箱中工作:
ExchangeService ex = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
ex.AutodiscoverUrl("[email protected]");
ex.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "[email protected]");
ItemsResults<Item> findResults = ex.FindItems(WellKnownFolderName.Inbox, new ItemView(10));
Disclaimer: I have written the above code without actually testing it on a real Exchange server.
免责声明:我编写了上述代码,但并未在真正的 Exchange 服务器上进行实际测试。
回答by Asaf Magen
if you want to send email using only delegatespermission save the email first before sending it. it will set the smtp address that is required to send the message.
如果您只想使用委托权限发送电子邮件,请在发送前先保存电子邮件。它将设置发送消息所需的 smtp 地址。
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
service.Credentials = new WebCredentials("user1", "1234", "domain.com");
service.AutodiscoverUrl("[email protected]");
EmailMessage email = new EmailMessage(service);
email.ToRecipients.Add("[email protected]");
email.Subject = "HelloWorld";
email.Body = new MessageBody("Sent by using the EWS Managed API");
//save it first!
email.Save(new FolderId(WellKnownFolderName.Drafts, "[email protected]"));
email.Send();
i used it to avoid this error: "When making a request as an account that does not have a mailbox, you must specify the mailbox primary SMTP address for any distinguished folder Ids."
我用它来避免这个错误:“当作为没有邮箱的帐户发出请求时,您必须为任何可分辨文件夹 Id 指定邮箱主 SMTP 地址。”

