Microsoft Knowledge Base Email Alertz

(310212) - Describes how to use CDO for Windows 2000 library to send an e-mail with attachments. You can send text body or html body or a Web page in e-mail by using C# .NET.

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

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

Article ID: 310212 - View products that this article applies to.
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 "Summary" section, follow these steps:
  1. Start Microsoft Visual Studio.
  2. On the File menu, click New, and then click Project.
  3. Under Project Types, click Visual C#, and then click Console Application under Templates. By default, Program.cs is created.

    Note In Microsoft Visual C# .NET 2003, Visual C# is changed to Visual C# Projects. By default, Class1.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.

      Note In Visual C# .NET 2003, click Select.
    3. 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.
  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.Message oMsg = new CDO.Message();
    				CDO.IConfiguration iConfg; 
    
    				iConfg = oMsg.Configuration;
    
    				ADODB.Fields oFields;
    				oFields = iConfg.Fields;       
    
    				// Set configuration.
    				ADODB.Field oField = oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"];
                            
    				//TODO: To send by using the smart host, uncomment the following lines:
    				//oField.Value = CDO.CdoSendUsing.cdoSendUsingPort;
    				//oField = oFields["http://schemas.microsoft.com/cdo/configuration/smtpserver"];
    				//oField.Value = "smarthost";
    
    				// TODO: To send by using local SMTP service. 
    				//oField = 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 following line: 
    				//oMsg.TextBody = "Hello, how are you doing?";
    			
    
    				//TODO: To send HTML body, uncomment the following lines:
    				//String sHtml;
    				//sHtml = "<HTML>\n" + 
    				//	"<HEAD>\n" +
    				//	"<TITLE>Sample GIF</TITLE>\n" +
    				//	"</HEAD>\n" +
    				//	"<BODY><P>\n" + 
    				//	"<h1><Font Color=Green>Inline graphics</Font></h1>\n" +
    				//	"</BODY>\n" + 
    				//	"</HTML>";
    				//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("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 that 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;
    		}
    	}
    }
  6. Where TODO appears in the code, modify the code as indicated.
  7. To build and run the program, press F5.
  8. Verify that the e-mail message has been both sent and received.

REFERENCES

For more information about Microsoft Office development with Visual Studio, see the following Microsoft Developer Network (MSDN) Web site:
http://msdn.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:
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

Properties

Article ID: 310212 - Last Review: July 30, 2008 - Revision: 4.0
APPLIES TO
  • Microsoft Visual C# 2008 Express Edition
  • Microsoft Visual C# 2005
  • 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: 
kbsweptvs2008 kbcode kbhowto KB310212
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Tom Kretzmer - tkretzmer NOSPAM-AT-NOSPAM hotmail.com Report As Irrelevant  
Written: 11/15/2004 12:30 PM
Thanks for making this more searchable. This was the crucial portion of the send mail using cdosys.dll article that I needed (link below) With this methodology you can have any Windows 2000 Workstation or above building automated messages, bouncing to a central SMTP server. This is invaluable for monitoring / alerting. (Valuable portion here is the .AddAttachment method) What I can't understand is why it's so hard to find... http://support.microsoft.com/default.aspx?scid=kb;en-us;Q286431 Enjoy

Barbu Lucian - parvu2004 NOSPAM-AT-NOSPAM yahoo.com Report As Irrelevant  
Written: 2/13/2008 11:04 AM
What is a smart server? Is an external from microsoft server that anyone can use to send emails ? Thanks for the answear