public virtual void SendEmail(MailData Data) { if (String.IsNullOrEmpty(Data.To)) { foreach (string mail in configuration.GetAdminEmail()) { using (var message = new MailMessage("*****@*****.**",mail) { Subject = Data.Subject, Body = Data.Message, IsBodyHtml = true }) { SmtpClient smtp = new SmtpClient(); smtp.Port = 587; smtp.Host = "smtp.gmail.com"; smtp.EnableSsl = true; smtp.Timeout = 10000; smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.UseDefaultCredentials = false; smtp.Credentials = new System.Net.NetworkCredential("*****@*****.**", "meniu123"); smtp.Send(message); } } } else { using (var message = new MailMessage("*****@*****.**",Data.To) { Subject = Data.Subject, Body = Data.Message, IsBodyHtml = true }) { SmtpClient smtp = new SmtpClient(); smtp.Port = 587; smtp.Host = "smtp.gmail.com"; smtp.EnableSsl = true; smtp.Timeout = 10000; smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.UseDefaultCredentials = false; smtp.Credentials = new System.Net.NetworkCredential("*****@*****.**", "meniu123"); smtp.Send(message); } } }
public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { // Intento de registrar al usuario try { string token = WebSecurity.CreateUserAndAccount(model.UserName, model.Password, null, true); WebSecurity.Login(model.UserName, model.Password); //Change url on production string accountLink = String.Format("http://localhost:58585/usuarios/confirmar-email/{0}", token); var confirmationEmail = new MailData { To = model.UserName, Message = String.Format("¡Gracias por entrar a Meniu!, ¡Bienvenido! \nSigue el siguiente link para confirmar tu cuenta de Meniu \n {0} \n ", accountLink), Subject = "Confirmar Usuario Meniu" }; SendEmail(confirmationEmail); return RedirectToAction("Index", "Home"); } catch (MembershipCreateUserException e) { ModelState.AddModelError("", ErrorCodeToString(e.StatusCode)); } } // Si llegamos a este punto, es que se ha producido un error y volvemos a mostrar el formulario return View(model); }