locked
Accessing Exchange Server From Web Application RRS feed

  • Question

  • User-1772898816 posted

    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   
    
     
    Tuesday, July 15, 2008 11:38 AM

Answers

  • User214117797 posted

     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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 15, 2008 10:53 PM

All replies

  • User214117797 posted

     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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 15, 2008 10:53 PM
  • User-1772898816 posted
    Okay I have it working now, except for the fact that I get a timeout error on line 48. Any idea why?
    Wednesday, July 16, 2008 12:51 AM