Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5841529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:54:06+00:00 2026-05-22T11:54:06+00:00

Summary: I am having trouble connecting to an exchange 2007 mailbox that is running

  • 0

Summary:

I am having trouble connecting to an exchange 2007 mailbox that is running FBA with my C# code using webdav.

Details:

This code below has to work against an exchange 2007 server, connect, and read email from a mailbox.

I’ve tried EWS but it made no sense and I couldn’t get it working.

Below is my code. I am using the outlook url to get the cookies I need to login via webdav to the exchange url.

Error:

440 login timeout on the request.

Area where my code breaks:

Response = (HttpWebResponse)Request.GetResponse();

Code:

namespace logintomailbox
{
    class Program
    {
        #region auth dont exapnd
        //Authentication to exchange 2007 with webdav and filebasedauth (FBA) in C#
        internal static string dUser = "user";
        internal static string dDomain = "domain";
        internal static string dPassword = "password";
        #endregion
        internal static string MailBoxAliasName = "mailbox;
        internal static string ExchangeServerName = "appews.host.com";
        internal static string outlookServerName = "outlook.host.com";
        internal static string ReadAttachments = "1"; //1 means read attachments, 0 means dont
        internal static string MailBoxEarliestDateToRead = "2011-01-05T00:00:00.000Z";//date of emails to read from

        static void Main(string[] args)
        {
            //FBA code
            //once i get a 302 response code i am authenticated
            DoExchangeFBA("https://" + outlookServerName, dDomain + "/" + dUser, dPassword);

            //login via webdav
            QueryMailBoxViaDAV();

            //exit application
            //ExitProgram((int)ExitReturnCodes.NormalShutdown);
        }
        private static void QueryMailBoxViaDAV()
        {
            //create http web request object
            System.Net.HttpWebRequest Request;
            //Request.CookieContainer = newCookieContainer();
            //create http web response object
            System.Net.WebResponse Response;

            //create needed components of web request/response
            byte[] bytes = null;
            System.IO.Stream RequestStream = null;
            System.IO.Stream ResponseStream = null;
            XmlDocument ResponseXmlDoc = null;
            string strRootURI = null;

            //check if exchange server is 2007 or 2003                   
            if (ExchangeServerName == "appews.host.com")
            {
                //exchange 2007
                strRootURI = "https://" + ExchangeServerName + "/exchange/" + MailBoxAliasName;
            }
            else
            {
                //exchange 2003
                //example of previously used url for exchange 2003 mailboxes
                strRootURI = "https://" + ExchangeServerName + "/exchange/" + MailBoxAliasName + "/Inbox";
            }

            //begin webdav query
            string strQuery = "<?xml version=\"1.0\"?><D:searchrequest xmlns:D=\"DAV:\" >"
                        + "<D:sql>SELECT \"DAV:displayname\", "
                        + "\"urn:schemas:mailheader:message-id\", "
                        + "\"urn:schemas:mailheader:date\", "
                        + "\"urn:schemas:mailheader:from\", "
                        + "\"urn:schemas:mailheader:to\", "
                        + "\"urn:schemas:mailheader:subject\", "
                        + "\"urn:schemas:httpmail:hasattachment\", "
                        + "\"urn:schemas:httpmail:textdescription\" "
                        + " FROM \"" + strRootURI + "\""
                        + "WHERE \"DAV:ishidden\" = false AND \"DAV:isfolder\" = false "
                        + "AND \"urn:schemas:mailheader:date\" > CAST(\"" + MailBoxEarliestDateToRead.ToString() + "\" as \"dateTime.tz\")"
                        + "</D:sql></D:searchrequest>";

            //build http web request
            Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(strRootURI);
            Request.KeepAlive = false;
            Request.Credentials = new System.Net.NetworkCredential(
               dUser,
               dPassword,
               dDomain);
            Request.Method = "SEARCH";

            //now that everything is created try sending a request
            try
            {
                bytes = Encoding.UTF8.GetBytes((string)strQuery);
                Request.ContentLength = bytes.Length;
                RequestStream = Request.GetRequestStream();
                RequestStream.Write(bytes, 0, bytes.Length);
                RequestStream.Close();

                Request.ContentType = "text/xml";
                Request.KeepAlive = true;
                Response = (HttpWebResponse)Request.GetResponse();
                ResponseStream = Response.GetResponseStream();
                ResponseXmlDoc = new XmlDocument();
                ResponseXmlDoc.Load(ResponseStream);

                ResponseStream.Close();
                Response.Close();

                Console.WriteLine("Authentication Successful");
                Console.ReadLine();
            }
            //catch all exceptions and sent to console if they occur
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
                Console.ReadLine();
            }
        }
        private static CookieCollection DoExchangeFBA(string server, string userName, string password)
        {

            var uri = server + "/owa/auth/owaauth.dll";

            var request = (HttpWebRequest)HttpWebRequest.Create(uri);
            request.Method = "POST";
            request.CookieContainer = new CookieContainer();
            request.ContentType = "application/x-www-form-urlencoded";
            request.AllowAutoRedirect = false;
            request.ServicePoint.Expect100Continue = false;

            server = HttpUtility.UrlEncode(server);
            userName = HttpUtility.UrlEncode(userName);
            password = HttpUtility.UrlEncode(password);
            var bodyString = "destination={0}&flags=0&username={1}";
            bodyString += "&password={2}&SubmitCreds=Log+On&";
            bodyString += "forcedownlevel=0&trusted=0";
            bodyString = string.Format(bodyString, server,
                                        userName, password);

            var body = Encoding.ASCII.GetBytes(bodyString);

            request.ContentLength = body.Length;
            ServicePointManager.Expect100Continue = false;

            var stream = request.GetRequestStream();
            stream.Write(body, 0, body.Length);
            stream.Close();

            //Console.WriteLine((HttpWebResponse)request.GetResponse());

            var response = (HttpWebResponse)request.GetResponse();

            if (response.Cookies.Count < 2) throw
                new AuthenticationException("Failed to login to OWA!");
            return response.Cookies;
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:54:07+00:00Added an answer on May 22, 2026 at 11:54 am

    HttpWebRequest pass credentials to next HttpWebRequest

    How to insert CookieCollection to CookieContainer?

    Why is one cookie missed?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Java newbie here, I'm having trouble setting a new line in this code: String
I'm having trouble finding a good summary of the advantages/disadvantages of using pgbouncer for
Summary I am having trouble getting UrlRewriteFilter up and running on Tomcat. Problem I
Having trouble getting this to work: /// <summary> /// Retrieve search suggestions from previous
I'm having trouble with this class, in particular the method: public IQueryable<T> ExecuteOrderBys<T>(this IQueryable<T>
I'm having trouble with a gradient drawing call. I have a Form that looks
Ok, This seems straight forward but I am having trouble finding the solution. I
I'm having trouble calling functions using add_action. here is my process: I add the
Summary New to iPhone programming, I'm having trouble picking the right optimization strategy to
Good Day, I'm new to this Youtube GData api and i'm having trouble with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.