ASP.NET MVC 3 Beta: Using WebMail helper to send e-mail

This far I have blogged about WebImage and Chart helpers. Now let’s see another new helper – WebMail – that you can easily use to send e-mails. WebMail is easy to configure and extremely easy to use. In this posting I will show you simple feedback form that uses WebMail to send feedback messages.

Feedback view

As a first thing let’s create view for feedback form. I am using Razor view engine in this posting. Feedback view is really simple. It contains fields that user must fill and validation message that is shown when something is wrong.

@model dynamic
    View.Title =
    Layout =

(Html.BeginForm()) {
        <td>Your e-mail:</td>
    <input type="submit" value="Send" />

To be honest I have used no validation in this example. Validation message for _FORM is used to show exceptions when something went wrong. It makes it easier for you to play with this example.

WebMail feedback form

Sending e-mail using WebMail

Before we start sending e-mail I show you two controller actions. There actions are used to show feedback form and page that is shown when e-mail is successfully sent.

public ActionResult

public ActionResult
return View();

I have shown no FeedbackSent view here. I am very sure you are able to create this simple view by yourselves. :)

Now let’s take a look at controller action that uses WebMail to send e-mail. Notice how I can easily use SmtpServer static property to set the address of SMTP server. There are more static properties that let you configure WebMail helper. In the case of exception the exception is shown on feedback form with stack trace. If e-mail is sent without errors then user is redirected to FeedbackSent view where he or she can read some bla-bla-bla about when answer is expectedly sent and so on.

public ActionResult Feedback(string email, string subject, string
WebMail.SmtpServer = "my.smtp.server"

return RedirectToAction("FeedbackSent"
catch (Exception
, ex.ToString());

return View();

And now let’s take a look what’s going on in my mailbox where I sent my test messages.

Messages appeared to mailbox

Mission completed – seems like I can go to sleep now. :)

By the way, if you don’t specify sender when calling WebMail.Send() method then e-mail is sent out from DoNotReply@localhost address.

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    14 thoughts on “ASP.NET MVC 3 Beta: Using WebMail helper to send e-mail

    • October 20, 2010 at 2:14 pm

      What’s the advantage of using that web helper compared to

      SmtpClient client = new SmtpClient();

      Seems to be little to gain with the helper or am I missing something?

    • October 20, 2010 at 5:42 pm

      Looks pretty useless to me. Move it to Futures please before we’re stuck with it forever and we have to teach every new comer not to use it.

    • October 20, 2010 at 6:53 pm

      This extension is useful when you are using WebMatrix and Razor. But you can also use it with ASP.NET MVC. There is no reason to afraid this simple class. Be more open minded, guys! :)

    • October 22, 2010 at 7:26 pm

      Yeah, Carl, right you are. Testability is also problem with WebImage helper because it wants image path as constructor parameter and it loads image immediately. WebMail is a little bit different but the problem solves the same way: we need some interface, wrapper class and fake class to make these helpers usable in the case of tested code.

      I don’t want to be sarcastic without reason but I have question: why MS releases legacy code in A.D. 2010?

    • September 5, 2011 at 10:53 am

      So, i wanna test them without needing to up load website to web host. So how do i do

    • September 10, 2011 at 2:56 pm

      How to attach file in MVC 3

    • September 22, 2011 at 1:45 am

      Great tutorial, not sure why anyone would see a problem with it??? All I wanted to do was send an email from a form in my MVC3 Razor Project and now I can. Thanks to the author, you are a champion!!!

    • September 22, 2011 at 1:54 am

      Sorry Gunnar, I just found you name on the site, right up the top left. I really need to open my eyes!

    • October 19, 2011 at 4:38 pm

      I finish this code, and it works fine in my computer. But i encounter a problem when i publish my website on internet. Server give a error is:

    • October 30, 2011 at 5:32 am

      Hi Kenny,

      Do you have the correct SmtpServer?



    • February 13, 2013 at 11:40 am

      I am on localhost, what SMTP should I use? Thanks

    • February 13, 2013 at 11:51 am

      You can install SMTP server that comes with IIS by example.

    Leave a Reply

    Your email address will not be published. Required fields are marked *