0

I'm trying to use ASP MVC Postal in a background job to send emails as follows:

    public void CommentCreated(Comment comment, ApplicationUser user)
    {
        var viewsPath = Path.GetFullPath(HostingEnvironment.MapPath(@"~/Views/Emails"));
        var engines = new ViewEngineCollection();
        engines.Add(new FileSystemRazorViewEngine(viewsPath));

        var emailService = new Postal.EmailService(engines);

        var email = new CommentCreatedEmail
        {
            To = user.Email,
            From = ConfigurationManager.AppSettings["SmtpEMailFrom"],
            Subject = "Comment Created"
            Comment = comment,
            User = user

        };

        emailService.Send(email);

    }

My view CommentCreated.cshtml is as follows:

@model MyApp.Models.CommentCreatedEmail
To: @Model.To
From: @Model.From
Subject: @Model.Subject

<p>
<a href="@Url.Action("Details", "Comment", new { id = @Model.Comment.Id }, "http")">@Url.Action("Details", "Comment", new { id = @Model.Comment.Id }, "http")</a>

But I'm getting the following error:

Exception thrown: 'RazorEngine.Templating.TemplateCompilationException' in RazorEngine.dll
Exception thrown: 'System.Web.HttpCompileException' in System.Web.dll

Any ideas

adam78
  • 9,668
  • 24
  • 96
  • 207

3 Answers3

0

Put exception handling in to catch and identify where the error is occuring

public void CommentCreated(Comment comment, ApplicationUser user)
{
    try{
        var viewsPath = Path.GetFullPath(HostingEnvironment.MapPath(@"~/Views/Emails"));
        var engines = new ViewEngineCollection();
        engines.Add(new FileSystemRazorViewEngine(viewsPath));

        var emailService = new Postal.EmailService(engines);

        var email = new CommentCreatedEmail
        {
            To = user.Email,
            From = ConfigurationManager.AppSettings["SmtpEMailFrom"],
            Subject = "Comment Created"
            Comment = comment,
            User = user

        };

        emailService.Send(email);
    }
    catch(TemplateCompilationException ex)
    {
        foreach (var compilerError in ex.CompilerErrors)
        {
            Console.WriteLine(string.Format("{0} - {1} - Line {2} Column {3} in {4}", compilerError.ErrorNumber, compilerError.ErrorText, compilerError.Line, compilerError.Column, compilerError.FileName));
        }
    }
}
timkly
  • 793
  • 6
  • 14
0

Looks like this line :

Subject = "Comment Created"

is missing a comma (,) after it, shouldn't it be

Subject = "Comment Created",
Utkarsh Bais
  • 197
  • 8
0

Razor Engine have some limitations outside the MVC world, so, a lot of things related to it won't work at all.

I've run into this problem recently and my "solution" was to set the redirect base URL inside app config and combine it at run time, something like this:

var email = new CommentCreatedEmail
    {
        To = user.Email,
        From = ConfigurationManager.AppSettings["SmtpEMailFrom"],
        Subject = "Comment Created"
        Comment = comment,
        User = user
       CommentLink = ConfigurationManager.AppSettings["SiteUrl"] + "Comment/Details/"+comment.Id
    };

And in you .cshtml file:

@model MyApp.Models.CommentCreatedEmail
To: @Model.To
From: @Model.From
Subject: @Model.Subject

<p>
<a href="@Model.CommentLink">Comment</a>

Hope it helps!