/// <summary> /// 发送邮件通知 /// </summary> /// <param name="isLogin"></param> /// <param name="_userInfo"></param> /// <param name="tid"></param> /// <param name="p"></param> /// <param name="db"></param> internal void SendMail(UserInfo currentUser, int tid, BBS_Posts p, SqlSugarClient db) { //未配置邮箱不发送 if (PubGet.GetEmailUserName.IsNullOrEmpty()) { return; } var topic = db.Queryable <BBS_Topics>().Single(it => it.Tid == tid); var isOneUser = currentUser.Id == topic.Posterid; var html = FileSugar.FileToString(FileSugar.GetMapPath("~/Template/mail/Replies.html")).Replace('\r', ' ').Replace('\n', ' '); var oldHtml = html; //发贴和回贴不是同一个人 if (isOneUser.IsFalse()) { var toUser = db.Queryable <UserInfo>().Single(it => it.Id == topic.Posterid); if (toUser.Email.IsEamil()) { string toUserName = toUser.NickName; string fromUserName = currentUser.NickName; string toMail = toUser.Email; MailSmtp ms = new MailSmtp(PubGet.GetEmailSmtp, PubGet.GetEmailUserName, PubGet.GetEmailPassword); string url = RequestInfo.HttpDomain + "/Ask/{0}/{1}#btnSubmit".ToFormat(topic.Fid, topic.Tid); html = html.ToFormat(toUserName, fromUserName, topic.Title, DateTime.Now, url); var title = PubMethod.RemoveAllSpace(fromUserName + "回复了:" + StringSugar.ToCutString(topic.Title, 10, "...")); ms.Send(PubGet.GetEmailUserName, PubConst.SiteMailUserName, toMail, title, html); System.Threading.Thread.Sleep(100); } } html = oldHtml; //处理@ if (p.Message.IsValuable() && p.Message.Contains("@")) { var adUserIds = db.Queryable <BBS_Posts>().Where(it => it.Tid == tid && it.Parentid > 0).Select(it => it.Posterid).ToList(); var adUsers = db.Queryable <UserInfo>().In(adUserIds).ToList(); var matchUsers = Regex.Matches(p.Message, @"\<span style\=""color:#4f99cf""\>@(.+?)\<\/span\>"); if (matchUsers != null && matchUsers.Count > 0) { var userNames = matchUsers.Cast <Match>().Select(it => it.Groups[1].Value).ToList(); adUsers = adUsers.Where(it => userNames.Contains(it.NickName)).ToList(); foreach (var item in adUsers) { if (item.Email.IsValuable() && item.Id != currentUser.Id) { string toUserName = item.NickName; string fromUserName = currentUser.NickName; string toMail = item.Email; MailSmtp ms = new MailSmtp(PubGet.GetEmailSmtp, PubGet.GetEmailUserName, PubGet.GetEmailPassword); string url = RequestInfo.HttpDomain + "/Ask/{0}/{1}#btnSubmit".ToFormat(topic.Fid, topic.Tid); html = html.ToFormat(toUserName, fromUserName, p.Message, DateTime.Now, url); var title = PubMethod.RemoveAllSpace(fromUserName + "在【" + topic.Title.TryToString().Trim() + "】@了你"); ms.Send(PubGet.GetEmailUserName, PubConst.SiteMailUserName, toMail, title, html); System.Threading.Thread.Sleep(100); } } } } }
public JsonResult ActivateMailSend(string key, string userId, string mail) //命名反的误导黑客 { Check.Exception(mail.IsEamil().IsFalse(), "参数不合法"); var userIdInt = EncryptSugar.GetInstance().Decrypto(key).ObjToInt(); var date = EncryptSugar.GetInstance().Decrypto(userId).ObjToDate(); var model = new ResultModel <string>(); if (base.IsLogin == false) { model.ResultInfo = "登录超时请刷新页面重新登录"; return(Json(model, JsonRequestBehavior.AllowGet)); } var cm = CacheManager <DateTime> .GetInstance(); string mailTimeKey = PubConst.SessionMailTime + key.ToLower(); if (cm.ContainsKey(mailTimeKey)) { var mins = (DateTime.Now - cm[mailTimeKey]).TotalSeconds; if (mins < 60) { model.ResultInfo = "您刚才已经发送成功,如果还没有收到邮件,请等待{0}秒后重新发送。".ToFormat(Convert.ToUInt32(60 - mins)); return(Json(model, JsonRequestBehavior.AllowGet)); } } _service.Command <HomeOutsourcing>((db, o) => { var isAnyUser = db.Queryable <UserInfo>().Any(it => userIdInt == it.Id); var isOkDate = ((DateTime.Now - date).TotalDays <= 3); var isAnyMail = db.Queryable <UserInfo>().Any(it => mail.ToLower() == it.Email); if (isAnyUser && isOkDate && isAnyMail.IsFalse()) { var html = FileSugar.FileToString(FileSugar.GetMapPath("~/Template/mail/Validate.html")).Replace('\r', ' ').Replace('\n', ' '); string userName = _userInfo.NickName; string aHtml = "<a href=\"{0}\">{1}</a>".ToFormat(RequestInfo.HttpDomain + "" + Url.Action("ActivateMailSubmitSuccess", "UserCenter", new { key = key, userId = userId, mail }), "请点击这儿完成激活"); string dateString = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); MailSmtp ms = new MailSmtp(PubGet.GetEmailSmtp, PubGet.GetEmailUserName, PubGet.GetEmailPassword); html = html.ToFormat(userName, aHtml, dateString); ms.Send(PubGet.GetEmailUserName, PubConst.SiteMailUserName, mail, userName + "邮箱激活通知", html); model.ResultInfo = "发送成功,请打开邮箱完成激活!"; string uniqueKey = PubGet.GetUserKey; base.AddUpdateMailCache(uniqueKey); model.IsSuccess = true; Check.Exception(ms.Result.IsValuable(), "邮件激活失败!" + ms.Result); cm.Add(mailTimeKey, DateTime.Now, cm.Minutes); } else if (isAnyMail) { model.ResultInfo = "发送失败,该邮箱已经被激活,或已经存在。"; } else { model.ResultInfo = "发送失败"; } }); return(Json(model, JsonRequestBehavior.AllowGet)); }
protected void Page_Load(object sender, EventArgs e) { MailSmtp ms = new MailSmtp("smtp.qq.com", "1215247044", "xx"); //可选参数 ms.SetCC("*****@*****.**"); //抄送可以多个 ms.SetBC("*****@*****.**"); //暗送可以多个 ms.SetIsHtml(true); //默认:true ms.SetEncoding(System.Text.Encoding.UTF8); //设置格式 默认utf-8 ms.SetIsSSL(true); //是否ssl加密 默认为false //调用函数 bool isSuccess = ms.Send("*****@*****.**", "test", "*****@*****.**", "哈哈", "哈哈"); //输出结果 Response.Write(ms.Result); }