Accessing Exchange Server From Web Application

Last post 07-16-2008 12:51 AM by dcwhisen. 2 replies.

Sort Posts:

  • Accessing Exchange Server From Web Application

    07-15-2008, 11:38 AM
    • Loading...
    • dcwhisen
    • Joined on 10-30-2007, 4:17 PM
    • Cullowhee, NC
    • Posts 141

    I found this code online just to create an outlook calendar folder. The following code is throwing an error saying that the host name cannot be resolved. Not sure where to go from here. I konw what my exchange server address is, but can't get it to connect. Any suggestions?

    1    using System;
    2    using System.Collections.Generic;
    3    using System.Text;
    4    using System.Net;
    5    using System.IO;
    6    
    7    namespace ExchangeFun
    8    {
    9        class Program
    10       {
    11           static void Main(string[] args)
    12           {
    13               try
    14               {
    15                   String sUri = "http://MBX71.ad2.softcom.biz/exchange/chris whisenhunt/inbox";
    16                   Uri myUri = new Uri(sUri);
    17                   HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(myUri);
    18   
    19                   String sQuery = "<?xml version='1.0'?>" +
    20                   "<a:propertyupdate xmlns:a='DAV:' " +
    21                   "xmlns:ex='http://schemas.microsoft.com/exchange/'>" +
    22                   "<a:set><a:prop>" +
    23                   "<ex:outlookfolderclass>IPF.Appointment</ex:outlookfolderclass>" +
    24                   "</a:prop></a:set>" +
    25                   "</a:propertyupdate>";
    26   
    27                   String userName = "";
    28                   String password = "";
    29                   NetworkCredential networkCredential = new NetworkCredential(userName, password);
    30                   CredentialCache credentialCache = new CredentialCache();
    31                   credentialCache.Add(myUri, "Basic", networkCredential);
    32                   httpWebRequest.Credentials = credentialCache;
    33   
    34                   httpWebRequest.KeepAlive = false;
    35                   httpWebRequest.Headers.Set("Pragma", "no-cache");
    36                   httpWebRequest.ContentType = "text/xml";
    37                   httpWebRequest.ContentLength = sQuery.Length;
    38   
    39                   httpWebRequest.Timeout = 300000;
    40                   httpWebRequest.Method = "MKCOL";
    41   
    42                   byte[] byteQuery = Encoding.ASCII.GetBytes(sQuery);
    43                   httpWebRequest.ContentLength = byteQuery.Length;
    44                   Stream stream = httpWebRequest.GetRequestStream();
    45                   stream.Write(byteQuery, 0, byteQuery.Length);
    46                   stream.Close();
    47   
    48                   HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    49                   String status = httpWebResponse.StatusCode.ToString();
    50                   Console.WriteLine("Status Code: {0}", status);
    51                   String reqHeaders = httpWebRequest.Headers.ToString();
    52                   Console.WriteLine(reqHeaders);
    53   
    54                   Stream stream2 = httpWebResponse.GetResponseStream();
    55                   StreamReader streamReader = new StreamReader(stream2);
    56                   String text = streamReader.ReadToEnd();
    57                   Console.WriteLine("Response: {0}", text);
    58                   streamReader.Close();
    59   
    60                   networkCredential = null;
    61                   credentialCache = null;
    62                   httpWebResponse = null;
    63                   httpWebRequest = null;
    64                   stream = null;
    65                   stream2 = null;
    66                   streamReader = null;
    67               }
    68               catch (Exception e)
    69               {
    70                   Console.WriteLine("{0} Exception caught.", e);
    71                   Console.ReadLine();
    72               }
    73           }
    74   
    75           static void Main(string[] args)
    76           {
    77   
    78           }
    79       }
    80   }
    81   
    
     
    If my reply helps answer your question then please mark as answer.
  • Re: Accessing Exchange Server From Web Application

    07-15-2008, 10:53 PM
    Answer
    • Loading...
    • jimmy q
    • Joined on 11-02-2006, 9:01 AM
    • Australia
    • Posts 2,489
    • Moderator

     First make sure you have configured you exchange to allow web mail, then make sure the account you are logging as have read permissions to the mail box you are trying to access.

  • Re: Accessing Exchange Server From Web Application

    07-16-2008, 12:51 AM
    • Loading...
    • dcwhisen
    • Joined on 10-30-2007, 4:17 PM
    • Cullowhee, NC
    • Posts 141
    Okay I have it working now, except for the fact that I get a timeout error on line 48. Any idea why?
    If my reply helps answer your question then please mark as answer.
Page 1 of 1 (3 items)
Microsoft Communities
Page view counter