2

I am trying to read the Out Of Office status and message from other users in the organization from my c# program. We are running Exchange 2013 on premises.

This app is running as an Active Directory account (with it's own exchange mailbox), and I cannot use impersonation.

I have spent some time trying out the solutions to similar questions such as:

I'm trying to get something like:

public void checkOOF(string userEmail){
bool isOOF = checkstuff(userEmail);
string message;
if(isOOF)
   message = getOOFMessage(userEmail);
}

Please help me understand, thank you.

Community
  • 1
  • 1
Aaron
  • 1,390
  • 1
  • 16
  • 30
  • Lame downvotes with no comments? How can I improve the question with no feedback? – Aaron Aug 24 '15 at 22:21
  • 2
    I was looking into the same scenario with this being the most detailed post covering a selection of options. I've also no clue why anybody would have downvoted you since as you say no comments so no indication what's wrong. – Myzifer Aug 29 '17 at 08:08
  • 1
    @Myzifer I was able to get an exact solution to my question that I now use and I just posted it as an answer. Hope that might help you. – Aaron Aug 30 '17 at 16:15

3 Answers3

4

This is what I ended up using and it works.

public static string getOOM(string emailToCheck) //needs to be full email of user.
{
            string EWSurl = String.Format("https://{0}/EWS/Exchange.asmx", ExchangePath);
            WebRequest webRequest = WebRequest.Create(EWSurl);
            HttpWebRequest httpwebRequest = (HttpWebRequest)webRequest;
            httpwebRequest.Method = "POST";
            httpwebRequest.ContentType = "text/xml; charset=utf-8";
            httpwebRequest.ProtocolVersion = HttpVersion.Version11;
            httpwebRequest.Credentials = new NetworkCredential("user", "password", "domain");//service Account
            httpwebRequest.Timeout = 60000;
            Stream requestStream = httpwebRequest.GetRequestStream();
            StreamWriter streamWriter = new StreamWriter(requestStream, Encoding.ASCII);

            StringBuilder getMailTipsSoapRequest = new StringBuilder("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
            getMailTipsSoapRequest.Append(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ");
            getMailTipsSoapRequest.Append("xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" ");
            getMailTipsSoapRequest.Append("xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\"><soap:Header>");
            getMailTipsSoapRequest.Append(" <t:RequestServerVersion Version=\"Exchange2010\"/></soap:Header><soap:Body>");
            getMailTipsSoapRequest.Append("<GetMailTips xmlns=\"http://schemas.microsoft.com/exchange/services/2006/messages\">");
            getMailTipsSoapRequest.Append("<SendingAs>");
            getMailTipsSoapRequest.Append("<t:EmailAddress>accessingemail@domain.com</t:EmailAddress>");
            getMailTipsSoapRequest.Append("<t:RoutingType>SMTP</t:RoutingType></SendingAs>");
            getMailTipsSoapRequest.Append("<Recipients><t:Mailbox>");
            getMailTipsSoapRequest.Append("<t:EmailAddress>" + emailToCheck + "</t:EmailAddress>");
            getMailTipsSoapRequest.Append("<t:RoutingType>SMTP</t:RoutingType></t:Mailbox></Recipients>");
            getMailTipsSoapRequest.Append(" <MailTipsRequested>OutOfOfficeMessage</MailTipsRequested></GetMailTips>");
            getMailTipsSoapRequest.Append("</soap:Body></soap:Envelope>");

            streamWriter.Write(getMailTipsSoapRequest.ToString());
            streamWriter.Close();
            HttpWebResponse webResponse = (HttpWebResponse)httpwebRequest.GetResponse();

            StreamReader streamreader = new StreamReader(webResponse.GetResponseStream());
            string response = streamreader.ReadToEnd();
            if (response.Contains("<t:Message/>"))
                return null;
            int messageIndex = response.IndexOf("<t:Message>");
            response = response.Substring(messageIndex, response.IndexOf("</t:Message>") - messageIndex);
            return response;
}
akasoggybunz
  • 344
  • 7
  • 21
Aaron
  • 1,390
  • 1
  • 16
  • 30
  • Of note is that I found that "user" did not work for the credentials. I kept getting 401 unauthorized until I specified the full email address for the username, i.e., "user@company.com". – Tawab Wakil Jan 03 '18 at 22:31
  • Does one know what the credentials must be? It works for regular user accounts but not for accounts without an assigned postbox. You mentioned "service accounts" – Jens Kohl Oct 04 '18 at 13:25
  • @JensKohl Our "Service Account" was just a dedicated regular user account with a mailbox (AppName@consoto.com) – Aaron Nov 04 '18 at 23:00
1

http://blogs.msdn.com/b/devmsg/archive/2014/06/03/ews-how-to-retrieve-the-oof-out-of-facility-settings-message-using-ews-for-an-exchange-user.aspx This one takes as a paramater, urlname and I'm not sure where that url is coming from. This one seems the most promising though, any ideas where that comes from?

urlname is just the EWS URL if you are using the Managed API than just use the value from service.url.

http://gsexdev.blogspot.com/2011/11/using-mailtips-in-ews-to-get-oof-out-of.html I don't have reference to ExchangeServiceBinding, even though I'm using Microsoft.Exchange.WebServices.Data;

This is proxy code generated from the Exchange Web Service WSDL file see https://msdn.microsoft.com/en-us/library/office/dd877040(v=exchg.140).aspx (Microsoft.Exchange.WebServices.Data) is the Managed API.

Glen Scales
  • 20,495
  • 1
  • 20
  • 23
1

Solution worked great for me as supplied by Aaron. However returning the substring gave me issues since ours was an html string rather than plain text and it didn't parse correctly. So i replaced the StreamReader down portion with the following. This still returns as a string but correctly parses as the html it was returned as.

        XDocument doc;
        using (Stream responseStream = response.GetResponseStream())
        {
            doc = XDocument.Load(responseStream);
        }
        return doc.Root.Descendants().Where(d => d.Name.LocalName == "Message").Select(d => d.Value).FirstOrDefault();
tstrand66
  • 968
  • 7
  • 11