代码之家  ›  专栏  ›  技术社区  ›  LoveFortyDown

Exchange Web Services-使用服务帐户访问组邮箱的日历

  •  0
  • LoveFortyDown  · 技术社区  · 10 年前

    我正在尝试使用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);
    

    有没有办法在不为服务帐户设置邮箱的情况下实现这一点?

    当我用我的凭据运行上述代码时,没有问题(我对组邮箱和我自己的邮箱拥有完全权限)。

    1 回复  |  直到 10 年前
        1
  •  2
  •   Jason Johnston    10 年前

    使用 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");