title: Setting up email settings in appsettings web.config
body: <p>Setting up email details in web.config - but no email is being sent!</p>
  
  <pre class="lang-xml prettyprint-override"><code>  &lt;appSettings&gt;
      &lt;add key="webpages:Version" value="1.0.0.0" /&gt;
      &lt;add key="ClientValidationEnabled" value="true" /&gt;
      &lt;add key="UnobtrusiveJavaScriptEnabled" value="true" /&gt;
      &lt;add key="smtpServer" value="smtp.live.com" /&gt;
      &lt;add key="EnableSsl" value = "true"/&gt;
      &lt;add key="smtpPort" value="587" /&gt;
      &lt;add key="smtpUser" value="MyEmail@live.co.uk" /&gt;
      &lt;add key="smtpPass" value="mypasswordgoeshere" /&gt;
      &lt;add key="adminEmail" value="no-reply@no-reply.com" /&gt;
    &lt;/appSettings&gt;
  </code></pre>
  
  <p>I am using the following class</p>
  
  <pre><code>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
        {
          // Create our message
          mm = new MailMessage(from, to, subject, body);
          mm.DeliveryNotificationOptions =
          DeliveryNotificationOptions.OnFailure;
          // Send it
          Client.Send(mm);
          isSent = true;
        }
        // Catch any errors, these should be logged and
        // dealt with later
        catch (Exception ex)
        {
          // If you wish to log email errors,
          // add it here...
          var exMsg = ex.Message;
        }
        finally
        {
          mm.Dispose();
        }
        return isSent;
      }
  
  
      public static bool SendWelcome(string email)
      {
        string body = "Put welcome email content here...";
        return SendMessage(
          ConfigurationManager.AppSettings["adminEmail"],
            email, "Welcome message", body);
      }
    }
  }
  </code></pre>
  
  <p>This is the Account controller:</p>
  
  <pre><code>[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);   
  }
  </code></pre>
  
  <p>Are the app settings in the web.config right for the enableSsl?
  any advice welcome</p>