您好,欢迎访问一九零五行业门户网

.Net Core分布式邮件系统

left5db79b134e9f6b82c0b36e0489ee08ed: 30px;>本篇分享的是由netcore搭建的分布式邮件系统,主要采用netcore的api和控制台应用程序,由于此系统属于公司的所以这里只能分享设计图和一些单纯不设计业务的类或方法;
为什么要在公司中首例采用netcore做开发为什么要在公司中首例采用netcore做开发,有些netcoreapi不是还不全面么,您都敢尝试?恐怕会有人这样问我,我只能告诉你netcore现在出2.0版本了,很多framwork的常用封装都已经有了,况且她主打的是mvc模式,能够高效的开发系统,也有很多core的nuget包支持了,已经到达了几乎可以放心大胆使用的地步,退一万不说有些东西不支持那这又如何,可以采用接口的方式从其他地方对接过来也是一种不错的处理方案。为了让c#这门优秀的语言被广泛应用,默默努力着。
目前我写的netcore方面的文章aspnetcore - mvc实战系列目录
.netcore上传多文件的几种示例
开源一个跨平台运行的服务插件 - taskcore.mainform
net core-学习笔记
asp.netcore1.1版本没了project.json,这样来生成跨平台包
正片环节 - 分布式邮件系统设计图
分布式邮件系统说明其实由上图可以知晓这里我主要采用了api+服务的模式,这也是现在互联网公司经常采用的一种搭配默认;利用api接受请求插入待发送邮件队列和入库,然后通过部署多个netcore跨平台服务(这里服务指的是:控制台应用)来做分布式处理操作,跨平台服务主要操作有:
. 邮件发送
. 邮件发送状态的通知(如果需要通知子业务,那么需要通知业务方邮件发送的状态)
. 通知失败处理(自动往绑定的责任人发送一封邮件)
. 填充队列(如果待发邮件队列或者通知队列数据不完整,需要修复队列数据)
api接口的统一验证入口这里我用最简单的方式,继承controller封装了一个父级的basecontroller,来让各个api的controller基础统一来做身份验证;来看看重写 public override void onactionexecuting(actionexecutingcontext context) 的验证代码:
1 public override void onactionexecuting(actionexecutingcontext context) 2 { 3 base.onactionexecuting(context); 4 5 var moresponse = new mobaserp(); 6 try 7 { 8 9 #region 安全性验证 10 11 var key = "request"; 12 if (!context.actionarguments.containskey(key)) { moresponse.msg = "请求方式不正确"; return; } 13 var request = context.actionarguments[key]; 14 var baserq = request as mobaserq; 15 //暂时不验证登录账号密码 16 if (string.isnullorwhitespace(baserq.username) || string.isnullorwhitespace(baserq.userpwd)) { moresponse.msg = "登录账号或密码不能为空"; return; } 17 else if (baserq.accid <= 0) { moresponse.msg = "发送者id无效"; return; } 18 else if (string.isnullorwhitespace(baserq.funcname)) { moresponse.msg = "业务方法名不正确"; return; } 19 20 //token验证 21 var strtoken = publicclass._md5($"{baserq.username}{baserq.accid}", ""); 22 if (!strtoken.equals(baserq.token, stringcomparison.ordinalignorecase)) { moresponse.msg = "token验证失败"; return; } 23 24 //验证发送者id 25 if (string.isnullorwhitespace(baserq.ip)) 26 { 27 var account = _db.emailaccount.singleordefault(b => b.id == baserq.accid); 28 if (account == null) { moresponse.msg = "发送者id无效。"; return; } 29 else 30 { 31 if (account.status != (int)enumhelper.emstatus.启用) 32 { 33 moresponse.msg = "发送者id已禁用"; return; 34 } 35 36 //验证ip 37 var iparr = account.allowips.split(new char[] { ',' }, stringsplitoptions.removeemptyentries); 38 //当前请求的ip 39 var nowip = this.getuserip(); 40 baserq.ip = nowip; 41 //默认*为所有ip , 匹配ip 42 if (!iparr.any(b => b.equals("*")) && !iparr.any(b => b.equals(nowip))) 43 { 44 moresponse.msg = "请求ip为授权"; return; 45 } 46 } 47 } 48 else 49 { 50 var account = _db.emailaccount.singleordefault(b => b.id == baserq.accid && b.allowips.any(bb => bb.equals(baserq.ip))); 51 if (account == null) { moresponse.msg = "发送者未授权"; return; } 52 else if (account.status != (int)enumhelper.emstatus.启用) 53 { 54 moresponse.msg = "发送者id已禁用"; return; 55 } 56 } 57 58 //内容非空,格式验证 59 if (!context.modelstate.isvalid) 60 { 61 var values = context.modelstate.values.where(b => b.errors.count > 0); 62 if (values.count() > 0) 63 { 64 moresponse.msg = values.first().errors.first().errormessage; 65 return; 66 } 67 } 68 69 #endregion 70 71 moresponse.status = 1; 72 } 73 catch (exception ex) 74 { 75 moresponse.msg = "o no请求信息错误"; 76 } 77 finally 78 { 79 if (moresponse.status == 0) { context.result = json(moresponse); } 80 } 81 }
邮件请求父类实体:
1 /// <summary> 2 /// 邮件请求父类 3 /// </summary> 4 public class mobaserq 5 { 6 7 public string username { get; set; } 8 9 public string userpwd { get; set; } 10 11 /// <summary> 12 /// 验证token(md5(账号+配置发送者账号信息的id+ip)) 必填 13 /// </summary> 14 public string token { get; set; } 15 16 /// <summary> 17 /// 配置发送者账号信息的id 必填 18 /// </summary> 19 public int accid { get; set; } 20 21 /// <summary> 22 /// 业务方法名称 23 /// </summary> 24 public string funcname { get; set; } 25 26 /// <summary> 27 /// 请求者ip,如果客户端没赋值,默认服务端获取 28 /// </summary> 29 public string ip { get; set; } 30 31 }
第三方nuget包的便利此邮件系统使用到了第三方包,这也能够看出有很多朋友正为开源,便利,netcore的推广努力着;
首先看看mailkit(邮件发送)包,通过安装下载命令: install-package mailkit 能够下载最新包,然后你不需要做太花哨的分装,只需要正对于邮件发送的服务器,端口,账号,密码做一些设置基本就行了,如果可以您可以直接使用我的代码:
1 /// <summary> 2 /// 发送邮件 3 /// </summary> 4 /// <param name="dictoemail"></param> 5 /// <param name="title"></param> 6 /// <param name="content"></param> 7 /// <param name="name"></param> 8 /// <param name="fromemail"></param> 9 /// <returns></returns> 10 public static bool _sendemail( 11 dictionary<string, string> dictoemail, 12 string title, string content, 13 string name = "爱留图网", string fromemail = "841202396@qq.com", 14 string host = "smtp.qq.com", int port = 587, 15 string username = "841202396@qq.com", string userpwd = "123123") 16 { 17 var isok = false; 18 try 19 { 20 if (string.isnullorwhitespace(title) || string.isnullorwhitespace(content)) { return isok; } 21 22 //设置基本信息 23 var message = new mimemessage(); 24 message.from.add(new mailboxaddress(name, fromemail)); 25 foreach (var item in dictoemail.keys) 26 { 27 message.to.add(new mailboxaddress(item, dictoemail[item])); 28 } 29 message.subject = title; 30 message.body = new textpart("html") 31 { 32 text = content 33 }; 34 35 //链接发送 36 using (var client = new smtpclient()) 37 { 38 // for demo-purposes, accept all ssl certificates (in case the server supports starttls) 39 client.servercertificatevalidationcallback = (s, c, h, e) => true; 40 41 //采用qq邮箱服务器发送邮件 42 client.connect(host, port, false); 43 44 // note: since we don't have an oauth2 token, disable 45 // the xoauth2 authentication mechanism. 46 client.authenticationmechanisms.remove("xoauth2"); 47 48 //qq邮箱,密码(安全设置短信获取后的密码) ufiaszkkulbabejh 49 client.authenticate(username, userpwd); 50 51 client.send(message); 52 client.disconnect(true); 53 } 54 isok = true; 55 } 56 catch (exception ex) 57 { 58 59 } 60 return isok; 61 }
redis方面的操作包stackexchange.redis,现在netcore支持很多数据库驱动(例如:sqlserver,mysql,postgressql,db2等)这么用可以参考下这篇文章aspnetcore - mvc实战系列(一)之sqlserver表映射实体模型,不仅如此还支持很多缓存服务(如:memorycach,redis),这里讲到的就是redis,我利用redis的list的队列特性来做分布式任务存储,尽管目前我用到的只有一个主redis服务还没有业务场景需要用到主从复制等功能;这里分享的代码是基于stackexchange.redis基础上封装对于string,list的操作:
1 public class stackredis : idisposable 2 { 3 #region 配置属性 基于 stackexchange.redis 封装 4 //连接串 (注:ip:端口,属性=,属性=) 5 public string _connectionstring = "127.0.0.1:6377,password=shenniubuxing3"; 6 //操作的库(注:默认0库) 7 public int _db = 0; 8 #endregion 9 10 #region 管理器对象 11 12 /// <summary> 13 /// 获取redis操作类对象 14 /// </summary> 15 private static stackredis _stackredis; 16 private static object _locker_stackredis = new object(); 17 public static stackredis current 18 { 19 get 20 { 21 if (_stackredis == null) 22 { 23 lock (_locker_stackredis) 24 { 25 _stackredis = _stackredis new stackredis(); 26 return _stackredis; 27 } 28 } 29 30 return _stackredis; 31 } 32 } 33 34 /// <summary> 35 /// 获取并发链接管理器对象 36 /// </summary> 37 private static connectionmultiplexer _redis; 38 private static object _locker = new object(); 39 public connectionmultiplexer manager 40 { 41 get 42 { 43 if (_redis == null) 44 { 45 lock (_locker) 46 { 47 _redis = _redis getmanager(this._connectionstring); 48 return _redis; 49 } 50 } 51 52 return _redis; 53 } 54 } 55 56 /// <summary> 57 /// 获取链接管理器 58 /// </summary> 59 /// <param name="connectionstring"></param> 60 /// <returns></returns> 61 public connectionmultiplexer getmanager(string connectionstring) 62 { 63 return connectionmultiplexer.connect(connectionstring); 64 } 65 66 /// <summary> 67 /// 获取操作数据库对象 68 /// </summary> 69 /// <returns></returns> 70 public idatabase getdb() 71 { 72 return manager.getdatabase(_db); 73 } 74 #endregion 75 76 #region 操作方法 77 78 #region string 操作 79 80 /// <summary> 81 /// 根据key移除 82 /// </summary> 83 /// <param name="key"></param> 84 /// <returns></returns> 85 public async task<bool> remove(string key) 86 { 87 var db = this.getdb(); 88 89 return await db.keydeleteasync(key); 90 } 91 92 /// <summary> 93 /// 根据key获取string结果 94 /// </summary> 95 /// <param name="key"></param> 96 /// <returns></returns> 97 public async task<string> get(string key) 98 { 99 var db = this.getdb(); 100 return await db.stringgetasync(key); 101 } 102 103 /// <summary> 104 /// 根据key获取string中的对象 105 /// </summary> 106 /// <typeparam name="t"></typeparam> 107 /// <param name="key"></param> 108 /// <returns></returns> 109 public async task<t> get<t>(string key) 110 { 111 var t = default(t); 112 try 113 { 114 var _str = await this.get(key); 115 if (string.isnullorwhitespace(_str)) { return t; } 116 117 t = jsonconvert.deserializeobject<t>(_str); 118 } 119 catch (exception ex) { } 120 return t; 121 } 122 123 /// <summary> 124 /// 存储string数据 125 /// </summary> 126 /// <param name="key"></param> 127 /// <param name="value"></param> 128 /// <param name="expireminutes"></param> 129 /// <returns></returns> 130 public async task<bool> set(string key, string value, int expireminutes = 0) 131 { 132 var db = this.getdb(); 133 if (expireminutes > 0) 134 { 135 return db.stringset(key, value, timespan.fromminutes(expireminutes)); 136 } 137 return await db.stringsetasync(key, value); 138 } 139 140 /// <summary> 141 /// 存储对象数据到string 142 /// </summary> 143 /// <typeparam name="t"></typeparam> 144 /// <param name="key"></param> 145 /// <param name="value"></param> 146 /// <param name="expireminutes"></param> 147 /// <returns></returns> 148 public async task<bool> set<t>(string key, t value, int expireminutes = 0) 149 { 150 try 151 { 152 var jsonoption = new jsonserializersettings() 153 { 154 referenceloophandling = referenceloophandling.ignore 155 }; 156 var _str = jsonconvert.serializeobject(value, jsonoption); 157 if (string.isnullorwhitespace(_str)) { return false; } 158 159 return await this.set(key, _str, expireminutes); 160 } 161 catch (exception ex) { } 162 return false; 163 } 164 #endregion 165 166 #region list操作(注:可以当做队列使用) 167 168 /// <summary> 169 /// list长度 170 /// </summary> 171 /// <typeparam name="t"></typeparam> 172 /// <param name="key"></param> 173 /// <returns></returns> 174 public async task<long> getlistlen<t>(string key) 175 { 176 try 177 { 178 var db = this.getdb(); 179 return await db.listlengthasync(key); 180 } 181 catch (exception ex) { } 182 return 0; 183 } 184 185 /// <summary> 186 /// 获取队列出口数据并移除 187 /// </summary> 188 /// <typeparam name="t"></typeparam> 189 /// <param name="key"></param> 190 /// <returns></returns> 191 public async task<t> getlistandpop<t>(string key) 192 { 193 var t = default(t); 194 try 195 { 196 var db = this.getdb(); 197 var _str = await db.listrightpopasync(key); 198 if (string.isnullorwhitespace(_str)) { return t; } 199 t = jsonconvert.deserializeobject<t>(_str); 200 } 201 catch (exception ex) { } 202 return t; 203 } 204 205 /// <summary> 206 /// 集合对象添加到list左边 207 /// </summary> 208 /// <typeparam name="t"></typeparam> 209 /// <param name="key"></param> 210 /// <param name="values"></param> 211 /// <returns></returns> 212 public async task<long> setlists<t>(string key, list<t> values) 213 { 214 var result = 0l; 215 try 216 { 217 var jsonoption = new jsonserializersettings() 218 { 219 referenceloophandling = referenceloophandling.ignore 220 }; 221 var db = this.getdb(); 222 foreach (var item in values) 223 { 224 var _str = jsonconvert.serializeobject(item, jsonoption); 225 result += await db.listleftpushasync(key, _str); 226 } 227 return result; 228 } 229 catch (exception ex) { } 230 return result; 231 } 232 233 /// <summary> 234 /// 单个对象添加到list左边 235 /// </summary> 236 /// <typeparam name="t"></typeparam> 237 /// <param name="key"></param> 238 /// <param name="value"></param> 239 /// <returns></returns> 240 public async task<long> setlist<t>(string key, t value) 241 { 242 var result = 0l; 243 try 244 { 245 result = await this.setlists(key, new list<t> { value }); 246 } 247 catch (exception ex) { } 248 return result; 249 } 250 251 252 #endregion 253 254 #region 额外扩展 255 256 /// <summary> 257 /// 手动回收管理器对象 258 /// </summary> 259 public void dispose() 260 { 261 this.dispose(_redis); 262 } 263 264 public void dispose(connectionmultiplexer con) 265 { 266 if (con != null) 267 { 268 con.close(); 269 con.dispose(); 270 } 271 } 272 273 #endregion 274 275 #endregion 276 }
用到redis的那些操作就添加哪些就行了,也不用太花哨能用就行;
如何生成跨平台的api服务和应用程序服务这小节的内容最重要,由于之前有相关的文章,这里就不用再赘述了,来这里看看:asp.netcore1.1版本没了project.json,这样来生成跨平台包
以上就是.net core分布式邮件系统的详细内容。
其它类似信息

推荐信息