///// <summary> ///// 发送短信 ///// </summary> ///// <param name="smsInfo">短信实体</param> ///// <returns></returns> //private static async void SendSMS(SMSInfo smsInfo, SmsOption smsOption) //{ // //记得加上短信内容长度截取的代码,以免恶意用户发送超长短信导致不必费用. // try // { // if (smsInfo != null) // { // smsInfo.SMSContent = smsInfo.SMSContent.AutoSubstring(0, 70); // string url = smsOption.ApiUrl; // string gbkStr = smsInfo.SMSContent; // url = url.Replace("$TOPHONE", smsInfo.ToPhone); // url = url.Replace("$CONTENT", HttpUtility.UrlEncode(smsInfo.SMSContent, Encoding.UTF8)); // string responseStr = await Get(url); // if (!string.IsNullOrWhiteSpace(responseStr)) // { // //<response><result>0</result></response> // responseStr = responseStr.Replace("<response><result>", ""); // responseStr = responseStr.Replace("</result></response>", ""); // int flag = responseStr.ConvertTo(-717); // if (flag != 0) // { // Dictionary<int, string> dicReason = new Dictionary<int, string>(); // dicReason.Add(-99, "其它故障"); // dicReason.Add(5, "含有禁止发送的内容"); // dicReason.Add(-1, "用户名或密码不正确"); // dicReason.Add(-2, "余额不够"); // dicReason.Add(-3, "帐号没有注册"); // dicReason.Add(-4, "内容超长"); // dicReason.Add(-5, "账号路由为空"); // dicReason.Add(-6, "手机号码超过1000个(或手机号码非法或错误"); // dicReason.Add(-8, "扩展号超长"); // dicReason.Add(-12, "Key值要是32位长的英文,建议32个a"); // dicReason.Add(-13, "定时时间错误或者小于当前系统时间"); // dicReason.Add(-17, "手机号码为空"); // dicReason.Add(-18, "号码不是数字或者逗号不是英文逗号"); // dicReason.Add(-19, "短信内容为空"); // string info = "短信发送失败:"; // if (dicReason.ContainsKey(flag)) // { // info = info + dicReason[flag]; // } // else // { // info = info + flag + "(该代码没在错误类型中。)[" + responseStr + "]"; // } // throw new Exception(info); // } // } // else // { // throw new Exception("发送短信接口返回的响应字符串为空"); // } // } // if (OnRecordSMS != null) // { // OnRecordSMS.Invoke(smsInfo); // } // } // catch (Exception ex) // { // throw new Exception("系统发送短信时发生异常:" + ex); // } //} /// <summary> /// 发送邮件,为异步发送邮件而写的内部方法 /// </summary> ///<param name="miData">邮件信息实体</param> /// <returns></returns> private static void SendEmail(MailInfo miData, MailOption mailOption) { try { if (miData != null) { MailMessage mailMessage = new MailMessage(); mailMessage.Priority = MailPriority.Normal; mailMessage.IsBodyHtml = true; mailMessage.From = new MailAddress(mailOption.EmailUserName); mailMessage.To.Add(miData.ToMail); mailMessage.Subject = miData.Title; mailMessage.Body = miData.Content; SmtpClient smtp = new SmtpClient(); smtp.Host = mailOption.EmailHost; smtp.Port = 587; smtp.EnableSsl = true; smtp.Credentials = new NetworkCredential(mailOption.EmailUserName, mailOption.EmailPassword); smtp.Send(mailMessage); if (OnRecordMail != null) { OnRecordMail.Invoke(miData); } } } catch (Exception ex) { throw new Exception("系统发送邮件时发生异常:" + ex); } }
//TDbContext tDbContex; public ValuesController(IOptions <MailOption> mailOption, IOptions <IpInfoQueryOption> ipQueryOption, IStudentServic studentServic, SomeoneClass someone, ISchedulerFactory schedulerFactory, MyDbContext efDbContext, ILogger <ValuesController> logger, IMapper mapper, IDistributedCache cache) { this.mailOption = mailOption.Value; this.ipQueryOption = ipQueryOption.Value; this.db = efDbContext; this.logger = logger; this.mapper = mapper; this.cache = cache; //this.tDbContex = tDbContext; this.schedulerFactory = schedulerFactory; this.studentServic = studentServic; this.someone = someone; }
/// <summary> /// 异步发送邮件 /// </summary> ///<param name="mi">邮件信息实体</param> public static void AsyncSendEmail(MailInfo mi, MailOption mailOption) { bool canSend = true; if (!mailOption.IPWhiteList.Contains(mi.OperaterIP))//如果不IP在白名单中,则用检查 { if (OnCheckMail != null) { canSend = OnCheckMail.Invoke(mi); } } if (canSend) { ThreadPool.QueueUserWorkItem(new WaitCallback(x => { SendEmail(mi, mailOption); })); } }