public bool InsertEmail(tblEmail email) { if(email.Sender!="" && email.Email!="" && email.Title!="" && email.Content!="") return _da.InsertEmail(email); return false; }
partial void DeletetblEmail(tblEmail instance);
partial void UpdatetblEmail(tblEmail instance);
partial void InserttblEmail(tblEmail instance);
public ActionResult Send() { //Step 1: Insert to tblEmail bool isValid = true; tblEmail email = new tblEmail(); email.ID = Guid.NewGuid(); if (Request["sender"] != null && !String.IsNullOrEmpty(Request["sender"])) { email.Sender = Request["sender"].ToString(); } else { isValid = false; if (Request.Cookies["Culture"] != null && Request.Cookies["Culture"].Value == "en-US") { TempData["Sender"] = "Please enter Sender"; } else { TempData["Sender"] = "Cần nhập người gửi"; } } if (Request["email"] != null && !String.IsNullOrEmpty(Request["email"])) { email.Email = Request["email"].ToString(); if (!isEmail(email.Email)) { isValid = false; if (Request.Cookies["Culture"] != null && Request.Cookies["Culture"].Value == "en-US") { TempData["EmailInvalid"] = "Email is invalid"; } else { TempData["EmailInvalid"] = "Email không hợp lệ"; } } } else { isValid = false; if (Request.Cookies["Culture"] != null && Request.Cookies["Culture"].Value == "en-US") { TempData["EmailInvalid"] = "Email is invalid"; } else { TempData["EmailInvalid"] = "Email không hợp lệ"; } } email.SendDate = DateTime.Now; if (Request["department"] != null) { email.SendTo = byte.Parse(Request["department"].ToString()); } if (Request["title"] != null && !String.IsNullOrEmpty(Request["title"])) { email.Title = Request["title"].ToString(); } else { isValid = false; if (Request.Cookies["Culture"] != null && Request.Cookies["Culture"].Value == "en-US") { TempData["Title"] = "Please enter Title"; } else { TempData["Title"] = "Cần nhập tiêu đề"; } } if (Request["content"] != null && !String.IsNullOrEmpty(Request["content"])) { email.Content = Request["content"].ToString(); } else { isValid = false; if (Request.Cookies["Culture"] != null && Request.Cookies["Culture"].Value == "en-US") { TempData["Content"] = "Please enter Content"; } else { TempData["Content"] = "Cần nhập nội dung"; } } if (isValid && _nguyenHiepService.InsertEmail(email)) { //Step 2: Send Email List<tblUser> allUser = new List<tblUser>(); allUser = _nguyenHiepService.GetUserByDepartment(byte.Parse(Request["department"].ToString())); /*Hung implement sending mail by SMTP via google*/ #region Send mail //set up SMTP client SmtpClient smpt = new SmtpClient("smtp.gmail.com", 587); string mailFromAccount = "*****@*****.**"; string mailFromPass = "******"; smpt.UseDefaultCredentials = false; NetworkCredential cred = new NetworkCredential(mailFromAccount, mailFromPass); smpt.Credentials = cred; smpt.EnableSsl = true; smpt.Timeout = 20000; //Build up email message MailMessage mail = new MailMessage(); mail.From = new MailAddress("*****@*****.**", "Nguyen Hiep site Admin"); if (allUser != null && allUser.Count > 0) { foreach (tblUser user in allUser) { if (user.Email != null) { mail.To.Add(user.Email); } } } mail.Subject = email.Title; mail.Body = String.Format("<b>Customer Name:</b> {0} <br/><b>Date:</b> {1}<br/> <b>From email address:</b> {2}<br/> <b>Person in charge:</b> {3}<br/><br/> <b>Content detail:<b><br/><hr> {4}", email.Sender, email.SendDate, email.Email, Request["departmentname"].ToString(), email.Content); mail.IsBodyHtml = true; mail.BodyEncoding = Encoding.UTF8; #endregion Send mail try { if (mail.From != null && !String.IsNullOrEmpty(mail.From.Address) && mail.To != null && mail.To.ToList().Count > 0) { smpt.Send(mail); if (Request.Cookies["Culture"] != null && Request.Cookies["Culture"].Value == "en-US") { TempData["Message"] = "Send successful!"; } else { TempData["Message"] = "Gửi mail thành công!"; } email = new tblEmail(); } else { if (Request.Cookies["Culture"] != null && Request.Cookies["Culture"].Value == "en-US") { TempData["Message"] = "Send Unsuccessful!"; } else { TempData["Message"] = "Không gửi mail thành công!"; } } } catch(SmtpException ex) { if (Request.Cookies["Culture"] != null && Request.Cookies["Culture"].Value == "en-US") { TempData["Message"] = "Send Unsuccessful!"; } else { TempData["Message"] = "Không gửi mail thành công!"; } } } TempData["Department"] = LoadDataForDropDownList(); TempData["EmailReload"] = email; return RedirectToAction("ViewContact"); }
public bool InsertEmail(tblEmail email) { if (email != null && email.ID != null && !email.ID.Equals(Guid.Empty)) { _dataContext.tblEmails.InsertOnSubmit(email); _dataContext.SubmitChanges(); return true; } return false; }