Article ID: 310225 - Last Review: December 3, 2007 - Revision: 4.2

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

This article was previously published under Q310225
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
Expand all | Collapse all

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:
http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx (http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx)
For additional information about how to use CDOSYS, click the following article numbers to view the articles in the Microsoft Knowledge Base:
310212  (http://support.microsoft.com/kb/310212/ ) How to use the Cdosys.dll library to send an e-mail with attachments
310221  (http://support.microsoft.com/kb/310221/ ) How to use the Cdosys.dll library to embed a message in a new message by using Visual C# .NET
310224  (http://support.microsoft.com/kb/310224/ ) How to use the Cdosys.dll library to process mail in the Drop directory by using Visual C# .NET

APPLIES TO
  • Microsoft Visual C# 2005
  • Microsoft Visual C# .NET 2003 Standard Edition
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Collaboration Data Objects 2.0
  • Microsoft ActiveX Data Objects 2.5
  • Microsoft ActiveX Data Objects 2.6
  • Microsoft ActiveX Data Objects 2.7
  • Microsoft Internet Information Services 6.0
  • Microsoft Internet Information Services 5.0
Keywords: 
kbcode kbhowto KB310225
 

Article Translations