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.