Setting up email in web.config - but email is not sent!
<appSettings> <add key="webpages:Version" value="1.0.0.0" /> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> <add key="smtpServer" value="smtp.live.com" /> <add key="EnableSsl" value = "true"/> <add key="smtpPort" value="587" /> <add key="smtpUser" value=" MyEmail@live.co.uk " /> <add key="smtpPass" value="mypasswordgoeshere" /> <add key="adminEmail" value=" no-reply@no-reply.com " /> </appSettings>
I use the following class
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Net.Mail; using System.Net; using System.Configuration; namespace MVCcars.Utils { public static class MailClient { private static readonly SmtpClient Client; static MailClient() { Client = new SmtpClient { Host = ConfigurationManager.AppSettings["SmtpServer"], Port = Convert.ToInt32( ConfigurationManager.AppSettings["SmtpPort"]), DeliveryMethod = SmtpDeliveryMethod.Network }; Client.UseDefaultCredentials = false; Client.Credentials = new NetworkCredential( ConfigurationManager.AppSettings["SmtpUser"], ConfigurationManager.AppSettings["SmtpPass"]); } private static bool SendMessage(string from, string to, string subject, string body) { MailMessage mm = null; bool isSent = false; try {
This is the account controller:
[HttpPost] public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { // Attempt to register the user MembershipCreateStatus createStatus; Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus); if (createStatus == MembershipCreateStatus.Success) { // Send welcome email MailClient.SendWelcome(model.Email); FormsAuthentication.SetAuthCookie( model.UserName, false /* createPersistentCookie */); return RedirectToAction("create", "Customer"); } else { ModelState.AddModelError("", ErrorCodeToString(createStatus)); } } // If we got this far, something failed, // redisplay form return View(model); }
Are application settings in web.config right for enableSsl? any advice welcome
source share