| Introduction | |
|
[
Back To Top ]
In Asp.Net 1.x sending email was fairly simple. The only issue was that there were different ways to send email depending on whether the application was web or windows based. Windows developers used MAPI to send emails using an email client like Outlook. Web developers used CDOSYS to send rich emails. Microsoft realized that a change was warranted. In Asp.Net 2.0 Microsoft released a new set of classes to handle email. The only issue is that to many developers, sending email became much more complex. Within this article, we will walk through the changes to the web.config file, the new classes that are available and how to send email using the new MailMessage class. | |
| Examining the Web.config | |
|
[
Back To Top ]
By now you should be familiar with the system.web section group that is found in the web.config file. In Asp.Net 2.0 a new group was added that contains configurable sections for the System.Net namespace called the system.net section group. This group allows email settings to be established by using the mailSettings section group. Since we are sending emails and not receiving emails, we will use the SMTP protocol. To define the SMTP settings, we will add the SMTP section group to the mailSettings section group. The SMTP section group then contains a network section that specifies attributes for the SMTP network settings. These attributes include host, port, username, and password. An example of the system.net section group can be found in listing 1. Listing 1 <system.net>
<mailSettings>
<smtp>
<network host="mail.contoso.com"
userName="email@contoso.com" password="password" />
</smtp>
</mailSettings>
</system.net>
| |
| Accessing the Web.config from Code | |
|
[
Back To Top ]
To prevent confusion, in Asp.Net 2.0 the System.Web.Mail namespace has been marked deprecated. All of the classes are still accessible using IntelliSense and will still function properly. However, they too are marked obsolete. Instead, a new namespace found at System.Net.Mail should be used. This new namespace contains classes to manage your mail client and messages. The SmtpClient class is used to establish a host, port, and network credentials for your SMTP server. The MailMessage class is similar to the MailMessage class found in the old System.Web.Mail namespace. This class allows a full email message to be built. There is also an Attachment class that allows an attachment to be generated so it can later be added to the MailMessage object. In addition to these three most commonly used classes, you will find that System.Net.Mail contains an AlternateView and LinkedResource class. Unfortunately, Asp.Net 2.0 does not provide a clean and easy way to access the system.net section group from the web.config file within code. The SmtpClient object contains a UseDefaultCredentials boolean property, but that specifies whether the SmtpClient object is to use the credentials of the user currently running the application. Before we begin accessing the attributes in the web.config file, we will need to determine what credentials will be required to connect to the SMTP server and ensure that the values we will use will work. In many cases, the credentials that would be used to connect to a Microsoft Exchange server would be different than those to connect to many ISP email systems. In the example to follow we will use a username, password, and hostname. The first task on our plate is to read the mailSettings section group from the web.config file. To accomplish this task, we will need to create a new object of type System.Configuration.Configuration. This object will store our web.config file. We will assign this new object to the applications web.config by using the OpenWebConfiguration method of the WebConfigurationManager class. The OpenWebConfiguration method will look for a relative path. We will pass in the HttpContext.Current.Request.ApplicationPath since we will want to load the web.config file for the current application. The second task is to load the specific section group from the object that contains our web.config file into a new object. A new object of type System.Net.Configuration.MailSettingsSectionGroup will be created. This object will be assigned to the mailSettings section in the web.config file. We will use the GetSectionGroup method of our configuration object to obtain this section. ASP.NET does not know that the mailSettings section is found under system.net so we must specify the full section group as system.net/mailSettings. An example of this can be seen in listing 2. Listing 2 Dim config As System.Configuration.Configuration = _
WebConfigurationManager.OpenWebConfiguration( _
HttpContext.Current.Request.ApplicationPath)
Dim settings As System.Net.Configuration.MailSettingsSectionGroup = _
CType(config.GetSectionGroup("system.net/mailSettings"), _
System.Net.Configuration.MailSettingsSectionGroup)
| |
| Creating the SMTP Client | |
|
[
Back To Top ]
A new object of type SmtpClient will be created to act as the SMTP server for mail being sent out. The SmtpClient object will need to have the network credentials passed into it. Thus, we will need to create a new object of type System.Net.NetworkCredential and pass in the username and password values from the web.config. These values can be found by assessing the Smtp.Network.Username and Smtp.Network.Password properties of the settings object defined earlier. The NetworkCredential object will be assigned to the Credentials property of the SmtpClient object. The other property we must set is the SmtpClient object's host property. This too will be assigned a value from the settings object created earlier. An example of this can be seen in the listing given below. Listing 3 'Obtain the Network Credentials from the mailSettings section
Dim credential As New System.Net.NetworkCredential( _
settings.Smtp.Network.UserName, settings.Smtp.Network.Password)
'Create the SMTP Client
Dim client As New SmtpClient()
client.Host = settings.Smtp.Network.Host
client.Credentials = credential
| |
| Creating a MailMessage Object | |
|
[
Back To Top ]
The MailMessage object in Asp.Net 2.0 is much more robust than the one found in the CDOSYS libraries in Asp.Net 1.x. The new MailMessage object still contains all of the common properties that an email message has including To, From, Subject, and Body. The To and From properties have changed a bit. The From property is no longer "just a string." Instead, the From property is of type MailAddress. The To property is similar in that it is a collection of MailAddress objects. Since the CC and Bcc properties have the same requirements as the To property, they too are a collection of MailAddress objects. The MailMessage object contains some additional properties including, but not limited to, AlternateViews, Attachments, BodyEncoding, DeliveryNotificationOptions, IsBodyHtml, Priority, and ReplyTo. The AlternateViews property allows an alternate view to be specified by passing in either a filename or a stream and assigning a MIME type to the view. The Attachments property allows attachments to be added to the message. The BodyEncoding property is a value that can specify the System.Text.Encoding type of the email message. The DeliveryNotificationOptions property specifies when delivery notifications should be received. The IsBodyHtml property is a boolean value signifying whether the email's body is HTML or plain text. The Priority property specifies whether this email is of high, low, or normal importance. The ReplyTo property is the email address that the reply message would go to. To send the MailMessage object, the object must be passed into the Send method of the SmtpClient object. An example of this can be seen in the listing below. Listing 4 'Build Email Message
Dim email As New MailMessage
email.From = New MailAddress("email@contoso.com")
email.To.Add("steveb@contoso.com")
email.CC.Add("billg@contoso.com")
email.Subject = "Test Email"
email.IsBodyHtml = True
email.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
email.Body = "<strong>Hello Steve!</strong>"
'Send Email
client.Send(email)
| |
| Conclusion | |
|
[
Back To Top ]
As you can see now, sending email in Asp.Net 2.0 is much more complex. However, it is also much more robust. Many of the email options that you could not access before without adding an email header to the email message can now be added with properties. Hopefully the team will continue to enhance this namespace and add additional functionality in the future. |
相关推荐
LangFlow开源神器:拖拽构建AI Agent与MCP协议集成实战
LangFlow 15万星开源神器:拖拽搭建 AI Agent,一键部署 API / MCP / JSON 在AI应用开发领域,传统编码方式往往需要处理复杂的API调用、数据处理和流程编排,这让很多开发者望而却步。LangFlow作为GitHub上获得15万星的开源项目,通过可视化拖拽的方式彻底改变了AI Agent的开发体验。本文将完整介绍如何使用LangFlow快速构建功能强大的AI Agent,并实现API、MCP协议和JSON配置的一键部署。 1. LangFlow与AI Agent核心概念解析 1
asp.net 发邮件[转]
asp.net 界面 <form id="form1" runat="server"> <div> <table align="center" border="0" cellpadding="0" cellspacing="0" width="776"> <tr> &...
【华为鸿蒙3.0/荣耀安卓12使用VMOS Pro的激活方式】
解压激活工具 →连接数据线 轻松搞定激活!
ASP.NET 2.0中改变passwordrecovery发邮件时的信息
在ASP.NET 2.0的登陆控件中(详细见我的文章,www.yesky.com/SoftChannel/72342380468043776/20041102/1870966.shtml),其中的passwordrecovery(恢复用户密码控件中,可以当用户输入密码回答问题,答案后,将用户的用户名和密码EMAIL给用户,其中,我们 可以改善一下它的提示密码信息)
别再折腾CUDA了!Win11上VSCode一键配置PyTorch GPU环境(附Anaconda虚拟环境避坑指南)
本文提供Win11系统下使用VSCode和Anaconda一键配置PyTorch GPU环境的详细指南,解决CUDA版本冲突等常见问题。通过conda的原子化依赖解决方案,安装时间从2小时缩短至15分钟,适合深度学习初学者快速搭建开发环境。
FAST-LIO2实战:如何在无人机上部署并实现厘米级定位(附ROS配置全流程)
本文详细介绍了如何在无人机上部署FAST-LIO2激光雷达惯性里程计系统,以实现厘米级精确定位。内容涵盖从硬件选型(如激光雷达与机载计算机)、软件环境配置(ROS Noetic)、传感器标定,到FAST-LIO2参数调优、与PX4飞控集成以及实际飞行测试的全流程。重点解决了实时定位中的外参标定、数据同步等关键问题,为无人机在无GPS或复杂光照环境下提供稳定可靠的定位解决方案。
C# ASP.net发邮件代码
C# ASP.net发邮件代码 System.Net.Mail.SmtpClient client = new SmtpClient("smtp.163.com"); client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential("cwy_speed@163.com
Nuvoton OpenTitan开源安全芯片技术解析与应用
硬件安全模块(HSM)作为可信计算基的核心组件,其设计透明度和可验证性正成为行业焦点。RISC-V架构凭借开源特性,为构建可审计的安全芯片提供了新范式。OpenTitan项目通过开放Verilog源码和形式化证明,实现了从物理不可克隆函数(PUF)到分层安全启动的全栈透明。该方案在lowRISC Voyager开发板上实测显示,安全启动时间小于200ms,性能损耗仅7%,显著优于传统黑盒方案。目前该技术已应用于Chromebook和数据中心,其40nm工艺版本更将功耗降至5μA待机水平,为物联网和边缘计算提
ASP.Net 2.0 发送邮件的代码
这两天因为要做一个可以收集用户提交信息,然后发送邮件到指定邮箱的网页。在 asp.net 2.0 里面发送邮件可以使用 System.Net.Mail 下面的类。http://www.systemnetmail.com/faq/2.1.aspx 这个地址有 System.Net.Mail 的作者写的仿佛是 MSDN 类似的 Faq,看代码的时候可以参考以下。什么都不说了,下面贴出来代码吧,毕竟代码
DEM高程数据的获取、合并与坐标转换
DEM高程数据的获取、合并与坐标转换一、DEM数据的获取二、数据合并三、投影转换方法一四、投影转换方法二 今天用DEM数据做了一个坡度分析,数据使用过程中涉及到投影坐标转换,由于不是很熟悉所以费了一番功夫。经过一番折腾总结出两个方法,方法一自认为比较便捷的方法,即通过BIGMAP软件中工具列表中的投影转换功能进行。方法二为传统方法,即在Arcgis中用投影栅格工具进行转换。 一、DEM数据的获取 在地理空间数据云网站下载DEM数据(精度包括30米和90米),数据格式为tif格式或img格式,坐标为WGS_1
基于Simulink实现电动汽车电机驱动系统的建模与仿真
通过上述步骤,我们成功实现了基于Simulink的电动汽车电机驱动系统的建模与仿真。该系统能够全面评估电机驱动系统的性能,验证控制策略的有效性,并通过优化设计提高车辆的动力性和效率。引入智能预测:结合人工智能技术,实现更智能的控制策略。扩展功能:增加对多种电机类型的支持,提升系统通用性。实验验证:将仿真模型应用于实际硬件,进行实验验证,评估其在实际工况下的表现。
推荐一个开源留言板:梦想家园留言板2008(ASP.NET2.0)
简介:欢迎使用功能强大的梦想家园留言板2008(Build 0509) 本留言板完全开源,可以在只需保留版权信息的情况下用于任何个人和商业目的 采用ASP.NET2.0+AJAX,体验AJAX无刷新乐趣 灵活的管理员配置功能:1.可配置留言是否需要审核才可显示2.可配置管理员的回复是否全部显示 初始管理帐号: admin 密码: admin
VSS shielding线可以floating吗?
shielding vss不打孔,就相当于dummy metal,如果周围有其他干扰线,他们影响了这段dummy metal,这段dummy metal仍然会对目标线产生干扰,bug的是,工具知道这段dummy是vss,那么计算寄生的时候仍然把它当vss,就有可能噪声没有完全消除,而用户又看不见。而真正接地的线,相当于一直是0,就不会对目标线产生干扰,一般加shield是在route之前,这时候shield能尽量覆盖目标线,等到绕线后再加就会出现shield只覆盖到一部分目标线的情况。
梦想家园留言板2008(ASP.NET2.0)
欢迎使用功能强大的梦想家园留言板2008(Build 0509) 本留言板完全开源,可以在只需保留版权信息的情况下用于任何个人和商业目的 采用ASP.NET2.0+AJAX,体验AJAX无刷新乐趣 灵活的管理员配置功能: 1.可配置留言是否需要审核才可显示 2.可配置管理员的回复是否全部显示 初始管理帐号: admin 密码...
IDEA的使用指南
IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.
贝叶斯统计 [韦来生编]
贝叶斯(Bayes)统计是近几十年来迅速发展起来的数理统计的一个重要分支。贝叶斯方法与经典统计方法的主要不同之处是进行统计推断时除了利用样本信息外,还要利用参数的先验信息,因此可以提高统计推断或统计决策的效果。它在经济、金融、生物、医学、自然科学和社会科学等许多领域具有广泛的应用。贝叶斯方法的研究已渗透到了统计学的几乎所有领域。作者在给中国科学技术大学概率论与数理统计专业本科生开设的“数理统计”课程中有一章专门讲授“贝叶斯方法和统计决策理论”,近十年来也给中国科学技术大学概率统计专业研究生开设过几次“贝叶斯分析”课。本书是在对过去讲稿的内容作了适当的增减和调整的基础上完成的。 本书共分六章。第1章是绪论,介绍了贝叶斯统计的若干基本概念,同时对必要的数理统计的基础知识有重点地作了回顾。第二章介绍了确定先验分布的若干可供选择的方法。第三章介绍了几类常见统计模型参数的后验分布的主要结果和计算方法。第四章和第五章分别介绍了“贝叶斯统计推断”和“贝叶斯统计决策”的内容。第六章介绍了贝叶斯统计计算的若干方法,包括蒙特卡洛方法和马尔可夫链蒙特卡洛(MCMC)方法以及相关统计软件的简介。本书的每一章附右大量的例题和习题,书末附有常用的几个表格和部分习题解答供读者查用。 大约可在54学时内讲授本书的第1章至第六章的主要内容,教师可根据学时需要选讲本书第1章至第六章的部分内容。书中标“*”号的章节可略去不讲,留给读者作为阅读材料。第六章例题中的R代码和部分例题的数据文件等可从作者个人主页下载。 作者在编写本书的过程中参考了J.O.Berger教授的《统计决策论及贝叶斯分析》和茆诗松教授编写的《贝叶斯统计》等书中的一些内容,以及《贝叶斯分析》一书中由张伟平博士编写的有关贝叶斯统计计算的一些内容。在此表示衷心感谢。 本书编写准备过程中,中国科学技术大学统计与金融系的几个研究生帮助完成了本书部分中文的录入和排版,作者对他们的辛勤工作表示真诚的感谢。高等教育出版社为本书的出版给予了大力支持。在此一并致谢。
[Asp.net] Web 定时自动发邮件
1. 添加Global.asax文件 add-->web--> Global Application Class 2. 在Global.asax里添加代码 protected void Application_Start(object sender, EventArgs e) { System.Timers.Timer ti
温度PID控制PLC程序
温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值
ASP.net发邮件的例程 !
ASP.net发邮件的例程: %> public void SendMail (Object Obj, EventArgs E) { labelSendMailResult.Text = ""; if (Page.IsValid) { MailMessage mailObj
霍尼韦尔SoftMaster使用说明
霍尼韦尔SoftMaster使用说明
关于 Asp.Net Forums 2.4 For .Net Framework 2.0 - Asp.Net Forums (venjiang)
Asp.Net Forums 2.4 是运行于.Net Framework 2.0 平台,基于2.3版本转化. 目前运行不稳定,并且有很多bug.见于很多朋友都在寻求2.0下的版本.现发布出来.此版本不宜实际运行网站,仅用于网友测试学习使用. 至此,基于asp.net fourms的本地化版本,将不再继续开发.您可以使用此版本进行改造完善,并进行商用,请保留原版权标识.同样也非常希望共享您的代码,
.Net 访问Mysql的connector/net5.0
http://www.mangbar.com/document/5d023b21128ed8d701128f861f21002b(转) asp.net连接Mysql(connector/net 5.0) 田勇 | 2007-5-15 19:41:35 Mysql的connector/net5.0下载地址:http://dev.mysql.com/get/D
ASP.NET Web Pages - WebMail 对象
摘要:本文详细介绍了ASP.NET Web Pages中WebMail对象的使用方法,提供2025年主流邮箱配置参数,包括腾讯企业邮、163邮箱等。文章展示了从基础到高级的邮件发送示例,涵盖纯文本、HTML邮件、附件、抄送等功能,并推荐将配置写入Web.config的最佳实践。同时提供了生产级完整代码示例和精美的邮件模板,帮助开发者快速实现安全稳定的邮件发送功能。
[翻译]ASP.NET 2.0中的健康监测系统(Health Monitoring)(3) - 触发自定义事件
原文地址:http://aspnet.4guysfromrolla.com/articles/062707-1.aspx[原文源码下载][译者改后源码下载][翻译]ASP.NET 2.0中的健康监测系统(Health Monitoring)(3) - 触发自定义事件原文发布日期:2007.06.27作者:Scott Mitchell翻译:webabcd介绍在之前的文章中(译者注:ASP.N
asp.net发送电子邮件_如何在ASP.NET Core中发送电子邮件
asp.net发送电子邮件 您通常需要通过应用程序发送电子邮件。 您可以利用MailKit NuGet包在ASP.NET Core中发送电子邮件。 MailKit是一个开源邮件客户端库,可以在Windows,Linux或Mac系统上运行的.NET或.NET Core应用程序中使用。 本文讨论了如何使用MailKit NuGet包在ASP.NET Core中发送电子邮件。 若要使用本文提供的代...
ASP.NET 2.0中改变passwordrecovery发邮件时的信息(原作)
在ASP.NET 2.0的登陆控件中(详细见我的文章,www.yesky.com/SoftChannel/72342380468043776/20041102/1870966.shtml),其中的passwordrecovery(恢复用户密码控件中,可以当用户输入密码回答问题,答案后,将用户的用户名和密码EMAIL给用户,其中,我们可以改善一下它的提示密码信息)
mysql connector net 5.0_asp.net连接Mysql(connector/net 5.0)
Mysql的connector/net5.0下载地址:http://dev.mysql.com/get/Downloads/Connector-Net/mysql-connector-net-5.0.6.zip/from/pick安装好以后,点属性,然后点查找目标,点向上一层目录,找到Binaries\.NET 2.0,然后将这个文件复制到你的工程目录下,一般这样的DLL文件会保存到bin目录下....
ASP.NET 2.0中的登陆控件简介(2)
Login控件 拖拉一个Login控件到窗体中,就发现和平时见到的登陆窗口差不多了,在该控件的属性窗口中,比较重要的是DestinationPageUrl 属性,该属性将指定用户在登陆后跳转的页面,而FailureText表示登陆失败后提示的文本,CreateUserText表示创建新用户的提示文本,PasswordRecoveryText表示提示密码的文本,PasswordRecovery
磁链:电机控制的核心状态量与实时观测方法
磁链是表征电机绕组中电磁能量耦合的关键状态量,本质为磁通与匝数的乘积,具有明确的拓扑性和方向性。其动态行为直接决定转矩生成与能量传递效率,是FOC、DTC、MPC等先进控制策略的共同物理基础。在工程实践中,磁链无法直测,需通过电压模型与电流模型融合估算,并依赖高精度电流/电压采样、在线参数辨识及龙伯格观测器等技术实现鲁棒实时观测。准确的磁链观测支撑了高性能驱动、能效优化乃至轴承故障预警等跨领域应用,已成为现代电机控制系统设计与调试的底层核心。
985




被折叠的 条评论
为什么被折叠?



