C#和Exchange Web服务使用服务帐户错误

C#和Exchange Web服务使用服务帐户错误,c#,exchangewebservices,C#,Exchangewebservices,我正在尝试使用服务帐户访问“通知”电子邮件地址的收件箱。此服务帐户与我的帐户具有相同的访问权限,但没有自己的电子邮件地址,它只是一个服务帐户。当我使用我的帐户时,下面的测试代码起作用,但是当我使用服务帐户时,我得到以下错误: 当以没有邮箱的帐户进行请求时,您 必须为任何可分辨邮件指定邮箱主SMTP地址 文件夹ID 这段代码目前应该只显示收件箱中有多少封邮件主题为“测试”。我在这个网站上尝试了许多帖子的建议答案,但没有一个有效。谢谢你的帮助。代码如下: ExchangeService servic

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

当以没有邮箱的帐户进行请求时,您 必须为任何可分辨邮件指定邮箱主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());
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());
ExchangeService服务=新的ExchangeService(ExchangeVersion.Exchange2013\u SP1);
service.Credentials=新的WebCredentials(“ServiceAccount”、“Password”);
服务。自动发现URL(“ScriptNotifications@domain.com,重定向UrlValidationCallback);
ItemView mView=新的ItemView(10);
mView.OrderBy.Add(ItemSchema.DateTimeReceived,SortDirection.Descending);
string querystring=“subject:\“test\”;
Console.WriteLine(“主题为“测试”的电子邮件总数:”);
FindItemsResults=service.FindItems(新文件夹ID(WellKnownFolderName.Inbox,新邮箱()ScriptNotifications@domain.com”),查询字符串,mView);
WriteLine(results.Items.Count.ToString());
更新:

我想添加Noonand的代码,因为这给了我一个不同的错误。编辑以适应一个块而不是使用方法,下面是代码:

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());
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());
ImpersonatedUserId ImpersonatedUserId=新的ImpersonatedUserId(ConnectingIdType.SmtpAddress,“smtp.mysmtpaddress.com”);
ExchangeService服务=新的ExchangeService(ExchangeVersion.Exchange2013\u SP1);
service.Credentials=新的网络凭据(“ServiceAccount”、“Password”);
service.ImpersonatedUserId=ImpersonatedUserId;
服务。自动发现URL(“ScriptNotifications@domain.com,重定向UrlValidationCallback);
尝试
{
交替碱基反应=
service.ConvertId(新的AlternateId(IdFormat.EwsId,“占位符”)ScriptNotifications@domain.com),IdFormat.EwsId);
}
捕获(ServiceResponseException)
{
//预期异常,请参阅EWS文档
//尽管如此,提供的凭据仍可以针对此Exchange box成功进行身份验证
}
ItemView mView=新的ItemView(10);
mView.OrderBy.Add(ItemSchema.DateTimeReceived,SortDirection.Descending);
string querystring=“subject:\“test\”;
Console.WriteLine(“主题为“测试”的电子邮件总数:”);
FindItemsResults=service.FindItems(新文件夹ID(WellKnownFolderName.Inbox,新邮箱()ScriptNotifications@domain.com”),查询字符串,mView);
WriteLine(results.Items.Count.ToString());
当我运行该代码时,在“FindItemResults”行出现以下错误:

SMTP地址没有与其关联的邮箱

更新2:

多亏了Noonand,我相信这个错误是Exchange Server 2013本身的一个缺陷。请参见以下链接:


我必须等到我公司的变更流程完成后才能进行更新,这可能需要很长时间,但我想把它发布出来,这样其他有同样问题的人可以看到这一点并得到帮助。

类似的东西应该适合你。这是输入到记事本(请原谅任何打字错误)而不是编译,因为它只是为了让你沿着正确的道路前进

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

像这样的东西应该适合你。这是输入到记事本(请原谅任何打字错误)而不是编译,因为它只是为了让你沿着正确的道路前进

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

感谢您的尝试,但是,当我使用该代码时,会出现另一个错误。请参阅下面的代码。如果您遇到此错误,请参阅此处:我想是时候在中循环您的Exchange管理员了;-)啊哈!这听起来似乎正是问题所在。我将更新我的帖子,以确保人们看到链接。感谢您的尝试,但是,当我使用该代码时,我会遇到另一个错误。请参阅下面的代码。如果您遇到此错误,请参阅此处:我想是时候在中循环您的Exchange管理员了;-)啊哈!这听起来似乎正是问题所在。我会更新我的帖子,以确保人们看到链接。