How to modify a message by using Collaboration Data Objects for Exchange 2000 and an ActiveX Data Objects record in Visual C# (310204)



The information in this article applies to:

  • Microsoft Visual C# 2005
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)
  • Microsoft Exchange 2000 Server SP1
  • Collaboration Data Objects for Exchange 2000
  • ActiveX Data Objects (ADO) 2.5
  • Microsoft Exchange Server 2003 Enterprise Edition
  • Microsoft Exchange Server 2003 Standard Edition

This article was previously published under Q310204

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# .

back to the top

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

Modification Type:MinorLast Reviewed:10/4/2006
Keywords:kbMsg kbXML kbcode kbHOWTOmaster KB310204 kbAudDeveloper