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 retrieve messages by using CDOEX and ADO in Visual C#


View products that this article applies to.

This article was previously published under Q310206
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 Microsoft Collaboration Data Objects (CDO) for Exchange 2000 Library (CdoEx) in Microsoft Visual C# to retrieve messages from a user's Inbox.

Create the sample to retrieve messages

  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. Click Visual C# Projects under Project Types, and then click Console Application under Templates. By default, Class1.cs is created.

    Note In Visual Studio 2005, click Visual C# under Project Types. In Visual Studio .NET, Class1.cs is created by default. In Visual Studio 2005, Program.cs is created by default.
  4. Add a reference to the Microsoft CDO for Exchange 2000 Library and the Microsoft ActiveX Data Objects 2.5 Library. To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. Click the COM tab.
    3. Click Microsoft CDO for Exchange 2000 Library, and then click Select.

      Note In Visual Studio 2005, you do not have to click Select.
    4. Click Microsoft ActiveX Data Objects 2.5 Library, and then click Select.

      Note In Visual Studio 2005, you do not have to click Select.
    5. Click OK. If you are prompted to generate wrappers for the libraries that you selected, click Yes.
  5. In the Code window, replace the default code with the following code:
    using System;
    
    namespace Samples
    {
        class Class1
        {
            static void Main(string[] args)
            {
                try 
                {
                ADODB.Connection oCn = new ADODB.Connection();
                ADODB.Recordset oRs = new ADODB.Recordset();
    
                ADODB.Fields oFields;
                ADODB.Field oField;
    
                // TODO:
                string sFdUrl = "http://ExchServer/Exchange/UserAlias/Inbox";
    			
                oCn.Provider = "exoledb.datasource";
                oCn.Open(sFdUrl, "", "", -1);  
    
                if(oCn.State == 1)
                {
                    Console.WriteLine("Good Connection");
                }
                else
                {
                    Console.WriteLine("Bad Connection");
                }
    
    
                string strSql;
                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);
    
                // As an example, you only retrieve the first message.
                // You can use a while loop through each message.
    
                // Get the first message.
                oRs.MoveFirst();
    
                // Get Recordset fields.
                oFields = oRs.Fields;
    			
                string sUrl;
                oField = oFields["DAV:href"];
                sUrl = oField.Value.ToString();
    
                CDO.Message iMsg = 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);
    
                // Get message fields.
                oFields = iMsg.Fields;
    
    			
                for(int i = 0; i < oFields.Count; i++)
                {
                    oField = oFields[i];
                    Console.WriteLine("{0} : {1}", oField.Name, oField.Value);
                }
    
    
                oRs.Close();
                oCn.Close();
    			 
                oCn = null;
                oRs = null;
                oFields = null;
                oField = null;
                }
                catch (Exception e)
                {
                    Console.WriteLine("{0} Exception caught.", e);
                }			
            }
        }
    }
  6. Modify code for the TODO comments.
  7. Press F5 to build and to run the application.

↑ Back to the top


References

For more information, visit the following Microsoft Developer Network (MSDN) Web site:
Microsoft Office Development with Visual Studio
http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx

↑ Back to the top


Keywords: KB310206, kbhowtomaster

↑ Back to the top

Article Info
Article ID : 310206
Revision : 7
Created on : 11/29/2007
Published on : 11/29/2007
Exists online : False
Views : 487