Listing Inbox Contents Using ADO

Topic Last Modified: 2006-06-11

The following example demonstrates how to create a common item listing of a user's inbox. This list, or "view," is normally presented when a user selects a folder in their mailbox that contains messages.

Example

Visual Basic

Option Explicit

On Error GoTo ErrorHandler

' Reference to Microsoft ActiveX Data Objects 2.5 Library
' Reference to Microsoft CDO for Exchange 2000 Server Library

' Declare variables.
Dim Rec As New ADODB.Record
Dim Rs As New ADODB.Recordset
Dim strView As String
Dim strUser As String
Dim strURLInbox As String
Dim strURLMailbox As String
Dim Info As New ADSystemInfo
Dim Flds As ADODB.Fields
Dim Fld As ADODB.Field

' Set the user name.
strUser = "user"

' Build the mailbox URL for the user.
strURLMailbox = "file://./backofficestorage/" + Info.DomainDNSName + _
                "/MBX/" + strUser

' Get the inbox folder for the user's mailbox.
Rec.Open strURLMailbox
strURLInbox = Rec.Fields("urn:schemas:httpmail:inbox")

' Close the connection.
Rec.Close

' Open the user's inbox folder.
Rec.Open strURLInbox, Rec.ActiveConnection

' Build the search query.
strView = "select " _
        & "  ""DAV:href""" _
        & ", ""DAV:content-class""" _
        & ", ""urn:schemas:httpmail:datereceived""" _
        & ", ""DAV:isfolder""" _
        & ", ""DAV:getcontentlength""" _
        & ", ""urn:schemas:httpmail:from""" _
        & ", ""urn:schemas:httpmail:subject""" _
        & ", ""urn:schemas:mailheader:importance""" _
        & ", ""urn:schemas:httpmail:hasattachment""" _
        & ", ""urn:schemas:httpmail:read""" _
        & " from scope ('shallow traversal of """ _
        & strURLInbox & """') " _
        & " WHERE ""DAV:isfolder"" = false AND ""DAV:ishidden"" = false" _
        & " ORDER BY ""urn:schemas:httpmail:datereceived"" DESC"

' Open the recordset with the search query.
Rs.Open strView, Rec.ActiveConnection

' No messages in the inbox.
If Rs.RecordCount = 0 Then

Else
    ' Move to the first message.
    Rs.MoveFirst

    ' List all messages in the inbox.
    While Not Rs.EOF

        ' Get the message subject.
        Set Flds = Rs.Fields
        Set Fld = Flds("urn:schemas:httpmail:subject")

        ' List the message subject.
        Debug.Print Fld.Value
        Rs.MoveNext
    Wend
End If

' Clean up.
Rec.Close
Rs.Close

Set Rec = Nothing
Set Rs = Nothing
Set Flds = Nothing
Set Fld = Nothing
Exit Sub

ErrorHandler:

    ' Display error message.
    MsgBox Err.Description

    'Clean up.
    If Rec.State = adStateOpen Then Rec.Close
    Set Rec = Nothing
    If Rs.State = adStateOpen Then Rs.Close
    Set Rs = Nothing
    Exit Sub

C++

#include <stdio.h>
#include <conio.h>
#include <iostream.h>

#import "D:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
#import "D:\program files\common files\microsoft shared\cdo\cdoex.dll" no_namespace

struct StartOle
{
  StartOle() { CoInitialize(NULL); }
  ~StartOle() { CoUninitialize(); }
} _inst_StartOle;

void dump_com_error(_com_error &e)
{
  cout<< "An error occurred."<<endl;
  cout<< "Error code: " << e.Error()<< endl;
  cout<< "Error message: "<< e.ErrorMessage()<<endl;

}

HRESULT GetDomainName(BSTR * bstrDomainName);

void main()
{
  // Variables.
  _bstr_t strUser;
  _bstr_t strDomainName;
  _bstr_t strURLMailbox;
  FieldPtr Fld;
  FieldsPtr Flds;

  // Initialize variables.
  strUser = "user";
  strDomainName = "subdomain.example.com";
  strURLMailbox = "file://./backofficestorage/" + strDomainName + "/MBX/" + strUser;

  _ConnectionPtr Conn(__uuidof(Connection));
  _RecordPtr Rec(__uuidof(Record));
  _RecordsetPtr Rs(__uuidof(Recordset));

  HRESULT hr = S_OK;

  try
  {
    // Open a connection to the mailbox folder.
    Conn->Provider = "Exoledb.DataSource";
    hr = Conn->Open(strURLMailbox,"","",0);

    // Open the mailbox folder.
    hr = Rec->Open((_variant_t)strURLMailbox,
                  Conn->ConnectionString,
                  adModeReadWrite,
                  adFailIfNotExists,
                  adOpenSource,
                  bstr_t(), bstr_t());

    // Get the inbox folder of the mailbox.
    FieldsPtr Flds = Rec->GetFields();
    FieldPtr Fld = Flds->GetItem("urn:schemas:httpmail:inbox");
    _bstr_t strURLInbox = Fld->Value;

    // Close the connection.
    Rec->Close();

    // Open the inbox folder.
    hr = Rec->Open((_variant_t)strURLInbox,
                  Conn->ConnectionString,
                  adModeReadWrite,
                  adFailIfNotExists,
                  adOpenSource,
                  bstr_t(), bstr_t());

    // Build the search query.
    _bstr_t strView = "select "
                      "  \"DAV:href\""
                      ", \"DAV:content-class\""
                      ", \"urn:schemas:httpmail:datereceived\""
                      ", \"DAV:isfolder\""
                      ", \"DAV:getcontentlength\""
                      ", \"urn:schemas:httpmail:from\""
                      ", \"urn:schemas:httpmail:subject\""
                      ", \"urn:schemas:mailheader:importance\""
                      ", \"urn:schemas:httpmail:hasattachment\""
                      ", \"urn:schemas:httpmail:read\""
                      " from scope ('shallow traversal of \""
                      + strURLInbox + "\"') "
                      " WHERE \"DAV:isfolder\" = false AND \"DAV:ishidden\" = false"
                      " ORDER BY \"urn:schemas:httpmail:datereceived\" DESC";

    // Open the recordset with the search query.
    hr = Rs->Open(strView,
                  Conn->ConnectionString,
                  adOpenDynamic,
                  adLockReadOnly,
                  -1);

    // Check for messages in the inbox.
    if (Rs->RecordCount <= 0)
    {
      cout<<"There are no messages in the inbox."<<endl;
    }
    else
    {
      // Display the number of messages in the inbox.
      cout<<"Messages in the inbox: " << Rs->RecordCount <<endl;



      // Move to the first record in the recordset.
      Rs->MoveFirst();
      do
      {
         // Get the message subject.
         Flds = Rs->GetFields();
         Fld = Flds->GetItem("urn:schemas:httpmail:subject");

         // Display the message subject.
         cout<<(_bstr_t)Fld->Value<<endl;

         // Move to the next record.
         Rs->MoveNext();

      // Continue looping until there are no more messages.
      } while (!Rs->adoEOF);
    }

    // Close objects.
    Rec->Close();
    Rs->Close();
    Conn->Close();

    // Clean up.
    Flds = NULL;
    Fld = NULL;
    Rec = NULL;
    Rs = NULL;
    Conn = NULL;
  }
  catch(_com_error &e)
  {
    dump_com_error(e);
  }

  // Clean up.
  if (Rec)
    if (Rec->State == adStateOpen)
    {
      Rec->Close();
      Rec = NULL;
    }
  if (Rs)
    if (Rs->State == adStateOpen)
    {
      Rs->Close();
      Rs = NULL;
    }
  if (Conn)
    if (Conn->State == adStateOpen)
    {
      Conn->Close();
      Conn = NULL;
    }
}