How to delete mail items by using an ADO recordset in Visual C# (310202)



The information in this article applies to:

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

This article was previously published under Q310202

INTRODUCTION

This article describes how to use ActiveX Data Objects (ADO) to delete objects by using an ADO recordset in Microsoft Visual C# . It includes a step-by-step procedure that shows you how to delete a mail item.

back to the top

Delete a mail item by using an ADO recordset

  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. On the File menu, click New and then click Project.
  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 Visual Studio 2005, by default, Program.cs is created.
  5. Add a reference to the Microsoft ActiveX Data Objects 2.5 Library. To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. In the Add References dialog box, click the COM tab, click Microsoft ActiveX Data Objects 2.5 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. 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.Recordset oRs = new ADODB.Recordset();
    
    				ADODB.Fields oFields;
    				ADODB.Field oField;
    
    				// TODO: Replace the folder URL with your folder URL.
    				string sFdUrl = "http://ExchServer/exchange/UserAlias/Inbox";
    			
    				oCn.Provider = "exoledb.datasource";
    				oCn.Open(sFdUrl, "", "", -1);  
    
    
    				string strSql;
    				strSql = "";
    				strSql = "select ";
    				strSql = strSql + " \"urn:schemas:mailheader:content-class\"";
    				strSql = strSql + ", \"DAV:href\" ";
    				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";
    				//TODO: Replace 'Test.eml' with the name of the mail that you want to delete.
    				strSql = strSql + " AND \"DAV:displayname\" = 'Test.eml'";  
    
    
    				oRs.Open(strSql, oCn,
    					ADODB.CursorTypeEnum.adOpenUnspecified,
    					ADODB.LockTypeEnum.adLockOptimistic,
    					1);
    
    				Console.WriteLine(oRs.RecordCount);	
       
    				oRs.MoveFirst();
    				while(!oRs.EOF)
    				{
    			
    					oFields = oRs.Fields;
    
    					oField = oFields["DAV:href"];
    					Console.WriteLine(oField.Value);
    
    					oField = oFields["DAV:displayname"];
    					Console.WriteLine(oField.Value);
    					oRs.Delete(ADODB.AffectEnum.adAffectCurrent);
    
    					oRs.MoveNext();  
    					Console.WriteLine("Item Deleted");
    					Console.WriteLine("--------------------------");
    
    				}
    
    				oCn.Close();
    
    				oCn = null;
    				oField = null;
    				oFields = null;
    			}
    			catch (Exception e)
    			{
    				Console.WriteLine("{0} Exception caught.", e);
    			}			
    		}
    	}
    }
    

  7. Modify the code accordingly where you see the TODO comment.
  8. Press F5 to build and to run the program.
  9. Verify that the objects were deleted from the folder.
back to the top

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