Microsoft KB Archive/310212

= How to use the Cdosys.dll library to send an e-mail message with attachments by using Visual C# =

Article ID: 310212

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 Q310212



SUMMARY
This article describes how to use the Collaboration Data Objects (CDO) for Windows 2000 library (Cdosys.dll) to send an e-mail message with attachments. You can send text or HTML or a Web page in the body of the e-mail message by using the local SMTP server or by using a smart host server 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, Visual C# Projects is changed to Visual C#. 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 Visual C# 2005, you do not need to click Select. To accept your selections, click OK in the Add References dialog box.

If you receive a dialog box 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.Message oMsg = new CDO.Message; CDO.IConfiguration iConfg;

iConfg = oMsg.Configuration;

ADODB.Fields oFields; oFields = iConfg.Fields;

// Set configuration. ADODB.Field oField = oFields[&quot;http://schemas.microsoft.com/cdo/configuration/sendusing&quot;]; //TODO: To send by using the smart host, uncomment the following lines: //oField.Value = CDO.CdoSendUsing.cdoSendUsingPort; //oField = oFields[&quot;http://schemas.microsoft.com/cdo/configuration/smtpserver&quot;]; //oField.Value = &quot;smarthost&quot;;

// TODO: To send by using local SMTP service. //oField = oFields[&quot;http://schemas.microsoft.com/cdo/configuration/sendusing&quot;]; //oField.Value = 1;

oFields.Update;

// Set common properties from message.

//TODO: To send text body, uncomment the following line: //oMsg.TextBody = &quot;Hello, how are you doing?&quot;;

//TODO: To send HTML body, uncomment the following lines: //String sHtml; //sHtml = &quot;<HTML>\n&quot; + // &quot;<HEAD>\n&quot; + // &quot;<TITLE>Sample GIF</TITLE>\n&quot; + // &quot;</HEAD>\n&quot; + // &quot;<BODY><P>\n&quot; + // &quot; <Font Color=Green>Inline graphics</Font> \n&quot; + // &quot;</BODY>\n&quot; + // &quot;</HTML>&quot;; //oMsg.HTMLBody = sHtml;

//TOTO: To send WEb page in an e-mail, uncomment the following lines and make changes in TODO section. //TODO: Replace with your preferred Web page //oMsg.CreateMHTMLBody(&quot;http://www.microsoft.com&quot;,               //  CDO.CdoMHTMLFlags.cdoSuppressNone,                 //  &quot;&quot;, &quot;&quot;); oMsg.Subject = &quot;Test SMTP&quot;;

//TODO: Change the To and From address to reflect your information. oMsg.From = &quot;someone@example.com&quot;; oMsg.To = &quot;someone@example.com&quot;; //ADD attachment. //TODO: Change the path to the file that you want to attach. oMsg.AddAttachment(&quot;C:\\Hello.txt&quot;, &quot;&quot;, &quot;&quot;); oMsg.AddAttachment(&quot;C:\\Test.doc&quot;, &quot;&quot;, &quot;&quot;); oMsg.Send; }           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> Verify that the e-mail message has been both sent and received.</li></ol>

<div class="references_section">