1

I am trying to access the inbox of a "Notification" email address using a service account. This service account has the same access permissions as my account, but does not have its own email address, it is simply a service account. When I use my account, the below test code works, however when I use the service account I get the below 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.

This code currently should simply display how many emails in the inbox has the subject of 'test'. I have tried the suggested answer of many posts on this site, and none have worked. Thanks for your help. Below is the code:

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());

UPDATE:

I wanted to add Noonand's code, since that gives me a different error. Editing to fit into one block rather than using methods, here is the code:

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());

When I run that code, I get the below error at the "FindItemResults" line:

The SMTP address has no mailbox associated with it.

UPDATE 2:

Thanks to Noonand, I believe the error is a bug in Exchange Server 2013 itself. See below for the link:

https://support.microsoft.com/en-us/kb/3006861

I have to wait until the change process goes through in my company before I make this update, which could take a long time, but I wanted to put this out there so others who have the same problem may see this and be helped.

Sean S
  • 15
  • 7

1 Answers1

0

Something like this should work for you. This was typed into Notepad (please forgive any typos) and not compiled, as it's just intended to get you travelling down the right path.

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;
}
noonand
  • 2,763
  • 4
  • 26
  • 51
  • 1
    Thanks for the attempt noonand, however, when I use that code I get another error. See below for the code. – Sean S May 31 '16 at 19:11
  • If you're getting that error then see here: https://support.microsoft.com/en-us/kb/3006861 Time to loop your Exchange admin in I think ;-) – noonand Jun 07 '16 at 08:38
  • 1
    AH HA! This sounds like it is exactly what the issue is. I will update my post to make sure that people see the link. – Sean S Jun 08 '16 at 15:35