How to use the cdosys.dll Library to send an e-mail with attachmentsview products That this article applies.
THIS ARTICLE WAS Previously Published Under Q310212
SUMMARYThis 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 # .NET.
Note the cdosys.dll library is also knower as cdosys.
More inforationto use cdosys as described in the "summary" section, Follow these Steps:
Start Microsoft Visual Studio .NET. 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.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. 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.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.field;
// SET Configuration.
Adodb.field Ofield = Ofields ["http://schemas.microsoft.com/cdo/configuration/sendusing];
// Todo: to send by using the smart host, uncomment the stocking lines: //ofield.value = cdo.cdosendusing.cdosendusingport;
// of deield = Ofields ["http://schemas.microsoft.com/cdpo/configuration/smtpserver];
//ofield.value = "smarthost";
// Todo: TO Send By Using Local SMTP Service.
// of deield = Ofields ["http://schemas.microsoft.com/cdo/configuration/sendusing"];
//ofield.value = 1;
Ofields.Update ();
// SET Common Properties from Message.
// Todo: To send text body, uncomment the folload line:
//omsg.textbody = "Hello, How are you doing?";
// Todo: TO Send HTML Body, Uncomment The Following Lines:
// String Shtml;
// shtml = " / n"
// "
/ n"// "
// " hEAD> / N"
// "
/ n"
// "
// " body> / n"
// " html>";
//omsg.htmlbody = html;
// 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("http://www.microsoft.com ",
// cdo.cdomhtmlflags.cdosuppressnone,
// "", "");
OMSG.SUBJECT = "Test SMTP";
// Todo: Change the to and from address to reflect your information.
OMSG.FROM = "someone@example.com";
omsg.to = "someone@example.com";
// Add attachment.
// Todo: Change the path to the file. You Want to attach.
OMSG.Addattachment ("c: //hello.txt", "", "");
OMSG.Addattachment ("C: //Test.doc", "", ""); omsg.send ();
}
Catch (Exception E)
{
Console.writeline ("{0} exception caught.", E);
}
Return;
}
}
} Where Todo APPEARS INDICATED.TO BUILD AND RUN The Program, Press F5.Verify That The e-mail message has been Both Sent and Rece some
Referencesfor More Information About Microsoft Office Developments With Visual Studio, See The Following Microsoft Developer Network (MSDN) Web Site:
http://msdn.microsoft.com/library/en-us/dnoffdev/html/vsofficedev.aspFor additional information about how to use CDOSYS, click the following article numbers to view the articles in the Microsoft Knowledge Base:
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
310225 How to use the cdosys.dll library to save a message to a file by using visual c # .net
The Information in this Article Applies TO:
Microsoft Visual C # .NET (2003) Microsoft Visual C # .NET (2002) Microsoft Collaboration Data Objects for Windows 2000ActiveX Data Objects (ADO) 2.5ActiveX Data Objects (ADO) 2.6ActiveX Data Objects (ADO) 2.7Microsoft Internet Information Services version 6.0Microsoft Internet Information SERVICES 5.0
Last Reviewed: 7/26/2004 (1.0) Keywords: KBCode Kbhowto KB310212 KBAUDDEVELOPER
Microsoft LEGAL LINKS TERMS of Use Security & Privacy Accessibility