【问题标题】:C# and Exchange Web Service using a service account-Error使用服务帐户的 C# 和 Exchange Web 服务 - 错误
【发布时间】:2016-04-06 18:00:25
【问题描述】:

我正在尝试使用服务帐户访问“通知”电子邮件地址的收件箱。这个服务账号和我的账号有相同的访问权限,但是没有自己的邮箱,只是一个服务账号。当我使用我的帐户时,以下测试代码有效,但是当我使用服务帐户时,我收到以下错误:

当以没有邮箱的帐户发出请求时,您 必须为任何可区分的邮箱指定邮箱主 SMTP 地址 文件夹 ID。

此代码目前应该只显示收件箱中有多少封电子邮件的主题为“测试”。我已经尝试了该站点上许多帖子的建议答案,但没有一个有效。谢谢你的帮助。下面是代码:

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

我必须等到公司的变更流程完成后才能进行此更新,这可能需要很长时间,但我想将其发布出去,以便其他有相同问题的人可以看到并获得帮助.

【问题讨论】:

    标签: c# exchangewebservices


    【解决方案1】:

    这样的东西应该适合你。这是在记事本中输入的(请原谅任何拼写错误)而不是编译,因为它只是为了让您走上正确的道路。

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

    【讨论】:

    • 感谢您中午的尝试,但是,当我使用该代码时,我又遇到了另一个错误。代码见下文。
    • 如果您遇到该错误,请查看此处:support.microsoft.com/en-us/kb/3006861 我认为是时候循环您的 Exchange 管理员了 ;-)
    • 啊哈!这听起来正是问题所在。我会更新我的帖子以确保人们看到链接。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    • 1970-01-01
    相关资源
    最近更新 更多