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文件中保存密码。
答案 0 :(得分:10)
使用具有所需属性的SmtpClient调用SmtpClientWrapper
。
SmtpClient client = new SmtpClient("mail.example.com", 995);
SmtpClientWrapper wrapper = new SmtpClientWrapper(client);