檢索指定的使用者外出設定

首先讓我們建立一個 ExchangeManager 物件,建構函式將連線到我們的服務。它還有一個 GetOofSettings 方法,它將返回指定電子郵件地址的 OofSettings 物件:

using System;
using System.Web.Configuration;
using Microsoft.Exchange.WebServices.Data;

namespace SetOutOfOffice
{
    class ExchangeManager
    {
        private ExchangeService Service;

        public ExchangeManager()
        {
            var password = WebConfigurationManager.ConnectionStrings["Password"].ConnectionString;
            Connect("exchangeadmin", password);
        }
        private void Connect(string username, string password)
        {
            var service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
            service.Credentials = new WebCredentials(username, password);
            service.AutodiscoverUrl("autodiscoveremail@domain.com" , RedirectionUrlValidationCallback);
            
            Service = service;
        }
        private static bool RedirectionUrlValidationCallback(string redirectionUrl)
        {
            return redirectionUrl.Equals("https://mail.domain.com/autodiscover/autodiscover.xml");
        }    
        public OofSettings GetOofSettings(string email)
        {
            return Service.GetUserOofSettings(email);
        }            
    }
}

我們現在可以在其他地方這樣稱呼它:

var em = new ExchangeManager();
var oofSettings = em.GetOofSettings("testemail@domain.com");