1

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 = "me@my.org"
        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!
user1477388
  • 20,790
  • 32
  • 144
  • 264

3 Answers3

4

After reading a couple of issues and answer, it could get it to work with this:

public override string ViewPath {
        get { return AppDomain.CurrentDomain.BaseDirectory + @"\EmailTemplates\"; }
    }
carlosJ
  • 133
  • 2
  • 8
2

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/
nemesv
  • 138,284
  • 16
  • 416
  • 359
  • I changed to your code but I still get the error: `You must provide a view for this email. Views should be named ~/Views/Email/VerificationEmail.txt.cshtml or ~/Views/Email/VerificationEmail.html.cshtml (or aspx for WebFormsViewEngine) depending on the format you wish to render.` – user1477388 Aug 20 '12 at 16:19
  • Can you help with this or do I have to delete and repost with more info? Thanks. – user1477388 Aug 20 '12 at 17:45
  • 1
    @user1477388 I've managed to repro your issue, something is definitely wrong in ActionMailer... there is no need to delete your question it's fine. I will update my answer if I find something. – nemesv Aug 20 '12 at 17:52
  • Thank you. I hope we can find a solution soon. Do you have any suggestions to finding a solution faster, for instance, should I start a bounty? – user1477388 Aug 20 '12 at 17:53
  • 1
    I've update my answer basically you need to write `Return Email("VerificationEmail", model)` without specifying the path, because viewnames with paths currently not supported even with `cshtmls` – nemesv Aug 20 '12 at 18:03
  • I think that will work. It's asking me to find `c:\Dev\actionmailer\src\ActionMailer.Net\SmtpMailSender.cs`. I was wondering which file do I put the ` ` in? Is it `~/web.config` or `~/views/web.config`? Thanks. – user1477388 Aug 20 '12 at 18:12
  • 1
    the smtp config setting should go to the `~/web.config` – nemesv Aug 20 '12 at 18:14
  • I added it, but it just says, "Failure sending mail" now. I will probably have to open a new question. – user1477388 Aug 20 '12 at 18:17
  • I just had to set the proper folder, it doesn't send mail (not sure if it's supposed to) it just creates a file in the specified location. I assume it will send when I change to SMTP. – user1477388 Aug 20 '12 at 18:21
  • 1
    Hi there. I'm assuming you've resolved your problem. ActionMailer for MVC relies on the Views being in the standard location for ASP.NET MVC, and it also relies on you specifying the right view name without any file extension (it will figure out the right extension on its own). – Scott Arrington Aug 21 '12 at 14:54
  • I am having this issue as well where ActionMailer can't find my view. I suspect it is because the initial controller is in one area, then it calls the ActionMailer controller, which is in another area. Is this possible to use a view in a different area than the original calling controller? – crichavin Apr 10 '13 at 03:16
1

Had the same issue as Chad Richardson. To solve the issue which happens when trying to send email from other area just add this code to Application_Start method:

var razorEngine = ViewEngines.Engines.OfType<RazorViewEngine>().First();
    razorEngine.ViewLocationFormats = razorEngine.ViewLocationFormats.Concat(new string[] 
    { 
        "~/Areas/.../{0}.cshtml"
    }).ToArray();
kleopatra
  • 51,061
  • 28
  • 99
  • 211
Ivan M
  • 11
  • 1