Microsoft KB Archive/310225

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

Article ID: 310225

Article Last Modified on 12/3/2007

-

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

-



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 You receive unexpected errors when using ADO and ADO MD in a .NET Framework application



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 &quot;Summary&quot; section, follow these steps:  Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, click New, and then click Project. 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. Add a reference to the Microsoft CDO For Windows 2000 Library. To do this, follow these steps:  On the Project menu, click Add Reference. 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.</li> 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.</li></ol> </li>  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(&quot;C:\\Inetpub\\mailroot\\Drop&quot;); Console.WriteLine(&quot;Count: &quot; + 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,                     &quot;&quot;, &quot;&quot;);

stm.Type = ADODB.StreamTypeEnum.adTypeText; stm.Charset = &quot;US-ASCII&quot;;

CDO.IDataSource iDsrc; iDsrc = iMsg.DataSource;

iDsrc.SaveToObject(stm, &quot;_Stream&quot;); // TODO: modify the file path. stm.SaveToFile(&quot;C:\\temp\\Test.eml&quot;, ADODB.SaveOptionsEnum.adSaveCreateOverWrite);

iMsg = null; iMsgs = null; iDropDir = null; iDsrc = null; stm = null; }           catch (Exception e)            { Console.WriteLine(&quot;{0} Exception caught.&quot;, e); }           return; }        } } </li> Where TODO appears in the code, modify the code as indicated.</li> To build and run the program, press F5.</li></ol>

<div class="references_section">