web.config的自定义配置

在ASP.NET2.0中,使用自定义配置变得容易了很多。只要编写继承于System.Configuration.ConfiguationSetion的类,并将公共属性封装为ConfigurationProperty属性,表示它的值需要从web.config文件中的配置中读取,实际的读取操作发生在用get方法从基类中读取设置的时候!由于元素嵌套在一个自定义部分中,需要创建一个继承于ConfigurationElement的新类,并且用ConfigurationProperty重新定义属性。

参考代码:

View Code
 1 public class MyMailSection :ConfigurationSection
2 {
3 [ConfigurationProperty("defaultConnectionStringName", DefaultValue = "LocalSqlServer")]
4 public string DefaultConnectionStringName { get {
5 return (string)base["defaultConnectionStringName"];
6 }
7 set { base["defaultConnectionStringName"] = value; }
8 }
9
10 [ConfigurationProperty("defaultCacheDuration", DefaultValue = 600)]
11 public int DefaultCacheDuration
12 { get { return (int)base["defaultCacheDuration"]; }
13 set { base["defaultCacheDuration"] = value; }
14 }
15
16 [ConfigurationProperty("contactFrom", IsRequired = true)]
17 public ContactFromElement ContactFrom
18 {
19 get { return (ContactFromElement)base["contactFrom"]; }
20 //set { base["contactFrom"] = value; }
21 }
22 }
23
24 public class ContactFromElement:ConfigurationElement
25 {
26 [ConfigurationProperty("mailSubject", DefaultValue = "邮件来自某某网站:{0}")]
27 public string MailSubject
28 {
29 get { return (string)base["mailSubject"]; }
30 set { base["mailSubject"] = value; }
31 }
32
33 [ConfigurationProperty("mailTo",IsRequired=true )]
34 public string MailTo
35 {
36 get { return (string)base["mailTo"]; }
37 set { base["mailTo"] = value; }
38 }
39
40 [ConfigurationProperty("mailCC")]
41 public string MailCC
42 {
43 get { return (string)base["mailCC"]; }
44 set { base["mailCC"] = value; }
45 }
46 }


定义了客户发送邮件给我们网站的配置部分。包括主题/正文,其中mailTo属性存储了网站管理员的邮箱地址,以后网络注册的客户如果需要邮件联系管理员,可以通过这个属性配置。

这里申请一个QQ新号,作为网站管理员的邮箱。

image

接下来,必须在web.config的<configSection>中新建一个元素来映射MyMailSection类。定义好了这个映射后,就可以编写自定义的设置了,如下所示:

 

View Code
1 <configuration>
2 <configSections>
3 <section name="myMail" type="三层体系.MyMailSection"/>
4 </configSections>
5
6 <myMail defaultConnectionStringName="localSqlServer">
7 <contactFrom mailTo="1615974697@qq.com" />
8 </myMail>

注意:上面的type必须是全名称,若MyMailSection类文件在App_Code下,应该如是写:type=”三层体系.MyMailSection,__code”
这里由于是自定义配置节,所以没有智能提示,注意大小写不要错误
  比如这里的mailTo=,必须和相应代码中的[ConfigurationProperty("mailTo",IsRequired=true )] 相互匹配。
 
接下来,就可以代码进行访问这个自定义配置的节点了!
首先创建页面Contact.aspx,它用来允许用户通过在线填写表单,来向网站管理者发送邮件。
为了演示简单,去掉样式,源视图如下:
View Code
 1 <table>
2 <tr>
3 <td>您的姓名:</td>
4 <td><asp:TextBox ID="txtName" runat="server">网风</asp:TextBox></td>
5 </tr>
6 <tr>
7 <td>您的邮箱:</td>
8 <td><asp:TextBox ID="txtEmail" runat="server">1692076402@qq.com</asp:TextBox></td>
9 </tr>
10 <tr>
11 <td>您的邮箱密码:</td>
12 <td><asp:TextBox ID="txtPassword" TextMode="Password" runat="server"></asp:TextBox>
13 </td>
14 </tr>
15 <tr>
16 <td>邮箱服务器:</td>
17 <td> <asp:TextBox ID="txtMailServer" runat="server">smtp.qq.com</asp:TextBox>
18 (如<span
19 style="color: rgb(0, 0, 0); font-family: arial; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); font-size: small; display: inline !important; float: none; ">smtp.qq.com&nbsp;
20 smtp.163.com</span>)</td>
21 </tr>
22 <tr>
23 <td>主题:</td>
24 <td><asp:TextBox ID="txtSubject" runat="server">邮件主题</asp:TextBox></td>
25 </tr>
26 <tr>
27 <td>正文:</td>
28 <td><asp:TextBox ID="txtBody" runat="server" Height="182px" TextMode="MultiLine" Width="293px"></asp:TextBox>
29 </td>
30 </tr>
31 <tr>
32 <td><asp:Literal ID="lblInfo" runat="server"></asp:Literal>
33 </td>
34 <td><asp:Button ID="Button1" runat="server" onclick="Button1_Click"
35 Text="发送邮件给我们网站" /></td>
36 </tr>
37 </table>


对应的设计视图的截屏如下:

对应的按钮代码:

View Code
 1 protected void Button1_Click(object sender, EventArgs e)
2 {
3 MailMessage msg=new MailMessage();//电子邮件
4 msg.IsBodyHtml=false;
5 msg.From=new MailAddress(txtEmail.Text,txtName.Text);//邮件发信人地址
6 MyMailSection myMail=
7 WebConfigurationManager.GetSection("myMail") as MyMailSection;//从当前的web.config中检索指定的配置节(myMail)
8 msg.To.Add(new MailAddress(myMail.ContactFrom.MailTo));//邮件的收件人地址(存储在web.config节点myMail中)
9 if (!string.IsNullOrEmpty(myMail.ContactFrom.MailCC))
10 {
11 msg.CC.Add(myMail.ContactFrom.MailCC);//邮件的抄送地址
12 }
13 msg.Subject = string.Format( myMail.ContactFrom.MailSubject,
14 txtSubject.Text); //邮件的主题
15 msg.Body = txtBody.Text; //邮件的正文
16 SmtpClient client = new SmtpClient(); //允许使用简单邮件传输协议(SMTP)来发送邮件
17
18 //邮件发送人的身份凭证(登录邮箱和登录密码)
19 client.Credentials = new System.Net.NetworkCredential(txtEmail.Text, txtPassword.Text);
20
21 client.Host = txtMailServer.Text;//邮箱发送服务器的名称或IP
22 // client.EnableSsl = true; //是否使用安全套接字层(SSL)连接加密
23
24 client.Send(msg);//将指定的邮件msg发送到SMTP服务器(Host属性)以便传递。
25
26 txtName.Text = "";
27 txtEmail.Text = "";
28 txtSubject.Text = "";
29 txtBody.Text = "";
30
31
32 lblInfo.Text = "发送成功!";
33 }


 

运行,看效果:

按钮事件出现异常,上百度进行搜索!

原来网风的QQ邮箱还没有开通smtp功能,不多说,赶紧设置!

如果是马上注册就想开通是不可以的,目前QQ的提示:【您的QQ邮箱激活时间不满14天,故暂时无法设置。】

开通好后,自然就可以正常发送邮件了。网络管理员可以登录邮箱看到接收到的邮件:

posted @ 2011-12-22 20:50  net小虫  阅读(408)  评论(0编辑  收藏  举报