简体   繁体   English

使用C#中的Exchange Web服务托管API检索错误的邮箱项目

[英]Wrong mailbox items being retrieved using Exchange Web Services managed API in C#

I'm trying to retrieve Inbox items from a specific mailbox (in which i have permissions), using Exchange Web Services managed API. 我正在尝试使用Exchange Web服务托管API从特定邮箱(我有权限)中检索收件箱项目。 I've tested the code first using my own email address via AutodiscoverUrl, and it works fine. 我首先使用我自己的电子邮件地址通过AutodiscoverUrl测试了代码,它运行正常。 However when i tried using the other email address, EWS still retrieves my own inbox items. 但是,当我尝试使用其他电子邮件地址时,EWS仍会检索我自己的收件箱项目。 Is this due to a cache or something? 这是由于缓存还是什么?

My code is as follows: 我的代码如下:

    ExchangeService ex = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
    ex.AutodiscoverUrl("someothermailbox@company.com");

    FindItemsResults<Item> findResults = ex.FindItems(WellKnownFolderName.Inbox, new ItemView(10));

    foreach (Item item in findResults.Items)
         Console.WriteLine(item.Subject);

The e-mail address given to AutodiscoverUrl has 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("someothermailbox@company.com")), 
    new ItemView(10));

If you have the permissions to impersonate the other user, you can impersonate the other user when connecting to the EWS and the following call to FindItem will work on the inbox of the impersonated user: 如果您具有模拟其他用户的权限 ,则可以在连接到EWS时模拟其他用户,并且以下对FindItem调用将在模拟用户的收件箱中起作用:

ExchangeService ex = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
ex.AutodiscoverUrl("someothermailbox@company.com");
ex.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "someothermailbox@company.com");
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服务器上进行实际测试。

if you want to send email using only delegates permission 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("user2@domain.com");

        EmailMessage email = new EmailMessage(service);
        email.ToRecipients.Add("user2@domain.com");
        email.Subject = "HelloWorld";
        email.Body = new MessageBody("Sent by using the EWS Managed API");

        //save it first!
        email.Save(new FolderId(WellKnownFolderName.Drafts, "user1@domain.com"));

        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地址。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 C# 和 Exchange Web Services Managed API 获取文件夹的策略? - How to get a folder's policy using C# and Exchange Web Services Managed API? 使用Exchange Web Services API确定邮箱是否存在 - Determine if mailbox exists using Exchange Web Services API Exchange Web服务托管API:访问其他用户项 - Exchange Web Services Managed API: Accessing other users items C# 交换 Web 服务托管 API 模拟 -&gt; Microsoft Graph ZDB974238714CA8DE634A7CE108A14F - C# Exchange Web Services Managed API Impersonation -> Microsoft Graph API 使用 EWS 托管 API 和 C# 从邮箱中搜索附件 - Search attachments from a mailbox using EWS Managed API and C# 如何使用Exchange Web服务(EWS)API通过共享邮箱发送电子邮件 - How to send email via a Shared MailBox using Exchange Web Services (EWS) API 使用 C# 禁用交换邮箱 - Disable an exchange mailbox using C# 使用C#在Exchange 2003中创建邮箱 - Create Mailbox in Exchange 2003 using C# Exchange Web服务-使用服务帐户访问组邮箱的日历 - Exchange Web Services - accessing calendar of group mailbox using service account 使用 Exchange web 服务读取 Office 365 邮箱 - Read an office 365 mailbox using Exchange web services
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM