利用win2k系统的cdosys.dll库发送带附件的邮件之办法

原创 2004年09月03日 22:23:00
 

How to use the Cdosys.dll library to send an e-mail with attachments

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# .NET.

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 .NET.
  2. On the File menu, click New, and then click Project.
  3. Under Project Types, click Visual C# Projects, and then click Console Application under Templates. 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, and then 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/library/en-us/dnoffdev/html/vsofficedev.asp

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

The information in this article applies to:

  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)
  • Microsoft Collaboration Data Objects for Windows 2000
  • ActiveX Data Objects (ADO) 2.5
  • ActiveX Data Objects (ADO) 2.6
  • ActiveX Data Objects (ADO) 2.7
  • Microsoft Internet Information Services version 6.0
  • Microsoft 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

通过common-emai类库发送普通邮件和带有附件的邮件的完整示例

1.此示例是通过maven构建,pom.xml文件如下:
  • sidongxue2
  • sidongxue2
  • 2015年01月09日 13:30
  • 1580

Python模块探秘 Smtplib发送带有各种附件的邮件

这两天对Python的邮件模块比较感兴趣,于是就查了查资料。同时在实际的编码过程中也遇到了各种各样的问题。下面我就来分享一下我与smtplib的故事。前提条件我的上一篇博文里面讲解了,发送邮件必须的条...
  • Marksinoberg
  • Marksinoberg
  • 2016年05月26日 12:56
  • 19392

利用spring mail 发送带附件的邮件

http://pan.baidu.com/s/1mhC86NI
  • zy_281870667
  • zy_281870667
  • 2016年05月26日 15:48
  • 2445

【JavaMail开发】使javamail-1.4.2 发送邮件时中文附件乱码或显示"??"的解决办法

使用mail-1.4.1.jar发送邮件时中文附件乱码或显示"??"的解决办法.
  • tiny__wang
  • tiny__wang
  • 2016年08月18日 21:30
  • 211

利用smtp协议发送带附件的邮件

之前写过一个发邮件的,不过没带附档,今天再看了下smtp协议,做了个带附档的邮件发送例子,也就这样吧。...
  • xcltapestry
  • xcltapestry
  • 2016年05月07日 21:57
  • 3182

使用System.Net.Mail发送邮件时,附件如果是中文名的处理办法

有个开发需求是,需要在发送邮件时同时发送
  • cjsyr_cjsyr
  • cjsyr_cjsyr
  • 2014年11月10日 11:21
  • 2807

利用spring mail 发送带附件的邮件

http://pan.baidu.com/s/1mhC86NI
  • zy_281870667
  • zy_281870667
  • 2016年05月26日 15:48
  • 2445

php 使用pear库发送带附件邮件(来自Stack overflow)

  • magic_zj00
  • magic_zj00
  • 2012年02月14日 16:02
  • 1011

JavaMail邮件发送-能发送附件和带背景音乐的邮件的小系统

这里使用的是JavaMail技术,前台使用了fckeditor做邮件美化,由于只是示例,后台发送时只是将邮件保存在本地,但是可以查看,如果需要实际发送,请参考我的其他博客文章,我写了很多关于邮件发送的...
  • cuisuqiang
  • cuisuqiang
  • 2014年11月13日 16:13
  • 536

JavaMail邮件发送-能发送附件和带背景音乐的邮件的小系统

这里使用的是JavaMail技术,前台使用了fckeditor做邮件美化,由于只是示例,后台发送时只是将邮件保存在本地,但是可以查看,如果需要实际发送,请参考我的其他博客文章,我写了很多关于邮件发送的...
  • xiyang_1990
  • xiyang_1990
  • 2013年09月09日 17:42
  • 1614
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:利用win2k系统的cdosys.dll库发送带附件的邮件之办法
举报原因:
原因补充:

(最多只允许输入30个字)