PTuckley February 2016

Exchange Web Services - accessing calendar of group mailbox using service account

I am trying to access a group mailbox using the Exchange Web Services.

A domain account with full access to the group mailbox has been setup, but I am getting the following 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.

My code is below:

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013);

service.UseDefaultCredentials = false;
service.Credentials = new WebCredentials("ServiceAccount", "<PASSWORD>");

//find web service url
service.AutodiscoverUrl("<GROUP MAILBOX EMAIL ADDRESS>", RedirectionUrlValidationCallback);

//find calendar ID
FolderId folderID = new FolderId(WellKnownFolderName.Calendar, new Mailbox("<GROUP MAILBOX EMAIL ADDRESS>"));

//find calendar
CalendarFolder calendar = CalendarFolder.Bind(service, folderID);

//create CalendarView
CalendarView view = null;

var propertySet = new PropertySet(
    AppointmentSchema.Id,
    AppointmentSchema.Subject,
    AppointmentSchema.Start,
    AppointmentSchema.End
);

view = new CalendarView(DateTime.Now.AddMonths(-1), DateTime.Now.AddMonths(1));
view.PropertySet = propertySet;

//get appointments
var appointments = calendar.FindAppointments(view);

Is there any way of achieving this without setting up a mailbox for the service account?

When I run the above code with my credentials, there are no issues (I have full permission to the group mailbox, and a mailbox of my own).

Answers


Jason Johnston February 2016

Use impersonation. This code works great for me to connect to a shared mailbox as a service account.

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
service.Credentials = new NetworkCredential(service_acct, password);

// Set impersonation to the shared mailbox
service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "sharedmbx@contoso.com");

Post Status

Asked in February 2016
Viewed 1,500 times
Voted 9
Answered 1 times

Search




Leave an answer