public void ShouldSendEmailAsync() { MailMessage mail = new MailMessage(); mail.From = new MailAddress("*****@*****.**"); mail.To.Add("*****@*****.**"); mail.Subject = "Test Mail" + DateTime.Now.ToString("hh:mm:ss"); mail.Body = "This is for testing SMTP mail"; EMail eMail = new EMail(); var ret= eMail.SendEmailAsync(mail); // wait for all emails to signal // while(!ret.IsCompleted) {} }
public ActionResult SendEmail(EMailView model, string returnUrl) { if (ModelState.IsValid) { EMail eMail = new EMail(); try { //make sure to address is ok (as this set by system not user) Regex regex = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"); if (model.LogOnModel.Email == null) { ModelState.AddModelError("", "The 'To' address is blank"); } else { var match = regex.Match(model.LogOnModel.Email); if (match.Success) { string message = string.Format("<p>Message from {0}</p></br>{1}", model.EmailFrom, model.Message); eMail.SendEmailAsync(new MailMessage(EMail.UserName, model.LogOnModel.Email, "Do Not Reply - Art1st site contact from " + model.EmailFrom, message)); } else { //log message sent string message = string.Format("{0} \n from {1}", model.Message, model.EmailFrom); Logger.Error("Failed to send email to " + model.LogOnModel.UserName, null); EMail.SendEmailToAdministrator("Failed to send email to " + model.LogOnModel.UserName, message); } //do stuff if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\")) { return Redirect(returnUrl); } else { return RedirectToAction("Index", "Home"); } } } catch(FormatException ex) { ModelState.AddModelError("", "Please enter a valid email address"); } catch (Exception ex) { Logger.Error("Error in sendEmail", ex); ModelState.AddModelError("", ex.ToString()); } } // If we got this far, something failed, redisplay form return View(model); }
public ActionResult ForgottenPassword(LogOnModel logOnModel) { try { var currentUser = Membership.GetUser(logOnModel.UserName); if (currentUser != null) { var password = currentUser.ResetPassword(); var emailSender = new EMail(); var body = string.Format("Hi {0}!\r\nYour new password is {1}", currentUser.UserName, password); var mailMessage = new MailMessage("*****@*****.**", currentUser.Email, "ArtSite details", body); var result = emailSender.SendEmailAsync(mailMessage); return RedirectToAction("PassWordSent", new { emailAddress = currentUser.Email }); } else { ModelState.AddModelError("", string.Format("'{0}' is not registered", logOnModel.Email)); } } catch (Exception ex) { ModelState.AddModelError("", ex.ToString()); } return View(); }