Put Your Exchange 2000 Mailbox On The Web With ADO


Put Your Exchange 2000 Mailbox On The Web With ADO




Introduction:



In Exchange version 5, Microsoft introduced CDO (Collaboration Data Objects), a MAPI wrapper that enabled you to access the contents of the Exchange Information Store using Active Server Pages (ASP) technology. CDO is still available in Exchange 2000, for backward compatibility, but the preferred way of accessing IS data from the Web is now ActiveX Data Objects (ADO) in concert with the Web Storage System (WSS).



One great advantage of ADO/WSS is that you no longer need to create a Profile Directory on the server for the temporary files produced by CDO, and you no longer need to make sure that prospective users have the ‘Log On Locally’ right on the server. It is therefore much easier to implement, and you can also achieve quite a lot for comparatively little effort. You are basically setting up a connection to the Exchange IS as if it was any other database, and issuing SQL (Structured Query Language) statements to it.




Procedure:



To produce an Active Server Page of your own, you need nothing more than Notepad. You also need to be able to save the file onto the IIS server on which you want to run it. In this case, that IIS server will also be your Exchange 2000 server.



The program code presented in this article is written in VBScript, which is a scripting language supported by ASP (Active Server Pages). In ASP, you can mix HTML code and Script code on the same page. The Script is executed before the output it sent to the browser, so the user only sees the results of the program execution, not the script you created. The parts of the page that are Script are enclosed in delimiters <% … %> and these can be freely interspersed with the HTML source surrounding them.



I am using a single page for this example. It makes the page more complicated, but at least you get to see it all in one place. The page uses conditional program flow to decide which parts of the page are actually executed. If a mailbox has not yet been entered, it will ask for one. Then it will list the ten most recent messages in the mailbox, and then if you click on one of the links, it will display some of the message details.



The code is listed in its entirety at the end of the article, but let’s take a look at some parts of it first. Following a small amount of HTML preamble are the following lines:




  servername = Request.ServerVariables(“SERVER_NAME”)


  username = Request.ServerVariables(“REMOTE_USER”)


  p = Instr(username, “\”)


  If p <> 0 Then username = Right(username, Len(username) – p)




These lines get the server name and the user name, making sure that any domain name is removed from the user name. These are used to construct a string to be used to specify the data source for the ADODB Connection object:




  inboxURL = “http://” & servername & “/Exchange/” & username & “/Inbox/”


  Set conn = Server.CreateObject(“ADODB.Connection”)


  conn.Provider = “ExOLEDB.DataSource”


  conn.Open inboxURL




The first time the page is displayed, a list of hyperlinks to the ten most recent messages will be displayed. Clicking on a link takes us back into the same page, but with ?subject=<the subject of one of the messages> appended to the URL. The next line of code, therefore, decides if we are looking at the list of messages, or requesting the details for an individual message:




  subject  = Request.QueryString(“subject”)




The first time this is executed, the variable named ‘subject’ is empty, indicating that no mailbox link will have been clicked on, so hyperlinks to the most recent ten messages are displayed by the following code:




  Response.Write username & “<p>”



  strSQL = “SELECT “”urn:schemas:httpmail:subject””, “”DAV:displayname””” & _


   ” FROM SCOPE(‘shallow traversal of “”” & inboxURL & “””‘)” & _


   ” WHERE “”DAV:ishidden”” = False AND “”DAV:isfolder”” = False” & _


   ” ORDER BY “”urn:schemas:httpmail:date”” DESC”



  Set rs = conn.Execute(strSQL)



  n = 1


  Do While (Not rs.EOF) And (n <= 10)


    %><a href=’MBX.asp?subject=<% = Server.URLEncode(rs(“DAV:displayname”)) %>’>


    <% = Server.HTMLEncode(rs(“urn:schemas:httpmail:subject”)) %>


    </a><br>


    <% n = n + 1


  rs.MoveNext


  Loop


  Set rs = Nothing




The variable ‘strSQL’ contains the SQL statement that will retrieve the required IS records. Server.HTMLEncode and Server.URLEncode are used to make sure that the output from, and input to, the program are expressed in valid HTML character entities, otherwise it would not display properly. The output should look something like this:






