Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

This article was previously published under Q314383
Caution ADO and ADO MD have not been fully tested in a Microsoft .NET Framework environment. They may cause intermittent issues, especially in service-based applications or in multithreaded applications. The techniques that are discussed in this article should only be used as a temporary measure during migration to ADO.NET. You should only use these techniques after you have conducted complete testing to make sure that there are no compatibility issues. Any issues that are caused by using ADO or ADO MD in this manner are unsupported. For more information, see the following article in the Microsoft Knowledge Base:
840667 (http://support.microsoft.com/kb/840667/ ) You receive unexpected errors when using ADO and ADO MD in a .NET Framework application

↑ Back to the top


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.

↑ Back to the top


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.

↑ Back to the top


Keywords: KB314383, kbxml, kbcode, kbhowto

↑ Back to the top

Article Info
Article ID : 314383
Revision : 4
Created on : 11/29/2007
Published on : 11/29/2007
Exists online : False
Views : 525