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 modify a message by using Collaboration Data Objects for Exchange 2000 and an ActiveX Data Objects record in Visual C#


View products that this article applies to.

This article was previously published under Q310204
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


Introduction

This article describes how to use the Microsoft Collaboration Data Objects (CDO) for Exchange 2000 Library to modify a message by using an ActiveX Data Objects (ADO) record in Microsoft Visual C# .

Modify a message by using an ADO record

  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, click New.
  3. Under Project Types, click Visual C# Projects.

    Note In Visual Studio 2005, click Visual C# under Project Types.
  4. Under Templates, double-click Console Application.

    By default, Class1.cs is created.

    Note In Microsoft Visual Studio 2005, Program.cs is created by default.
  5. Add a reference to the Microsoft CDO for Exchange 2000 Library. To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. In the Add Reference dialog box, click the COM tab, click Microsoft CDO for Exchange 2000 Library, and then click Select.

      Note In Visual Studio 2005, you do not have to click Select.
    3. Click OK to accept your selections.

      If you are prompted to generate wrappers for the libraries that you selected, click Yes.
  6. Follow steps 5a through 5c to add a reference to the Microsoft ActiveX Data Objects 2.5 Library.
  7. In the code window, replace all the code with the following code:
    using System;
    
    namespace Samples
    {
    	class Class1
    	{
    		static void Main(string[] args)
    		{
    			try 
    			{
    			ADODB.Connection oCn = new ADODB.Connection();
    			ADODB.Record oRc = new ADODB.Record();
    
    			ADODB.Fields oFields;
    			ADODB.Field oField;
    
                            // TODO: Replace the URL with your object URL.
    			string sItemUrl = "http://ExchServer/Exchange/UserAlias/Inbox/Test.eml";
    			
    			oCn.Provider = "exoledb.datasource";
    			oCn.Open(sItemUrl, "", "", -1); 
    
    			if(oCn.State == 1)
    			{
    				Console.WriteLine("Good Connection");
    			}
    			else
    			{
    				Console.WriteLine("Bad Connection");
    			}
    
    
    			oRc.Open(sItemUrl, oCn, 
    				ADODB.ConnectModeEnum.adModeReadWrite, 
    				ADODB.RecordCreateOptionsEnum.adFailIfNotExists,
    				ADODB.RecordOpenOptionsEnum.adOpenSource,
    				"", "");
    
    			
    			oFields = oRc.Fields;
    			
    			string sUrl;
    			oField = oFields["DAV:href"];
    			sUrl = oField.Value.ToString();
    
    			CDO.Message iMsg = new CDO.Message();
    			iMsg.DataSource.Open(sUrl, oCn, 
    				ADODB.ConnectModeEnum.adModeReadWrite,
    				ADODB.RecordCreateOptionsEnum.adFailIfNotExists,
    				ADODB.RecordOpenOptionsEnum.adOpenSource,
    				"", "");
    
    			Console.WriteLine("{0}", iMsg.From);
    			Console.WriteLine("{0}", iMsg.Subject);
    			Console.WriteLine("{0}", iMsg.TextBody);
    
                            // Modify some properties.
    			iMsg.Subject = "Modified : " + iMsg.Subject;
    			iMsg.Fields["urn:schemas:httpmail:read"].Value = true;
    			iMsg.Fields.Update();
    			iMsg.DataSource.Save(); 
    
    			oRc.Close();
    			oCn.Close();
    
    			iMsg = null;
    			oCn = null;
    			oRc = null;
    			oFields = null;
    			oField = null;
    			}
    			catch (Exception e)
    			{
    				Console.WriteLine("{0} Exception caught.", e);
    			}			
    		}
            }
    }
    					

  8. Modify the code accordingly where you see the TODO comment.
  9. Press F5 to build and to run the program.
  10. Verify that the specified properties were modified.

↑ Back to the top


Keywords: KB310204, kbhowtomaster, kbcode, kbxml, kbmsg

↑ Back to the top

Article Info
Article ID : 310204
Revision : 6
Created on : 11/29/2007
Published on : 11/29/2007
Exists online : False
Views : 523