Fig. 1 – Inbox Message Listing.




If a message link had been clicked on, the variable named ‘subject’ will not be empty, and some message details will be displayed, followed by an OK button to take the user back into the same page again. This time a Record object is used to retrieve the fields of an individual message:




  Set rec = Server.CreateObject(“ADODB.Record”)


  rec.Open inboxURL & subject, conn



  %><form>



  <b>From:</b> <% = Server.HTMLEncode(rec.Fields(“urn:schemas:httpmail:sendername”)) %><br>


  <b>Subject:</b> <% = Server.HTMLEncode(rec.Fields(“urn:schemas:httpmail:subject”)) %><br>



  <input type=’submit’ value=’OK’><br>



  <b>Message:</b><br>


  <% = Replace(Server.HTMLEncode(rec.Fields(“urn:schemas:httpmail:textdescription”)), vbCrLf, “<br>”) %>



  <% rec.Close : Set rec = Nothing



  %><p>


  <input type=’submit’ value=’OK’>



  </form>




The output is Server.HTMLEncoded to make sure it displays properly, and any CR/LFs need to be replaced by “<br>”,  otherwise they would not appear on the screen at all. The output looks like this:






Fig. 2 – Some message details.




The last bit of the code releases the memory used by the Connection object and outputs the closing HTML tags.



To use the page, you simply need to put it in a Virtual Directory on your Exchange server that is protected by Authentication (Basic or Integrated), and it is ready to use. In my example, the page is named MBX.asp, and is in a Virtual Directory called test on a server named W2KS1, so the URL is http://W2KS1/test/MBX.asp




About the author:



Lee Derbyshire BSc (Hons), MCSE is a full-time IT Professional living in the UK. You can visit his Web page at www.leederbyshire.com .




The Program Code:



Here is the example code in its entirety. You can copy it and paste it into Notepad to save it onto your Exchange server.


 





<% @LANGUAGE = “VBScript” %>



<html>


<body>



<% servername = Request.ServerVariables(“SERVER_NAME”)


username = Request.ServerVariables(“REMOTE_USER”)


p = Instr(username, “\”)


If p <> 0 Then username = Right(username, Len(username) – p)



inboxURL = “http://” & servername & “/Exchange/” & username & “/Inbox/”


Set conn = Server.CreateObject(“ADODB.Connection”)


conn.Provider = “ExOLEDB.DataSource”


conn.Open inboxURL



subject  = Request.QueryString(“subject”)


If subject = “” Then



  Response.Write username & “<p>”



  strSQL = “SELECT “”urn:schemas:httpmail:subject””, “”DAV:displayname””” & _


   ” FROM SCOPE(‘shallow traversal of “”” & inboxURL & “””‘)” & _


   ” WHERE “”DAV:ishidden”” = False AND “”DAV:isfolder”” = False” & _


   ” ORDER BY “”urn:schemas:httpmail:date”” DESC”



  Set rs = conn.Execute(strSQL)



  n = 1


  Do While (Not rs.EOF) And (n <= 10)


    %><a href=’MBX.asp?subject=<% = Server.URLEncode(rs(“DAV:displayname”)) %>’>


    <% = Server.HTMLEncode(rs(“urn:schemas:httpmail:subject”)) %>


    </a><br>


    <% n = n + 1


  rs.MoveNext


  Loop


  Set rs = Nothing



Else



  Set rec = Server.CreateObject(“ADODB.Record”)


  rec.Open inboxURL & subject, conn



  %><form>



  <b>From:</b> <% = Server.HTMLEncode(rec.Fields(“urn:schemas:httpmail:sendername”)) %><br>


  <b>Subject:</b> <% = Server.HTMLEncode(rec.Fields(“urn:schemas:httpmail:subject”)) %><br>



  <input type=’submit’ value=’OK’><br>



  <b>Message:</b><br>


  <% = Replace(Server.HTMLEncode(rec.Fields(“urn:schemas:httpmail:textdescription”)), vbCrLf, “<br>”) %>



  <% rec.Close : Set rec = Nothing



  %><p>


  <input type=’submit’ value=’OK’>



  </form>



<% End If



conn.Close : Set conn = Nothing



%></body>


</html>


About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top