C# Language Microsoft.Exchange.WebServices Retrieve Specified User's Out of Office Settings

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!


First let's create an ExchangeManager object, where the constructor will connect to the services for us. It also has a GetOofSettings method, which will return the OofSettings object for the specified email address :

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);

We can now call this elsewhere like this:

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

Got any C# Language Question?