Search code examples
asp.net-mvcvb.netasp.net-mvc-3actionmailer.net

Sending Emails with ActionMailer.Mvc in VB, Cannot Find View


The error I get when I try to send an email is:

NoViewsFoundException

You must provide a view for this email. Views should be named ~/Views/Email/VerificationEmail.html.vbhtml.txt.cshtml or ~/Views/Email/VerificationEmail.html.vbhtml.html.cshtml (or aspx for WebFormsViewEngine) depending on the format you wish to render.

Error on line:

Return Email("~/Views/Email/VerificationEmail.html.vbhtml", model)

Can emails not be sent in .vbhtml, must they be sent in .cshtml? How can this work for VB?

Here is my code controller:

Imports ActionMailer.Net.Mvc

Public Class EmailController
    Inherits MailerBase

    Public Function VerificationEmail(ByVal model As RegisterModel) As EmailResult

        [To].Add(model.Email)
        From = "[email protected]"
        Subject = "Thanks for registering with us!"
        Return Email("~/Views/Email/VerificationEmail.html.vbhtml", model)

    End Function

End Class

Here is my view:

@modelType MyBlog.RegisterModel

@Code
    Layout = Nothing
End code

Welcome to My Cool Site, @Model.UserName

We need you to verify your email.  Click this nifty link to get verified!

@Html.ActionLink("Verify", "Account", New With {.code = Model.Email})

Thanks!

Solution

  • Of course you can have vbhtml email templates you just need to be careful with the naming (the .cshtmls exception message are hardcoded so don't be confused on it)

    Your view is named correctly as VerificationEmail.html.vbhtml you just need remove all the prefixes from the view name in the Email call:

    Return Email("VerificationEmail", model)
    

    Because ActionMailer will be automatically add the prefixes and select the correct template for you.

    Note that currently you cannot use relative viewnames like which start with ~ e.g. "~/Views/..." (I don't know wether this is a bug or feature).

    So you need put your mail template to the regular view folders e.g.

    • /Views/{MailControllerName}/
    • /View/Shared/