浅谈如何利用配置方式配置SMTP发送邮件
做多了网站平台,系统的人,都会或多或少遇到需要发送邮件这样的需求.
前几天在做一个企业黄页平台,也用到了邮件发送这一块.
但是担心有分站,又不想把SMTP直接写进code里,这样,修改起来太麻烦了
有的都是放在数据库里
但是,数据库里配置SMTP又有一点大材小用了
有多少人的网站会有多个SMTP服务器呢?
后来用了web.config里面的自定义配置节.分享一下我的实现方式
首先,在web.config\configuration\configSections增加一个自定义配置节的声明。
1 <section name="Smtp" type="SmSoft.Smtp.SmtpSectionConfig,SmSoft.Smtp"/>
然后,我们去web.config\configuration增加这个自定义配置声明的具体实现参数。
1 <Smtp>
2 <Default
3 SiteName="企业黄页"
4 Server="smtp.qq.com"
5 Username="service@wenbian.com"
6 Password="123123123"
7 SSL="False"
8 Port="25"/>
9 </Smtp>
2 <Default
3 SiteName="企业黄页"
4 Server="smtp.qq.com"
5 Username="service@wenbian.com"
6 Password="123123123"
7 SSL="False"
8 Port="25"/>
9 </Smtp>
我们在项目里建立一个叫SmtpSectionConfig的类
别忘记,我们先把System.Configuration这个程序集引用进来。
然后才能引用它的命名空间
1 using System.Configuration;
2 namespace SmSoft.Smtp
3 {
4 public class SmtpSectionConfig : ConfigurationSection
5 {
6 [ConfigurationProperty("Default")]
7 public DefaultSmtp Default
8 {
9 get
10 {
11 return (DefaultSmtp)this["Default"];
12 }
13
14 }
15
16 //可以增加几个其它用处的smtp
17 }
18 }
2 namespace SmSoft.Smtp
3 {
4 public class SmtpSectionConfig : ConfigurationSection
5 {
6 [ConfigurationProperty("Default")]
7 public DefaultSmtp Default
8 {
9 get
10 {
11 return (DefaultSmtp)this["Default"];
12 }
13
14 }
15
16 //可以增加几个其它用处的smtp
17 }
18 }
有人要问了,DefaultSmtp 它是什么?
呵呵
我们需要为具体的Smtp增加一个配置元素
我们现在就增加一个DefaultSmtp类
1 namespace SmSoft.Smtp
2 {
3 public class DefaultSmtp : ConfigurationElement
4 {
5 public DefaultSmtp()
6 : base()
7 {
8 this._Initialized = false;
9 }
10
11 [ConfigurationProperty("SiteName")]
12 public string SiteName
13 {
14 get
15 {
16 var o = this["SiteName"];
17 if (o != null)
18 return o.ToString();
19 else
20 return string.Empty;
21 }
22 }
23
24 [ConfigurationProperty("Server")]
25 public string Server
26 {
27 get
28 {
29 var o = this["Server"];
30 if (o != null)
31 return o.ToString();
32 else
33 return string.Empty;
34 }
35 }
36
37 [ConfigurationProperty("Username")]
38 public string Username
39 {
40 get
41 {
42 var o = this["Username"];
43 if (o != null)
44 return o.ToString();
45 else
46 return string.Empty;
47 }
48 }
49
50 [ConfigurationProperty("Password")]
51 public string Password
52 {
53 get
54 {
55 var o = this["Password"];
56 if (o != null)
57 return o.ToString();
58 else
59 return string.Empty;
60 }
61 }
62
63 [ConfigurationProperty("SSL")]
64 public bool SSL
65 {
66 get
67 {
68 var o = this["SSL"];
69 if (o != null)
70 return o.ToString().ToLower() == "true";
71 else
72 return false;
73 }
74 }
75
76 [ConfigurationProperty("Port")]
77 public int Port
78 {
79 get
80 {
81 var o = this["Port"];
82 var p = 25;
83 if (o == null)
84 return p;
85 else
86 {
87 int.TryParse(o.ToString(), out p);
88 return p;
89 }
90
91 }
92 }
93
94 private SmtpClient _Smtp;
95 private Boolean _Initialized;
96
97 public bool Send(MailMessage Msg)
98 {
99 if (this._Initialized == false)
100 return false;
101 this._Smtp.Send(Msg);
102 return true;
103 }
104
105 public void Init()
106 {
107 this._Smtp = new SmtpClient(this.Server, this.Port);
108 this._Smtp.UseDefaultCredentials = true;
109 this._Smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
110 this._Smtp.Credentials = new NetworkCredential()
111 {
112 Password = this.Password,
113 UserName = this.Username
114 };
115 this._Smtp.EnableSsl = this.SSL;
116 this._Initialized = true;
117 }
118 }
119 }
2 {
3 public class DefaultSmtp : ConfigurationElement
4 {
5 public DefaultSmtp()
6 : base()
7 {
8 this._Initialized = false;
9 }
10
11 [ConfigurationProperty("SiteName")]
12 public string SiteName
13 {
14 get
15 {
16 var o = this["SiteName"];
17 if (o != null)
18 return o.ToString();
19 else
20 return string.Empty;
21 }
22 }
23
24 [ConfigurationProperty("Server")]
25 public string Server
26 {
27 get
28 {
29 var o = this["Server"];
30 if (o != null)
31 return o.ToString();
32 else
33 return string.Empty;
34 }
35 }
36
37 [ConfigurationProperty("Username")]
38 public string Username
39 {
40 get
41 {
42 var o = this["Username"];
43 if (o != null)
44 return o.ToString();
45 else
46 return string.Empty;
47 }
48 }
49
50 [ConfigurationProperty("Password")]
51 public string Password
52 {
53 get
54 {
55 var o = this["Password"];
56 if (o != null)
57 return o.ToString();
58 else
59 return string.Empty;
60 }
61 }
62
63 [ConfigurationProperty("SSL")]
64 public bool SSL
65 {
66 get
67 {
68 var o = this["SSL"];
69 if (o != null)
70 return o.ToString().ToLower() == "true";
71 else
72 return false;
73 }
74 }
75
76 [ConfigurationProperty("Port")]
77 public int Port
78 {
79 get
80 {
81 var o = this["Port"];
82 var p = 25;
83 if (o == null)
84 return p;
85 else
86 {
87 int.TryParse(o.ToString(), out p);
88 return p;
89 }
90
91 }
92 }
93
94 private SmtpClient _Smtp;
95 private Boolean _Initialized;
96
97 public bool Send(MailMessage Msg)
98 {
99 if (this._Initialized == false)
100 return false;
101 this._Smtp.Send(Msg);
102 return true;
103 }
104
105 public void Init()
106 {
107 this._Smtp = new SmtpClient(this.Server, this.Port);
108 this._Smtp.UseDefaultCredentials = true;
109 this._Smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
110 this._Smtp.Credentials = new NetworkCredential()
111 {
112 Password = this.Password,
113 UserName = this.Username
114 };
115 this._Smtp.EnableSsl = this.SSL;
116 this._Initialized = true;
117 }
118 }
119 }
同样,因为用到了ConfigurationEelement,我们需要增加System.Configuration的引用
我们在增加一个用来发送邮件的快捷方法,就大功告成了。
1 namespace SmSoft.Smtp
2 {
3 public class MailHelper
4 {
5 private static DefaultSmtp _SmtpServer;
6 static MailHelper()
7 {
8 try
9 {
10 var smtp = (SmtpSectionConfig)ConfigurationManager.GetSection("Smtp");
11 _SmtpServer = smtp.Default;
12 _SmtpServer.Init();
13 }
14 catch
15 {
16 throw new Exception("请配置Smpt邮件发送配置节");
17 }
18 }
19 public static Boolean Send(String Email, String Msg)
20 {
21 MailMessage Message = new MailMessage(new MailAddress(_SmtpServer.Username, _SmtpServer.SiteName, Encoding.GetEncoding("UTF-8")), new MailAddress(Email));
22 Message.Body = Msg;
23 Message.BodyEncoding = Encoding.GetEncoding("UTF-8");
24 Message.IsBodyHtml = true;
25 Message.Priority = MailPriority.Normal;
26 Message.Subject = "主题";
27 Message.SubjectEncoding = Encoding.GetEncoding("UTF-8");
28 _SmtpServer.Send(Message);
29 return true;
30
31 }
32 }
33 }
2 {
3 public class MailHelper
4 {
5 private static DefaultSmtp _SmtpServer;
6 static MailHelper()
7 {
8 try
9 {
10 var smtp = (SmtpSectionConfig)ConfigurationManager.GetSection("Smtp");
11 _SmtpServer = smtp.Default;
12 _SmtpServer.Init();
13 }
14 catch
15 {
16 throw new Exception("请配置Smpt邮件发送配置节");
17 }
18 }
19 public static Boolean Send(String Email, String Msg)
20 {
21 MailMessage Message = new MailMessage(new MailAddress(_SmtpServer.Username, _SmtpServer.SiteName, Encoding.GetEncoding("UTF-8")), new MailAddress(Email));
22 Message.Body = Msg;
23 Message.BodyEncoding = Encoding.GetEncoding("UTF-8");
24 Message.IsBodyHtml = true;
25 Message.Priority = MailPriority.Normal;
26 Message.Subject = "主题";
27 Message.SubjectEncoding = Encoding.GetEncoding("UTF-8");
28 _SmtpServer.Send(Message);
29 return true;
30
31 }
32 }
33 }
var smtp = (SmtpSectionConfig)ConfigurationManager.GetSection("Smtp");
这样,就可以获取我们在web.config内配置的SMTP服务器信息了
非常简单
以后发送邮件,只需要
SmSoft.Smtp.MailHelper.Send(email, content)
作者:Sam
QQ群: 40529944
出处:www.cnblogs.com/sam251
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
QQ群: 40529944
出处:www.cnblogs.com/sam251
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
分类: ASP.net 随笔
标签: smtp