public ActionResult DoOrder(gModels.User people,string paymentmethod,long shipfee) { gModels.Cart cart = null; if (Session["Cart"] != null) { cart = (gModels.Cart)Session["Cart"]; } else { return Json(new { success = false }, JsonRequestBehavior.AllowGet); } long Idcustomer = 0; if (User.Identity.IsAuthenticated) { var u = _userService.GetUserByEmail(User.Identity.Name); if (u == null) return Json(new { success = false, msg = "Not login" }, JsonRequestBehavior.AllowGet); Idcustomer = u.ID; } else { var customer = sc.findCustomer(people.Email, people.Phone); if (customer == null) { Idcustomer = sc.NewCustomer(people, Organize.ID); } else { Idcustomer = customer.ID; } } string error = ""; int result = sc.AddOrder(cart, Idcustomer, people, paymentmethod, shipfee); if (result > 0) { var mailconfig = sc.getOneObject("mailconfig", Organize.ID); //send mail to mailconfig var config = mailconfig.Attrs.ToDictionary(m => m.AttrName, m => m.AttrValue); try { var template = sc.getOneObject("mailtemplate", Organize.ID); var SmtpServer = new SmtpClient(config["host"]); SmtpServer.Port = int.Parse(config["port"]); SmtpServer.Credentials = new System.Net.NetworkCredential(config["user"], config["password"]); SmtpServer.EnableSsl = true; //get email template var dnow = DateTime.Now; var templateconfig = template.Attrs.ToDictionary(m => m.AttrName, m => m.AttrValue); var emailcontent = templateconfig["mailcontent"]; emailcontent = emailcontent.Replace("_TOTALAMOUNT_", String.Format("{0:0,0 vnđ}", cart.TotalAmount)); emailcontent = emailcontent.Replace("_TIME_", dnow.ToString("hh:mm:ss dd/MM/yyyy")); emailcontent = emailcontent.Replace("_NAME_", people.FullName); int i = 1; string cartstr = "<table cellspacing='0' cellpadding='10' border='1' style='width:100%'><tr style='background:#038738;color:#fff'><td>#</td><td>Sản phẩm</td><td>Số lượng</td><td>Đơn giá</td><td>Thành tiền</td></tr>"; foreach (var citem in cart.List) { cartstr += "<tr>"; cartstr += "<td style='text-align:center'>"; cartstr += i.ToString(); cartstr += "</td>"; cartstr += "<td>"; if (!string.IsNullOrEmpty(citem.Object.gImage)) { string imglink = citem.Object.gImage; cartstr += "<img style='width:50px;height:50px;float:left;margin-right:10px' src='" + imglink + "' alt='" + citem.Object.Title + "'/>"; } cartstr += "<span style='float:left;padding-top:10px;font-weight:bold'>" + citem.Object.Title + "</span>"; cartstr += "</td>"; cartstr += "<td style='text-align:center'>"; cartstr += citem.Quantity; cartstr += "</td>"; cartstr += "<td>"; cartstr += String.Format("{0:0,0 }",1); cartstr += "</td>"; cartstr += "<td>"; cartstr += String.Format("{0:0,0 }", citem.Pricing); cartstr += "</td>"; cartstr += "</tr>"; i += 1; } cartstr += "<tr>"; cartstr += "<td colspan='4'></td><td>"; cartstr += "<span style='color:green;font-size:18px;font-weight:bold;'>" + String.Format("{0:0,0 }", cart.TotalAmount) + "</span> VND"; cartstr += "</td>"; cartstr += "</tr>"; cartstr += "</table>"; emailcontent = emailcontent.Replace("_CART_", cartstr); MatchCollection matchCollection = Regex.Matches(emailcontent, "<img.+?src=[\"'](.+?)[\"'].*?>", RegexOptions.IgnoreCase); foreach (Match match in matchCollection) { string value = match.Groups[1].Value; emailcontent = emailcontent.Replace(value, HttpUtility.UrlDecode(value)); } string customerstr = "<table cellspacing='0' cellpadding='10' border='1'><tr style='background:#038738;color:#fff'><td>Họ và tên</td><td>Số điện thoại</td><td>Email</td><td>Địa chỉ</td></tr><tr><td>" + people.FullName + "</td><td>" + people.Phone + "</td><td>" + people.Email + "</td><td>" + people.Address + "</td></tr><tr><td>Ghi chú</td><td colspan='3'></td></tr></table>"; emailcontent = emailcontent.Replace("_CUSTOMER_", customerstr); emailcontent = emailcontent.Replace("_LINKCHECKORDER_", "<a href='" + Request.Url.GetLeftPart(UriPartial.Authority) + "/kiem-tra-don-hang?email=" + HttpUtility.UrlEncode(people.Email) + "&phone=" + HttpUtility.UrlEncode(people.Phone) + "' target='_blank'>Kiểm tra đơn hàng</a>"); try { var email = new GuMail(); email.mSmtp = SmtpServer; email.From = config["user"]; email.Title = templateconfig["mailtitle"] + " - " + dnow.ToString("hh:mm:ss dd/MM/yyyy"); email.ContentText = templateconfig["shortdescription"]; email.ContentHtml = emailcontent; email.To = people.Email; email.Send(); } catch (Exception er) { error += "Không thể gửi mail tới email của bạn" + er.Message; } try { var email = new GuMail(); email.mSmtp = SmtpServer; email.From = config["user"]; email.Title = templateconfig["mailtitle"] + " - " + dnow.ToString("hh:mm:ss dd/MM/yyyy"); email.ContentText = templateconfig["shortdescription"]; email.ContentHtml = emailcontent; email.To = config["notifyemail"]; email.Send(); } catch (Exception err) { error += "\n Không thể gửi mail tới email của bạn" + err.Message; } } catch { } // cart = new Cart(); Session["Cart"] = cart; } return Json(new { success = true, data = result, error = error }, JsonRequestBehavior.AllowGet); }
//send mail public ActionResult SendMail(SendMail model) { var mailconfig = sc.getOneObject("mailconfig", Organize.ID); if (mailconfig == null) return Json(new { success = false, msg = "You should config mail sender first." }, JsonRequestBehavior.AllowGet); var config = mailconfig.Attrs.ToDictionary(m => m.AttrName, m => m.AttrValue); try { var template = sc.findObject("mailtemplate", model.TemplateID, Organize.ID); if (template == null) return Json(new { success = false, msg = "Template not found" }, JsonRequestBehavior.AllowGet); var SmtpServer = new SmtpClient(config["host"]); SmtpServer.Port = int.Parse(config["port"]); SmtpServer.Credentials = new System.Net.NetworkCredential(config["user"], config["password"]); SmtpServer.EnableSsl = true; List<Receiver> listerror = new List<Receiver>(); List<gModels.gObjectAttr> listAttr = new List<gObjectAttr>(); string recevers = ""; foreach (var receiver in model.Receivers) { try { var email = new GuMail(); email.mSmtp = SmtpServer; email.From = config["user"]; //get email template var templateconfig = template.Attrs.ToDictionary(m => m.AttrName, m => m.AttrValue); email.Title = template.Title; email.ContentText = templateconfig["shortdescription"]; email.ContentHtml = templateconfig["mailcontent"]; email.To = receiver.text; email.Send(); if (recevers != "") recevers += "," + receiver.text; else recevers += receiver.text; } catch { listerror.Add(receiver); } } listAttr.Add(new gModels.gObjectAttr() { ID = 9, AttrValue = recevers, gObjectGroupID = 3, Status = 1, CreateDate = DateTime.Now, AttrType = 17 }); listAttr.Add(new gModels.gObjectAttr() { ID = 11, AttrValue = JsonConvert.SerializeObject(template), gObjectGroupID = 3, Status = 1, CreateDate = DateTime.Now, AttrType = 4 }); //hh sc.UpdateOneModel(new gObject() { Title = template.Title, CreateDate = DateTime.Now, Status = 1, gGroupID = 3, Slug = template.Slug, Attrs = listAttr }, Organize.ID); return Json(new { success = true, error = listerror }, JsonRequestBehavior.AllowGet); } catch (Exception ex) { return Json(new { success = false, mgs = ex.Message }, JsonRequestBehavior.AllowGet); } }