



全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
ASP.NET中发送Email完整实例本文举例说明在ASP.NET中发送Email的众多可能性,内容覆盖了诸如Email格式、优先权、附件及Email编码等方面。 ASP.NET被赋予了一个发送Email的新对象,名为SmtpMail。使用SmtpMail对象从ASP.NET页面中发送Email时,可以遵循以下简单步骤: 包含与邮件有关类所需要的名称空间; 例示一个信息对象,设置属性; 使用SmtpMail对象实例的send方法发送邮件。 现在我们就来一步一步地研究从一个ASP.NET页面发送Email的过程。我们使用了VB来说明这个例子,最后将包含VB和C#的完整代码。 第一步:包含名称空间 在ASP.NET 页面中引入System.Web.Util 名称空间,这个名称空间中包括了发送一个email所必须的所有对象。这些对象是: SmtpMail:代表邮件系统,用于发送email。 MailMessage:代表一个信息,其属性包括发件人地址、收件人地址等。 MailFormat:代表信息的格式:HTML、文本等。 MailAttachment:代表一个email附件。 MailEncoding enum:代表Base64 或Uuencode的任何编码。取值范围:Base64、UUencode MailPriority enum:用来为信息设置优先权。值为:高、低、一般。 第二步:例示 MailMessage 对象 使用以下语句来例示MailMessage对象: Dim mailObj AS new MailMessage 用MailMessage对象的属性来准备邮件。MailMessage对象有下列属性: From:发件人的Email地址 To:收件人的Email地址 Subject:email的主题 Body:email的主体 CC:email抄送的收件人列表 BCC:email暗送的收件人列表 Priority:信息的优先权:高、低或一般 BodyEncoding:信息体的编码,如果有的话,就是Base64或UUencode BodyFormat:信息的格式:Html 或text Attachments:附加到email 的MailAttachment对象列表,主要就是对这个对象集合的一个引用 下面这段代码示范了使用MailMessage 对象属性的方法,它们代表了将在本例中创建的一个信息,这个信息要用SmtpMail对象来发送。在例子中,mailObj引用了信息对象的例示: mailObj.From = mailObj.To = Request.Form (to) mailObj.Subject = subject of the mail mailObj.Body = Message of the mail 第三步:发送Email 这时,我们就可以使用SmtpMail 对象的Send方法来发送邮件了: SmtpMail.Send(mailObj) 完整实例 最后,我们把以上解释的属性结合在一个完整的例子中。为了说明用ASP.NET 发送一个email 的全部可能性,我们还包含了一些“小技巧”。下面是使用VB.NET的完整例子: This method is called on the server when the submit button is clicked on the client and when the page posts back to itself Sub SendMail (Obj As Object, E As EventArgs) Instantiate a MailMessage object. This serves as a message object on which we can set properties. Dim mailObj AS new MailMessage Set the from and to address on the email mailObj.From = Request.Form(From) mailObj.To = Request.Form(To) mailObj.Subject = Subject Of the Mail mailObj.Body = Body of the Mail Optional: HTML format for the email mailObj.BodyFormat = MailFormat.Html Optional: Encoding for the message mailObj.BodyEncoding = MailFormat.Base64 Optional: Set the priority of the message to high mailObj.Priority = MailPriority.High Optional: Attach a file to the email. Note here that we have created a MailAttachment object to attach a file to the email mailObj.Attachments.Add(new MailAttachment(c:test.doc) Send the email using the SmtpMail object SmtpMail.Send(mailObj) End Sub Email Recipient: Email Sender: 在以上例子中,From(发件人)和 To(收件人)的Email地址是从相应的文本框中收集的,点击“Send Mail”(发送邮件)按钮时,邮件就被发送出去。当“Send Mail”(发送邮件)按钮被点击时,表单回递到它自己,在服务器上“SendMail”(发送邮件)程序被触发,邮件被发送。下面是使用C#的例子: / This method is called on the server when the submit / button is clicked on the client and when the page / posts back to itself public void SendMail (Object Obj, EventArgs E) / Instantiate a MailMessage object. This serves as a message object / on which we can set properties. MailMessage mailObj = new MailMessage(); / Set the from and to address on the email mailObj.From = Request.Form(From); mailObj.To = Request.Form(To); mailObj.Subject = Subject Of the Mail; mailObj.Body = Body of the Mail; / Optional: HTML format for the email mailObj.BodyFormat = MailFormat.Html; / Optional: Encoding for the message mailObj.BodyEncoding = MailFormat.Base64; / Optional: Set the priority of the message to high mailObj.Priority = MailPriority.High; / Optional: Attach a file to the email. / Note here that we have created a MailAttachment object to / attac
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 建筑方案设计评审意见医院
- 股权激励方案咨询维权
- 咨询婚介方案
- 三山区营销解决方案
- 河流护坡护底施工方案
- 产品咨询策划方案模板
- 建筑产品运营方案设计模板
- 企业咨询薪酬管理方案
- 股权设计方案咨询
- 连锁餐饮店营销管理方案
- 软件升级与迭代更新协议说明
- (高清版)DB34∕T 5225-2025 风景名胜区拟建项目对景观及生态影响评价技术规范
- (2025)学宪法讲宪法知识竞赛试题库及参考答案
- 乡村医生法律法规知识
- 净菜加工培训
- 2025年福建省中考英语试卷真题(含标准答案)
- 骨科VTE管理制度
- 医院运送现场管理制度
- GB/T 45653-2025新能源汽车售后服务规范
- 《公路钢渣沥青路面施工技术指南(征求意见稿)》编制说明
- 因学生先天性心脏病在校免责协议书8篇
评论
0/150
提交评论