我正在尝试使用Exchange Web服务访问组邮箱。
已设置具有对组邮箱的完全访问权限的域帐户,但我收到以下错误:
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.
我的代码如下:
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);
如果没有为服务帐户设置邮箱,有没有办法实现这一目标?
当我使用我的凭据运行上述代码时,没有问题(我拥有组邮箱的完全权限,以及我自己的邮箱)。
答案 0 :(得分:2)
使用impersonation。此代码非常适合我作为服务帐户连接到共享邮箱。
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");