MVCMailer smtp mailsettings以编程方式设置配置设置

时间:2013-01-04 12:53:27

标签: c# asp.net asp.net-mvc-3 smtpclient mvcmailer

MVCMailer使用web.config文件中的smtp设置,如下所示:

<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>

控制器:

public virtual MvcMailMessage Welcome()
{
    //ViewBag.Data = someObject;
    return Populate(x => {
              x.ViewName = "Welcome";
              x.To.Add("some-email@example.com");
              x.Subject = "Welcome";
        });
}

无论如何都要在代码中设置SMTP设置?我想避免在web.config文件中保存密码。

1 个答案:

答案 0 :(得分:10)

使用具有所需属性的SmtpClient调用SmtpClientWrapper

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