如何在主机www.mywindowshosting.com上使用mvcMailer发送邮件?
我使用以下代码:
UserMailer类:
public class UserMailer : MailerBase, IUserMailer
{
public UserMailer()
{
MasterName = "_Layout";
}
public MvcMailMessage ForgetPassword(EmailForgetPasswordViewModel forgetPasswordViewModel)
{
ViewData.Model = forgetPasswordViewModel;
return Populate(x =>
{
x.BodyTransferEncoding = System.Net.Mime.TransferEncoding.Base64;
x.BodyEncoding = Encoding.UTF8;
x.Subject = forgetPasswordViewModel.Subject;
x.ViewName = forgetPasswordViewModel.ViewName;
x.To.Add(forgetPasswordViewModel.To);
});
}
}
界面:
public interface IUserMailer
{
MvcMailMessage ForgetPassword(EmailForgetPasswordViewModel forgetPasswordViewModel);
}
行动:
[HttpPost]
[AllowAnonymous]
[MyValidateAntiForgeryToken]
public async Task<JsonResult> ForgotPassword(ForgetPasswordViewModel model)
{
if (ModelState.IsValid)
{
await _userMailer.ForgetPassword(new EmailForgetPasswordViewModel
{
To = model.Email,
Url = callbackUrl
}).SendAsync();
return SuccessSendMail();
}
return ModelState.GetListOfErrors();
}
输出:Request was sent successfully
。输出正确但不发送电子邮件。
Note
:代码在localhost
上正常运行,系统会发送电子邮件。但是当项目在www.mywindowshosting.com
上发布时,不会发送电子邮件但输出:Request was sent successfully
更新:
<smtp from="myMail@gmail.com">
<network enableSsl="true" host="smtp.gmail.com" port="587" userName="myMail@gmail.com" password="myPassword" />
</smtp>