Microsoft KB Archive/314383

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

Article ID: 314383

Article Last Modified on 11/29/2007

-

APPLIES TO


 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Exchange 2000 Server Standard Edition
 * Collaboration Data Objects for Exchange 2000
 * Microsoft ActiveX Data Objects 2.5

-



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 You receive unexpected errors when using ADO and ADO MD in a .NET Framework application



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.  Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. In the Visual Basic Projects types list, click Console Application.

By default, the Module1.vb file is created. Add a reference to the CDOEX Library. To do so, follow these steps:  On the Project menu, click Add Reference. Click the COM tab, locate Microsoft CDO for Exchange 2000 Library, and then click Select.</li> In the Add References dialog box, click OK.</li> If you are prompted to generate wrappers for the libraries that you selected, click Yes.</li></ol> </li> 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.</li>  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 = &quot;http://<ExchServer>/Exchange/<UserAlias>/Inbox&quot;

oCn.Provider = &quot;exoledb.datasource&quot; oCn.Open(sFdUrl, &quot;&quot;, &quot;&quot;, -1)

If oCn.State = 1 Then Console.WriteLine(&quot;Good Connection&quot;) Else Console.WriteLine(&quot;Bad Connection&quot;) Return End If

' TODO: You may want to retrieve more properties. Dim strSql As String strSql = &quot;&quot; strSql = &quot;select &quot; strSql = strSql + &quot; &quot;&quot;urn:schemas:mailheader:content-class&quot;&quot;&quot; strSql = strSql + &quot;, &quot;&quot;DAV:href&quot;&quot; &quot; strSql = strSql + &quot;, &quot;&quot;urn:schemas:mailheader:content-class&quot;&quot; &quot; strSql = strSql + &quot;, &quot;&quot;DAV:displayname&quot;&quot;&quot; strSql = strSql + &quot; from scope ('shallow traversal of &quot; + &quot;&quot;&quot;&quot;       strSql = strSql + sFdUrl + &quot;&quot;&quot;') &quot; strSql = strSql + &quot; WHERE &quot;&quot;DAV:ishidden&quot;&quot; = false&quot; strSql = strSql + &quot; AND &quot;&quot;DAV:isfolder&quot;&quot; = false&quot;

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(&quot;{0} : {1}&quot;, oField.Name, oField.Value) 'Next

' Get the item URL. Dim sURL As String oField = oFields.Item(&quot;DAV:href&quot;) 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, _         &quot;&quot;, &quot;&quot;)

Console.WriteLine(&quot;{0}&quot;, iMsg.Sender) Console.WriteLine(&quot;{0}&quot;, iMsg.Subject) Console.WriteLine(&quot;{0}&quot;, iMsg.TextBody)

' List the message fields. oFields = iMsg.Fields For i = 0 To oFields.Count - 1 oField = oFields.Item(i) Console.WriteLine(&quot;{0} : {1}&quot;, 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 </li> Search for the TODO text string in the code, and then modify the code for your environment.</li> Press the F5 key to build and to run the program.</li> Make sure that the messages have been retrieved from the Inbox.</li></ol>

Additional query words: Visual Basic VB .NET CDOEX ADODB exoledb Retrieve Messages

Keywords: kbhowto kbcode kbxml KB314383

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.