Put your mailbox on the Web with CDO v1.21

MS Exchange already comes with a nice Webmail facility called Outlook Web Access, so why would you want to make your own? Well, some people like to do these things just for the fun of it, and you may also learn something useful and interesting in the process.

Put your mailbox on the Web with CDO v1.21

 

 

Introduction:

 

There are currently two ways of accessing your Exchange mailbox from the Web; CDO (Collaboration Data Objects) and WSS (Web Storage System). WSS is only available to Exchange 2000, while CDO is available to Exchange 5.5, as well (and supplied with Exchange 2000 for backward compatibility). This example, therefore, will show you how to use CDO to access your mailbox, since, at the time of writing, Exchange 5.5 is still a widely-used messaging system. As a result, the example should work with both Exchange 5.5 and Exchange 2000.

 

 

Procedure:

 

First, though, a word of warning for Exchange 2000 users: You will need to make a few changes before trying this example, otherwise only accounts with Administrator rights will be able to use it. On the other hand, If you are an Administrator and just want to try it out, but are not planning to extend your work to your users in general, you do not need to make the changes.

 

The first change involves the creation of a registry key, so back up your registry before proceeding. You will need to create a directory (to which your users have write access) for the CDO temporary .mmp files, and then enter the name of this directory in the registry in a string value at the following location:

 

HKLM\SOFTWARE\Microsoft\Windows Messaging Subsystem

 

The string name is ProfileDirectory and it’s value is the name of the directory you are creating for this purpose (e.g. C:\WebTemp).

 

You will also need to give your users the ‘Log On Locally’ right on the server you are using. In an Active Directory domain, you do this in Domain Controller Security Policy, otherwise you do it in Local Security Policy.

 

In Exchange 5.5, you will be doing this on an existing OWA server, which will already have these changes made by the setup program.

 

Anyway, on to the code… The program 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. 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 look at some parts of it first. Following a small amount of preamble are the lines:

 

  mbx = Request("mbx")

  msg = Request("msg")

 

These lines work out what has been entered, or clicked on so far, and help the program decide what to do next. If mbx is not yet equal to anything, it displays a form to input a mailbox name and submit it back into the page.

 

  <form>

  Mailbox name:<br>

  <input name='mbx'><br>

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

  </form>

 

The output looks like this:

 

 

Fig. 1 – Getting the mailbox name.

 

 

When a mailbox name has been entered, the next part will be executed on the next visit to the page. This part contains a generic server name, which is SERVERNAME. You will need to replace this with your own Exchange server name. This might be a different server to your IIS server if you are using OWA 5.5, but will probably be the same server in OWA 2000. If you are using FE/BE Exchange 2000, use your BE server name.

 

  Set objSession = Server.CreateObject("MAPI.Session")

  lstrProfile = "SERVERNAME" & vbLf & mbx

  objSession.Logon "", "", False, True, 0, True, lstrProfile

 

This part logs onto the user’s mailbox. The first time this is executed, no mailbox link will have been clicked on, so hyperlinks to the most recent ten messages are displayed:

 

  Set objFolder = objSession.Inbox

  n = objFolder.Messages.Count

  For o = n To n - 9 Step -1

    Set objMsg = objFolder.Messages(o)

  %>

    <a href='CDO.asp?mbx=<% = mbx %>&msg=<% = objMsg.ID %>'><% = objMsg.Subject %></a><br>

  <%

  Next

 

The output looks like this:

 

 

Fig. 2 – CDO Message Listing.

 

 

Otherwise, if a link had been clicked on, some message details will be displayed, followed by an OK button to take the user back into the same page again.

 

  Set objMsg = objSession.GetMessage(msg)

  %>

  <form>

    <b>Sender:</b><br>

    <% = objMsg.Sender %>

    <p>

    <b>Subject:</b><br>

    <% = objMsg.Subject %>

    <p>

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

    <% = objMsg.Text %>

    <p>

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

    <input type='hidden' name='mbx' value='<% = mbx %>'>

  </form>

 

The output looks like this:

 

 

Fig. 3 – Some message details.

 

 

The last bit of the code does some tidying up and some postamble (I think I just made that word up, but I hope you understand what I mean).

 

To use the page, you need to put it in a directory on your OWA server that is protected by Authentication (Basic or Integrated), and away you go. In my example, the page is named CDO.asp, and is in a directory called test on a server named W2KS1, so the URL is http://W2KS1/test/CDO.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 OWA server. Remember to put your own Exchange server name in the line lstrProfile = "SERVERNAME" & vbLf & mbx

 

 

 

<% @LANGUAGE = "VBScript" %>

 

<html>

 

<body>

 

<%

' ASP/CDO Example by Lee Derbyshire.

 

' Retrieve any input from last page displayed

 

mbx = Request("mbx")

msg = Request("msg")

 

If mbx = "" Then

 

  ' Do this part if a mailbox name has NOT yet been entered

  ' Here we simply display an input box and finish

%>

 

  <form>

  Mailbox name:<br>

  <input name='mbx'><br>

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

  </form>

 

<%

Else

 

  ' Do this part if a mailbox name HAS been entered

  ' Here we log on to the user's mailbox

 

  Set objSession = Server.CreateObject("MAPI.Session")

  lstrProfile = "SERVERNAME" & vbLf & mbx

  objSession.Logon "", "", False, True, 0, True, lstrProfile

 

  If msg = "" Then

 

    ' Do this part if a message has NOT been selected

    ' Here we display links to the most recent 10 messages

 

    Set objFolder = objSession.Inbox

    n = objFolder.Messages.Count

    For o = n To n - 9 Step -1

      Set objMsg = objFolder.Messages(o)

%>

      <a href='CDO.asp?mbx=<% = mbx %>&msg=<% = objMsg.ID %>'><% = objMsg.Subject %></a><br>

<%

    Next

 

  Else

 

    ' Do this part if a message HAS been selected

    ' Here we get the message and display some details

 

    Set objMsg = objSession.GetMessage(msg)

%>

    <form>

      <b>Sender:</b><br>

      <% = objMsg.Sender %>

      <p>

      <b>Subject:</b><br>

      <% = objMsg.Subject %>

      <p>

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

      <% = objMsg.Text %>

      <p>

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

      <input type='hidden' name='mbx' value='<% = mbx %>'>

    </form>

<%

  End If

 

  ' Release any server objects from memory

 

  Set objFolder = Nothing

  Set objMsg = Nothing

 

  ' Log off the mailbox

 

  objSession.Logoff

  Set objSession = Nothing

 

End If

%>

 

</body>

 

</html>

 

About Lee Derbyshire

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

Click here for Lee Derbyshire's section.

Receive all the latest articles by email!

Get all articles delivered directly to your mailbox as and when they are released on MSExchange.org! Choose between receiving instant updates with the Real-Time Article Update, or a monthly summary with the Monthly Article Update. Sign up to the MSExchange.org Monthly Newsletter, written by Exchange MVP Henrik Walther, containing news, the hottest tips, Exchange links of the month and much more. Subscribe today and don't miss a thing!



Receive all the latest articles by email!

Receive Real-Time & Monthly MSExchange.org article updates in your mailbox. Enter your email below!
Click for Real-Time sample & Monthly sample

Become an MSExchange.org member!

Discuss your Exchange Server issues with thousands of other Exchange experts. Click here to join!

Solution Center

Readers' Choice

Which is your preferred Email Archiving solution?