5

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

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

Controller:

public virtual MvcMailMessage Welcome()
{
    //ViewBag.Data = someObject;
    return Populate(x => {
              x.ViewName = "Welcome";
              x.To.Add("some-email@example.com");
              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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
woggles
  • 7,444
  • 12
  • 70
  • 130

1 Answers1

10

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

SmtpClient client = new SmtpClient("mail.example.com", 995);
SmtpClientWrapper wrapper = new SmtpClientWrapper(client);
jao
  • 18,273
  • 15
  • 63
  • 96
  • I still an error that the SMTP host wasn't specified when doing this...I think I need to find a way to assign the wrapper to MVCMailer. – woggles Jan 07 '13 at 06:47
  • 2
    Ah ok I figured this out - you need to pass the wrapper to the Send method. Thanks :) – woggles Jan 07 '13 at 06:59