0

我在我的 ASP MVC5 项目中使用 Postal.MVC5。最初我在 web.config 中有我的 smtp 配置,所以这段代码工作得很好并且能够成功发送电子邮件。

public async Task<ActionResult>SendEmail(EmployeeViewModel emailModel)
{
  string _errorMessage;

  emailModel.To = "john@outlook.com";
  emailModel.Subject = "Test Subject";
  emailModel.Message = "Test Message";

  await emailModel.SendAsync();

  return Json(new {ErrorMessage = _errorMessage});
}

现在,我需要在运行时设置 Smtpclient 并按照此 stackoverflow 帖子中显示的步骤操作:How to Set SMTP Client in POSTAL MVC not in WEB.Config

public async Task<ActionResult>SendEmail(EmployeeEmailViewModel emailModel)
{
  string _errorMessage;

  emailModel.To = "john@outlook.com";
  emailModel.Subject = "Test Subject";
  emailModel.Message = "Test Message";

  //new code
  SmtpClient client = new SmtpClient("smtp.test.com");
  client.UseDefaultCredentials = false;
  client.Credentials = new NetworkCredential("secretId", "notSecretPassword");
  client.DeliveryMethod = SmtpDeliveryMethod.Network;
  client.Port = 111;
  client.EnableSsl = true;

  Postal.EmailService emailService = new EmailService(new ViewEngineCollection(), () => client);
  await emailService.SendAsync(emailModel);

  return Json(new {ErrorMessage = _errorMessage});
}

现在这段代码会抛出类似这样的错误: System.Exception: 找不到 EmployeeEmailViewModel 的电子邮件视图。搜索位置:在 Postal.EmailViewRenderer.CreateView(String viewName, ControllerContext controllerContext) 在 Postal.EmailViewRenderer.Render(Email email, String viewName)

我在 ~/Views/Emails/EmployeeEmailViewModel.cshtml 中有“EmployeeEmailViewModel”视图。

知道为什么我会收到此错误消息或如何解决它吗?谢谢桑吉夫

4

0 回答 0