0

I am using Postal.MVC5 in my ASP MVC5 project. Initially i had my smtp configuration in web.config so this piece of code was working just fine and was able to send email successfully.

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});
}

Now, i need to setup Smtpclient during runtime and followed steps shown in this stackoverflow post: 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});
}

Now this code throws an error something like this: System.Exception: Email view not found for EmployeeEmailViewModel. Locations searched: at Postal.EmailViewRenderer.CreateView(String viewName, ControllerContext controllerContext) at Postal.EmailViewRenderer.Render(Email email, String viewName)

I do have 'EmployeeEmailViewModel' view in ~/Views/Emails/EmployeeEmailViewModel.cshtml .

Any idea why i am getting this error message or how i can resolve it? Thanks Sanjeev

sanjeev
  • 765
  • 1
  • 7
  • 15
  • Instead of editing your question to provide an answer, you should submit the solution as an actual answer. – Sam Axe Nov 02 '18 at 19:03
  • @SamAxe you right, i will fix that. – sanjeev Nov 02 '18 at 19:37
  • Anyone who run into this issue, this is how i resolved it.** Postal.EmailService emailService = new EmailService(ViewEngines.Engines,() => client); In Web.config, leave from value in smtp attribute within If someone knows how to set MailMessage from value from code behind, then we don't need to do this on web.config. – sanjeev Nov 02 '18 at 19:37

0 Answers0