How to use the Cdosys.dll library to save a message to a file by using Visual C# (310225)



The information in this article applies to:

  • Microsoft Visual C# 2005
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)
  • Microsoft Collaboration Data Objects for Windows 2000
  • ActiveX Data Objects (ADO) 2.5
  • ActiveX Data Objects (ADO) 2.6
  • ActiveX Data Objects (ADO) 2.7
  • Microsoft Internet Information Services version 6.0
  • Microsoft Internet Information Services 5.0

This article was previously published under Q310225

SUMMARY

This article describes how to use the Collaboration Data Objects (CDO) for Windows 2000 library (Cdosys.dll) to save a message to a file in Microsoft Visual C#.

Note The Cdosys.dll library is also known as CDOSYS.

MORE INFORMATION

To use CDOSYS as described in the "Summary" section, follow these steps:
  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, and then click Console Application under Templates. By default, Class1.cs is created.

    Note In Microsoft Visual C# 2005, click Visual C# under Project Types. By default, Program.cs is created.
  4. Add a reference to the Microsoft CDO For Windows 2000 Library. To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. On the COM tab, locate Microsoft CDO For Windows 2000 Library, and then click Select.

      Note In Microsoft Visual C# 2005, you do not have to click Select.
    3. To accept your selections, click OK in the Add References dialog box.

      If you receive a message to generate wrappers for the libraries that you selected, click Yes.
  5. In the code window, replace all the code with the following code:
    namespace CdoSys
    {
    	using System;
    	class Class1
    	{
    		static void Main(string[] args)
    		{
    			try 
    			{			
    			CDO.DropDirectory iDropDir = new CDO.DropDirectory();
    			CDO.IMessages iMsgs;
    			CDO.IMessage iMsg;
    			
    
    			iMsgs = iDropDir.GetMessages("C:\\Inetpub\\mailroot\\Drop");
    			Console.WriteLine("Count: " + iMsgs.Count);
    
    			// Get first message.
    			iMsg = iMsgs[1];
    
    			ADODB.Stream stm = new ADODB.Stream();
    			stm.Open(System.Reflection.Missing.Value, ADODB.ConnectModeEnum.adModeUnknown,
    				     ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified,
    				     "", "");
    
    			stm.Type = ADODB.StreamTypeEnum.adTypeText;
    			stm.Charset = "US-ASCII";
    
    			CDO.IDataSource iDsrc;
    			iDsrc = iMsg.DataSource;
    
    			iDsrc.SaveToObject(stm, "_Stream");
                               // TODO: modify the file path.
    			stm.SaveToFile("C:\\temp\\Test.eml", ADODB.SaveOptionsEnum.adSaveCreateOverWrite);
    
    
    			iMsg = null;
    			iMsgs = null;
    			iDropDir = null;
    			iDsrc = null;
    			stm = null;
    			}
    			catch (Exception e)
    			{
    				Console.WriteLine("{0} Exception caught.", e);
    			}
    			return;
    		}
             }
    }
  6. Where TODO appears in the code, modify the code as indicated.
  7. To build and run the program, press F5.

REFERENCES

For more information about Microsoft Office Development with Visual Studio, see the following Microsoft Developer Network (MSDN) Web site: For additional information about how to use CDOSYS, click the following article numbers to view the articles in the Microsoft Knowledge Base:

310212 How to use the Cdosys.dll library to send an e-mail with attachments

310221 How to use the Cdosys.dll library to embed a message in a new message by using Visual C# .NET

310224 How to use the Cdosys.dll library to process mail in the Drop directory by using Visual C# .NET


Modification Type:MinorLast Reviewed:10/4/2006
Keywords:kbcode kbhowto KB310225 kbAudDeveloper