How to use the CDOEX Library to retrieve messages from a user's Inbox by using Visual Basic .NET (314383)



The information in this article applies to:

  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Exchange 2000 Server
  • Collaboration Data Objects for Exchange 2000
  • ActiveX Data Objects (ADO) 2.5

This article was previously published under Q314383

SUMMARY

This article describes how to use the Microsoft Collaboration Data Objects (CDO) for Exchange 2000 (CDOEX) Library to retrieve messages from a user's Inbox by using Microsoft Visual Basic .NET.

MORE INFORMATION

To use the CDOEX Library to retrieve messages from a user's Inbox, follow these steps.

Note You must run this code on a computer that is running Microsoft Exchange 2000 Server.
  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. In the Visual Basic Projects types list, click Console Application.

    By default, the Module1.vb file is created.
  4. Add a reference to the CDOEX Library. To do so, follow these steps:
    1. On the Project menu, click Add Reference.
    2. Click the COM tab, locate Microsoft CDO for Exchange 2000 Library, and then click Select.
    3. In the Add References dialog box, click OK.
    4. If you are prompted to generate wrappers for the libraries that you selected, click Yes.
  5. Repeat step 4 to add a reference to the Microsoft ActiveX Data Objects 2.5 Library.

    Note In step 4b, replace Microsoft CDO for Exchange 2000 Library with Microsoft ActiveX Data Objects 2.5 Library.
  6. In the code window, replace the code with the following:
    Module Module1
    
        Sub Main()
            Dim oCn As ADODB.Connection = New ADODB.Connection()
            Dim oRs As ADODB.Recordset = New ADODB.Recordset()
    
            Dim oFields As ADODB.Fields
            Dim oField As ADODB.Field
    
            ' TODO: Replace with your folder URL
            Dim sFdUrl As String
            sFdUrl = "http://<ExchServer>/Exchange/<UserAlias>/Inbox"
    
            oCn.Provider = "exoledb.datasource"
            oCn.Open(sFdUrl, "", "", -1)  
    
            If oCn.State = 1 Then
                Console.WriteLine("Good Connection")
            Else
                Console.WriteLine("Bad Connection")
                Return
            End If
    
            ' TODO: You may want to retrieve more properties.
            Dim strSql As String
            strSql = ""
            strSql = "select "
            strSql = strSql + " ""urn:schemas:mailheader:content-class"""
            strSql = strSql + ", ""DAV:href"" "
            strSql = strSql + ", ""urn:schemas:mailheader:content-class"" "
            strSql = strSql + ", ""DAV:displayname"""
            strSql = strSql + " from scope ('shallow traversal of " + """"
            strSql = strSql + sFdUrl + """') "
            strSql = strSql + " WHERE ""DAV:ishidden"" = false"
            strSql = strSql + " AND ""DAV:isfolder"" = false"
    
    
            oRs.Open(strSql, oCn, _
             ADODB.CursorTypeEnum.adOpenUnspecified, _
             ADODB.LockTypeEnum.adLockOptimistic, 1)
    
            ' Example: Get the first message.
            ' A loop can also be written to iterate through all the messages.
    
             oRs.MoveFirst()
    
            ' get Recordset fields
            oFields = oRs.Fields
    
            ' List all fields.
            'Dim i As Integer
            'For i = 0 To oFields.Count - 1
            '    oField = oFields.Item(i)
            '    Console.WriteLine("{0} : {1}", oField.Name, oField.Value)
            'Next
    
    
            ' Get the item URL.
            Dim sURL As String
            oField = oFields.Item("DAV:href")
            sURL = oField.Value.ToString()
            Console.WriteLine(sURL)
    
            ' You can also open the item with the Message object.
            Dim iMsg As CDO.Message = New CDO.Message()
            iMsg.DataSource.Open(sURL, oRs.ActiveConnection, _
             ADODB.ConnectModeEnum.adModeReadWrite, _
             ADODB.RecordCreateOptionsEnum.adFailIfNotExists, _
             ADODB.RecordOpenOptionsEnum.adOpenSource, _
             "", "")
    
            Console.WriteLine("{0}", iMsg.Sender)
            Console.WriteLine("{0}", iMsg.Subject)
            Console.WriteLine("{0}", iMsg.TextBody)
    
    
            ' List the message fields.
            oFields = iMsg.Fields
            For i = 0 To oFields.Count - 1
                oField = oFields.Item(i)
                Console.WriteLine("{0} : {1}", oField.Name, oField.Value)
            Next
    
    
            oRs.Close()
            oCn.Close()
    
            oCn = Nothing
            oRs = Nothing
            oFields = Nothing
            oField = Nothing
    
            'Use the following line to persist the console window for examination.
            Console.ReadLine()
        
    End Sub
    
    End Module
    					
  7. Search for the TODO text string in the code, and then modify the code for your environment.
  8. Press the F5 key to build and to run the program.
  9. Make sure that the messages have been retrieved from the Inbox.

Modification Type:MinorLast Reviewed:8/25/2005
Keywords:kbhowto kbcode kbXML KB314383 kbAudDeveloper