




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、在在.NET.NET 应用程序中用应用程序中用 System.Web.MailSystem.Web.Mail 发送电子邮发送电子邮件件本文关键字:本文关键字:作者:Mark Strawmyer日期:February 9, 2004欢迎来到 .NET Nuts & Bolts 栏目。在这个栏目中,我们将探讨怎样在应用中发送电子邮件。这将用到 System.Web.Mail 名字空间中的类。协作数据对象Windows 2000 协作数据对象 (Cdos/ target=_blankclass=infotextkeyDOSYS) 是微软用来创建和发送基于标准的电子邮件信息的消息组件。它是 用
2、与 Windows NT 的协作数据对象(CDONTS) 的替代物。 尽管由于向后兼容的原因 CDONTS 已包含在 Windows 2000 中, 但是 Windows XP,Windows Server 2003 以及更高版本均未包含或支持 CDONTS 组件。 所以任何使用 CDONTS 发送消息的应用程序都必须迁移到使用 Cdos/ target=_blankclass=infotextkeyDOSYS 上来。它提供了相同的功能,而且易于使用。除了作为替代物外, Cdos/ target=_blank class=infotextkeyDOSYS 还引入了一些 CDONTS 中没有的功
3、能,如:向新闻组发送消息的能力对消息的 MIME 体结构的控制接收和转发机制传输事件接受池以便对事件作出响应System.Web.Mail 命名空间包含了与 Cdos/ target=_blankclass=infotextkeyDOSYS 组件交互从而创建和发送信息的类。使用互联网信息服务(IIS)和 SMTP 服务为了能从应用程序中利用 Cdos/ target=_blank class=infotextkeyDOSYS 发送电子邮件,您必须确认 IIS 服务列表中已经安装了 SMTP 服务。在 Windows2000/XP 中,您可以通过控制面板 - 添加/删除程序 - 添加/删除 Wi
4、ndows组件选项来设置。STMP 服务的任务就是基于配置接收和发送消息。这个服务可以直接投递消息,也可以使用代理服务器来发送消息。当代理服务器已配置时,所有的消息将转发给它以备发送。你必须确保 IIS 和 SMTP 服务正确的安装和配置好。在投递之前,SMTP 服务使用一个目录结构来保存消息。默认的目录为C:Inetpubmailroot。这个文件夹中包含了一些子目录,如:Queue, Drop,Badmail。 如果你无法配置 SMTP 服务实例以便发送的话,您将可以在目录C:InetpubmailrootQueue 中的 *.EML 文件中找到邮件。这个技巧在离线创建邮件时将很有用。发送
5、消息正如前面提到的,发送电子邮件将是一件相对简单的事。类System.Web.Mail.MailMessage class 代表了将要发送的消息。E-mail 消息将由该类的实例来创建。这个类包含了诸如:收件人,发件人和主题等属性来让你控制想要发送的消息。还可以使用类 System.Web.Mail.MailAttachment 的实例创建附件,然后添加到 MailMessage 的 Attachments (附件)集合中。随后该消息将由 类 System.Web.Mail.SmtpMail 发送出去。发送邮件示例代码下面的 C# 示例代码将包含一个演示如何发送简单电子邮件的 Windows
6、控制台程序。当没有设置 SmtpMail 的 SmtpServer 属性时,本地机器将为其默认配置。你必须确保添加了针对 System.Web.dll 的引用,因为它是控制台应用程序而不是 ASP.NET 应用。using System;using System.Web.Mail;namespace CodeGuru.SendMail/ / Test console application to demonstrate sending e-mail./ class TestMail/ / The main entry point for the application./ static voi
7、d Main(string args)TestMail.Send(,Test Message Using Cdos/target=_blank class=infotextkeyDOSYS,Hello World!This is asimple message sentusing Cdos/ target=_blankclass=infotextkeyDOSYS.);/ / Send a message using the .NET wrapper for CollaborativeData/ Objects (CDO).This method shoul1234567891012345678
8、910 d be used when sending to a/ single recipientMessage originator/ Message receipent/ Message subject/ Message bodypublic static void Send(string MessageFrom,stringMessageTo,stringMessageSubject,stringMessageBody)MailMessage message = new MailMessage();message.From= MessageFrom;message.To= Message
9、To;本文关键字:本文关键字:message.Subject= MessageSubject;message.BodyFormat= MailFormat.Text;message.Body= MessageBody;trySystem.Console.WriteLine(Sending outgoing message);SmtpMail.Send(message);catch( System.Web.HttpException exHttp )System.Console.WriteLine(Exception occurred: +exHttp.Message);发送带有附件的邮件示例代
10、码下面的 C# 示例代码将包含一个演示如何发送带有附件的电子邮件的 Windows控制台程序。这将由创建类 MessageAttachment 的实例,然后将其添加到Attachments 集合来完成。using System;using System.Web.Mail;namespace CodeGuru.SendMail/ / console application to demonstrate sending e-mail with an/ attachment./ class TestMail/ 1234567891012345678910/ The main entry point
11、for the application./ static void Main(string args)TestMail.SendAttachment(,TestMessage Using Cdos/ target=_blank class=infotextkeyDOSYS,HelloWorld!This is a simplemessagesent using Cdos/ target=_blank class=infotextkeyDOSYS.,c:myattachment.txt);/ / Send a message using the .NET wrapper for Collabor
12、ativeData/ Objects (CDO).This method should be used when sendingto/ a single recipientMessage originator/ Message receipent/ Message subject/ Message body/ Path to attachment/ public本文关键字:本文关键字:message.To= MessageTo;message.Subject= MessageSubject;message.BodyFormat= MailFormat.Text;message.Body= Me
13、ssageBody;/ Create and add the attachmentMailAttachment attachment = newMailAttachment(MessageAttachmentPath);message.Attachments.Add(attachment);try/ Deliver the messageSystem.Console.WriteLine(Sending outgoing message);SmtpMail.Send(message);catch( System.Web.HttpException exHttp )System.Console.W
14、riteLine(Exception occurred: +exHttp.Message);可能的改进我们已经从不同途径演示了如何发送电子邮件。现在轮到你想想如何在你的应用程序中应用这项功能了。这里有一些想法和你共同分享:E-mail 警告当一个致命的或无法恢复的应用程序错误发生时,您的应用程序可以发送电子邮件到一指定地址以使其能很快得知。创建一个基于 Web 的联系消息窗体你可以使用户通过填写 Web 表单来发送用户反馈,然后编写相关程序把消息发送给适当的联系人。订阅服务当通过 Cdos/ target=_blank class=infotextkeyDOSYS 组件发送电子邮件来支持订阅类
15、型的服务时,您将需要发送多封邮件而不是单一邮件给所有的接收者。当一则消息拥有大量的接收者时,处理所有的接收者将戏剧性地减满处理速度。这时候您最好将接收者列表分解成多个列表,分好几次发送消息。使用 Bcc 发送消息当通过 Cdos/ target=_blank class=infotextkeyDOSYS组件发送电子邮件来支持订阅类型的服务时,您也许想要使用 Bcc 而不是 To来填写接收人地址。这样可以使得所有接收者无法得知接收者列表。发送 HTML 格式的邮件消息主体格式可以是 HTML。它可以使消息主体以HTML 格式发送而不是普通文本。-原文:Sending E-Mail with Sy
16、stem.Web.MailMark Strawmyer (view profile)February 9, 2004Rating: not yet rated-1234567891012345678910 -Welcome to the next installment of the .NET Nuts & Bolts column. Inthis column, well explore sending e-mail from within applications.This will involve utilizing classes contained in the System
17、.Web.Mailnamespace.Collaboration Data ObjectsCollaboration Data Objects for Windows 2000 (Cdos/ target=_blankclass=infotextkeyDOSYS) is a Microsoft messaging component thatallows for standards-based e-mail messages to be constructed and sent.It is a replacement of the Collaboration Data Objects for
18、NTS(CDONTS), which, as you can probably guess by the name, was forWindows NT. CDONTS is included with Windows 2000 for backwardscompatibility, but Windows XP, Windows Server 2003, and beyond do notinclude or support the use of CDONTS. Thus, any applications thatsend messages using CDONTS must be mig
19、rated to本文关键字:本文关键字:1234567891012345678910 ;string MessageSubject,stringMessageBody)MailMessage message = new MailMessage();message.From= MessageFrom;message.To= MessageTo;message.Subject= MessageSubject;message.BodyFormat= MailFormat.Text;message.Body= MessageBody;trySystem.Console.WriteLine(Sendin
20、g outgoing message);SmtpMail.Send(message);catch( System.Web.HttpException exHttp )System.Console.WriteLine(Exception occurred: +exHttp.Message);Sending a Message with an Attachment Sample CodeThe following sample code contains a C#-based Windows Consoleapplication that shows how to send an e-mail m
21、essage that includes anattachment. This is done by creating instances of theMessageAttachment class and then adding them to the message throughthe Attachments collection.using System;using System.Web.Mail;namespace CodeGuru.SendMail/ / console application to demonstrate sending e-mail with an/ attac
22、hment./ class TestMail/ / The main entry point for the application./ static void Main(string args)TestMail.SendAttachment(,&nbs1234567891012345678910 p;Test Message Using Cdos/target=_blank class=infotextkeyDOSYS,HelloWorld!This is a simplemessagesent using Cdos/ target=_blank class=infotextkeyD
23、OSYS.,&本文关键字:本文关键字:string MessageBody,string MessageAttachmentPath)/ Create and setup the messageMailMessage message = new MailMessage();message.From= MessageFrom;message.T1234567891012345678910 o= MessageTo;message.Subject= MessageSubject;message.BodyFormat= MailFormat.Text;message.Body= Messag
24、eBody;/ Create and add the attachmentMailAttachment attachment = newMailAttachment(MessageAttachmentPath);message.Attachments.Add(attachment);try/ Deliver the messageSystem.Console.WriteLine(Sending outgoing message);SmtpMail.Send(message);catch( System.Web.HttpException exHttp )System.Console.Write
25、Line(Exception occurred: +exHttp.Message);Possible EnhancementsWe have demonstrated how to send e-mail messages in a couple of ways.It is now up to you to think about ways in which you can utilize thisfunctionality within your applications. Here are some ideas toconsider on your own:E-mail alertswhen a fatal or unrecoverable application error occurs,your application could e-mail information to a designated location sothat it is immediately known.Build a We
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 小学教师反思对未来教育的展望与影响试题及答案
- 宜昌文员面试题及答案
- 2025年新能源汽车的市场营销策略试题及答案
- 工程测量面试题及答案
- 德州社工面试题及答案
- 招考小学教师试题及答案
- 尔雅情绪测试题及答案
- 2024年金湖县事业单位招聘真题
- 农产品电商生态系统分析试题及答案
- 绣湖初一数学试卷及答案
- 中期妊娠引产的护理
- 《摄影基础知识讲座》课件
- 东华全民健康信息平台建设方案
- 10t桥式起重机安装方案
- 【MOOC】倾听-音乐的形式与审美-武汉大学 中国大学慕课MOOC答案
- 第五讲铸牢中华民族共同体意识-2024年形势与政策
- 1例脑出血术后并颅内感染患者的个案护理
- 2024年重庆市普通高中学业水平选择性考试高考模拟调研卷(一)化学试题(含答案解析)
- 《发酵工程原理及技术》期末试题C及答案
- 中国书法史学习通超星期末考试答案章节答案2024年
- 企业社会责任与顾客满意
评论
0/150
提交评论