In this article I will explain a simple tutorial with an example, how to send email in ASP.Net MVC Razor.
This example will illustrate how to send email in ASP.Net MVC using GMAIL SMTP server.
Note: For beginners in using ADO.Net with ASP.Net MVC, please refer my article ASP.Net MVC: ADO.Net Tutorial with example.
 
 
Namespaces
You will need to import the following namespaces.
using System.IO;
using System.Net;
using System.Net.Mail;
 
 
Model
The Model class consists of the following properties which will be used to capture the values posted from the Form.
public class MessageModel
{
    public string To { get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
}
 
 
Controller
The Controller consists of two Action methods.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
 
Action method for handling POST operation
This Action method gets called when the Form is posted. The values of the Recipient email address (to), the Sender email address (from), Subject and Body are fetched from their respective fields in the MessageModel class object.
Then all these values are set into an object of the MailMessage class.
The email attachments are available in the postedFiles parameter. Since the HTML5 FileUpload element allows multiple files to be selected, user can select multiple files and all the selected files are added as Attachment to the List of Attachments of the MailMessage class object.
For attaching a File as attachment to the email, one has to select the File to be send as attachment using HTML FileUpload element.
Note:  You will notice that the File is directly added as attachment without saving it on disk, this is possible since the file data is extracted from the InputStream property which belongs to the type System.IO.Stream. The second parameter supplied is the name of the File which is extracted from the FileName property.
 
Then an object of SmtpClient class is created and the settings of the GMAIL SMTP server are set into it.
Note: It is necessary to use the sender’s email address credentials while defining the Gmail SMTP Server Credentials as Gmail the sender’s email address must be equal to Gmail Username specified in credentials.
 
Finally the email is sent using the Send function of the SmtpClient class object.
MailMessage Class Properties
Following are the required properties of the MailMessage class.
From – Sender’s email address.
To – Recipient(s) Email Address.
CC – Carbon Copies (if any).
BCC – Blind Carbon Copies (if any).
Subject – Subject of the Email.
Body – Body of the Email.
IsBodyHtml – Specify whether body contains text or HTML mark up.
Attachments – Attachments (if any).
ReplyTo – ReplyTo Email address.
 
SmptClient Class Properties
Following are the properties of the SmtpClient class.
Host – SMTP Server URL (Gmail: smtp.gmail.com).
EnableSsl – Specify whether your host accepts SSL Connections (Gmail: True).
UseDefaultCredentials – Set to True in order to allow authentication based on the Credentials of the Account used to send emails.
Credentials – Valid login credentials for the SMTP server (Gmail: email address and password).
Port – Port Number of the SMTP server (Gmail: 587)
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        return View();
    }
 
    [HttpPost]
    public ActionResult Index(MessageModel model, List<HttpPostedFileBase> attachments)
    {
        using (MailMessage mm = new MailMessage(model.Email, model.To))
        {
            mm.Subject = model.Subject;
            mm.Body = model.Body;
            foreach (HttpPostedFileBase attachment in attachments)
            {
                if (attachment != null)
                {
                    string fileName = Path.GetFileName(attachment.FileName);
                    mm.Attachments.Add(new Attachment(attachment.InputStream, fileName));
                }
            }
            mm.IsBodyHtml = false;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            NetworkCredential NetworkCred = new NetworkCredential(model.Email, model.Password);
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = 587;
            smtp.Send(mm);
            ViewBag.Message = "Email sent.";
        }
 
        return View();
    }
}
 
 
View
Inside the View, in the very first line the MessageModel class is declared as Model for the View.
The View consists of an HTML Form which has been created using the Html.BeginForm method with the following parameters.
ActionNameName of the Action. In this case the name is Index.
ControllerName – Name of the Controller. In this case the name is Home.
FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
HtmlAttributes – This array allows to specify the additional Form Attributes. Here we need to specify enctype = “multipart/form-data” which is necessary for uploading Files.
Inside the Form, the TextBox, TextArea, Password and FileUpload elements which are used to capture the details of the Email to be sent.
There’s also a Submit which when clicked the Form gets submitted and the Email is sent.
After successful sending of the Email, a success message is displayed in JavaScript Alert Message Box using ViewBag object.
@model Send_Email_Attachment_MVC.Models.MessageModel
 
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <table border="0" cellpadding="0" cellspacing="0">
            <tr>
                <td style="width: 80px">To:</td>
                <td>@Html.TextBoxFor(m => m.To)</td>
            </tr>
            <tr>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td>Subject:</td>
                <td>@Html.TextBoxFor(m => m.Subject)</td>
            </tr>
            <tr>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td valign="top">Body:</td>
                <td>@Html.TextAreaFor(m => m.Body, new { @rows = 10, @cols = 50 })</td>
            </tr>
            <tr>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td>File Attachment:</td>
                <td><input type="file" name="Attachments" multiple="multiple"/></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td>Gmail Email:</td>
                <td>@Html.TextBoxFor(m => m.Email)</td>
            </tr>
            <tr>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td>Gmail Password:</td>
                <td>@Html.PasswordFor(m => m.Password)</td>
            </tr>
            <tr>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="Send"/></td>
            </tr>
        </table>
    }
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        var message = "@ViewBag.Message";
        $(function () {
            if (message != "") {
                alert(message);
            }
        });
    </script>
</body>
</html>
 
 
Screenshots
The Form displaying Success message
Send email in ASP.Net MVC
 
The received email
Send email in ASP.Net MVC
 
 
Downloads