Search code examples
c#asp.netasp.net-mvc-3smtpclientmvcmailer

MVCMailer smtp mailsettings set configuration settings programmatically


MVCMailer uses the smtp settings from the web.config file as follows:

<system.net>
<mailSettings>
      <smtp from="[email protected]">
        <network enableSsl="true" host="smtp.gmail.com" port="587" userName="[email protected]" password="valid-password" />
      </smtp>
</mailSettings>
</system.net>

Controller:

public virtual MvcMailMessage Welcome()
{
    //ViewBag.Data = someObject;
    return Populate(x => {
              x.ViewName = "Welcome";
              x.To.Add("[email protected]");
              x.Subject = "Welcome";
        });
}

Is there anyway to set the SMTP settings in code? I want to avoid saving the password in the web.config file.


Solution

  • Call SmtpClientWrapper with a SmtpClient that has the properties you need.

    SmtpClient client = new SmtpClient("mail.example.com", 995);
    SmtpClientWrapper wrapper = new SmtpClientWrapper(client);