Microsoft KB Archive/310221

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

Article ID: 310221

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 5.0
 * Microsoft Internet Information Services 6.0

-



This article was previously published under Q310221



INTRODUCTION
This article describes how to use the Microsoft Collaboration Data Objects (CDO) for Windows 2000 library (Cdosys.dll) to embed a message in a new message. You can do this by using Microsoft Visual C#.

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



MORE INFORMATION
To use CDOSYS to embed a message in a new message, follow these steps:  Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, click New, and then click Project. Click Console Application in the Visual C# Projects types list.

By default, Class1.cs is created.

Note In Microsoft Visual C# 2005, you must click Console Application in the Visual C# list. 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. In the Add References dialog box, click OK to accept your selections.

If you receive a message to generate wrappers for the libraries that you select, 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.IBodyPart iBp; CDO.IMessage iEmbdded; CDO.Message oMsg; CDO.IDataSource iDsrc;

// Create a new message. oMsg = new CDO.Message; oMsg.From = &quot;rhaddock@northwindtraders.com&quot;; oMsg.To = &quot;rhaddock@northwindtraders.com&quot;; oMsg.Subject = &quot;CDOSYS Send mail&quot;; oMsg.TextBody = &quot;Hello&quot;; iBp = oMsg.BodyPart.AddBodyPart(-1);

// Embed the message. iMsgs = iDropDir.GetMessages(&quot;C:\\Inetpub\\mailroot\\Drop&quot;); Console.WriteLine(&quot;Messages Count : &quot; + iMsgs.Count.ToString);

// Get first message. iEmbdded = iMsgs[1];

// Embed the message in oMsg by using BodyPart. iDsrc = iEmbdded.DataSource; iDsrc.SaveToObject(iBp, &quot;IBodyPart&quot;);

// Send mail. oMsg.Send;

iEmbdded = null; oMsg = null; iBp = null; iMsgs = null; iDropDir = null; }           catch (Exception e)            { Console.WriteLine(&quot;{0} Exception caught.&quot;, e); }           return; }        } } </li> Press F5 to build and to run the program.</li></ol>

<div class="references_section">