博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何利用配置方式配置SMTP发送邮件
阅读量:7254 次
发布时间:2019-06-29

本文共 3949 字,大约阅读时间需要 13 分钟。

转自——学云网 做多了网站平台,系统的人,都会或多或少遇到需要发送邮件这样的需求. 前几天在做一个企业黄页平台,也用到了邮件发送这一块. 但是担心有分站,又不想把SMTP直接写进code里,这样,修改起来太麻烦了 有的都是放在数据库里 但是,数据库里配置SMTP又有一点大材小用了 有多少人的网站会有多个SMTP服务器呢? 后来用了web.config里面的自定义配置节.分享一下我的实现方式 首先,在web.config\configuration\configSections增加一个自定义配置节的声明。 1 然后,我们去web.config\configuration增加这个自定义配置声明的具体实现参数。 复制代码 1 2 9 复制代码 我们在项目里建立一个叫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 } 复制代码 有人要问了,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 } 复制代码 同样,因为用到了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 } 复制代码 var smtp = (SmtpSectionConfig)ConfigurationManager.GetSection("Smtp"); 这样,就可以获取我们在web.config内配置的SMTP服务器信息了 非常简单 以后发送邮件,只需要 SmSoft.Smtp.MailHelper.Send(email, content)

转载于:https://www.cnblogs.com/along521/archive/2012/06/26/2563186.html

你可能感兴趣的文章
hadoop安装
查看>>
作业-闰年流程图
查看>>
delegate事件委托
查看>>
Linux Process VS Thread VS LWP
查看>>
Linux命令——free
查看>>
.net操作AD域
查看>>
IO流实现模拟软件试用的功能
查看>>
ScrollView和ListView滑动冲突问题
查看>>
LAMP环境的搭建(二)----Mysql安装与配置
查看>>
js用解构来定义变量并赋值
查看>>
Linux 驱动开发笔记(一)
查看>>
2017-2018-2 20165226 实验五《网络编程与安全》实验报告
查看>>
iOS开发小技巧--相机相册的正确打开方式
查看>>
VB6 padleft padright
查看>>
人生的悲哀莫过于:求而不得,舍而不能,得而不惜
查看>>
<资料收集>让使用了SQLite的.NET应用自适应32位/64位系统
查看>>
php laravel 框架搭建与运行
查看>>
Java-transient总结
查看>>
DBA_实践指南系列10_Oracle Erp R12诊断功能Diagnostic(案例)
查看>>
phpStorm配置xdebug调试总结 [Mac/Linux]
查看>>