如何使用 Cdosys.dll 库发送带有附件的电子邮件
原始 KB 编号: 310212
摘要
本文介绍如何使用适用于 Windows 2000 库的协作数据对象 (CDO) (Cdosys.dll) 发送带有附件的电子邮件。 可以使用本地 SMTP 服务器或使用 Microsoft Visual C# 中的智能主机服务器在电子邮件正文中发送文本、HTML 或网页。
注意
Cdosys.dll 库也称为 CDOSYS。
更多信息
若要使用“摘要”部分中所述的 CDOSYS,请执行以下步骤:
启动 Microsoft Visual Studio。
在“ 文件 ”菜单上,选择“ 新建”,然后选择“ 项目”。
在“项目类型”下,选择“Visual C#”,然后选择“模板”下的“控制台应用程序”。 默认情况下,会创建Program.cs。
注意
在 Microsoft Visual C# .NET 2003 中, Visual C# 更改为 Visual C# 项目。 默认情况下,将创建Class1.cs。
添加对 Microsoft CDO For Windows 2000 库的引用。 为此,请按照下列步骤操作:
在“ 项目 ”菜单上,选择“ 添加引用”。
在“ COM ”选项卡上,找到 Microsoft CDO For Windows 2000 库。
注意
在 Visual C# .NET 2003 中,选择“ 选择”。
若要接受选择,请在“添加引用”对话框中选择“确定”。
如果收到一个对话框,用于为所选库生成包装器,请选择“ 是”。
在代码窗口中,将所有代码替换为以下代码:
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; } } }
在代码中出现 TODO 的位置,按指示修改代码。
若要生成并运行程序,请按 F5。
验证是否已发送和接收电子邮件。
References
有关使用 Visual Studio 进行 Microsoft Office 开发的详细信息,请参阅 使用 Visual Studio 进行 Microsoft Office 开发。
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