Introduction
Sending email is a very common task in any web application for many purposes. In daily development we need to add some mail functionality to our project to send e-mail to the customer or another in our web site.
Using the code
For sending mail from ASP.NET MVC we use the "System.Net.Mail" namespace. Let's see how to do this.
- Open Visual Studio
- "File" -> "New" -> "Project..."
- Choose Visual C#- Web then select ASP.NET MVC4 Web Application
- Add a new Internet Application then click OK
Step 1
Create a new Model Class in the model folder.
The following is the code for the new Model.
MailModel.cs
- public class MailModel {
- public string From {
- get;
- set;
- }
- public string To {
- get;
- set;
- }
- public string Subject {
- get;
- set;
- }
- public string Body {
- get;
- set;
- }
- }
Step 2
Create a New SendMailerController in the Controller folder.
The following is the code for the design of the new Controller.
SendMailerController.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Mail;
- using System.Web;
- using System.Web.Mvc;
- namespace SendMail.Controllers {
- public class SendMailerController: Controller {
-
-
- public ActionResult Index() {
- return View();
- }
- [HttpPost]
- public ViewResult Index(SendMail.Models.MailModel _objModelMail) {
- if (ModelState.IsValid) {
- MailMessage mail = new MailMessage();
- mail.To.Add(_objModelMail.To);
- mail.From = new MailAddress(_objModelMail.From);
- mail.Subject = _objModelMail.Subject;
- string Body = _objModelMail.Body;
- mail.Body = Body;
- mail.IsBodyHtml = true;
- SmtpClient smtp = new SmtpClient();
- smtp.Host = "smtp.gmail.com";
- smtp.Port = 587;
- smtp.UseDefaultCredentials = false;
- smtp.Credentials = new System.Net.NetworkCredential("username", "password");
- smtp.EnableSsl = true;
- smtp.Send(mail);
- return View("Index", _objModelMail);
- } else {
- return View();
- }
- }
- }
- }
Index.cshtml
- @model SendMail.Models.MailModel
- @{
- ViewBag.Title = "Index";
- }
- <h2>Index</h2>
- <fieldset>
- <legend>
- Send Email
- </legend>
- @using (Html.BeginForm())
- {
- @Html.ValidationSummary()
- <p>From: </p>
- <p>@Html.TextBoxFor(m=>m.From)</p>
- <p>To: </p>
- <p>@Html.TextBoxFor(m=>m.To)</p>
- <p>Subject: </p>
- <p>@Html.TextBoxFor(m=>m.Subject)</p>
- <p>Body: </p>
- <p>@Html.TextAreaFor(m=>m.Body)</p>
- <input type ="submit" value ="Send" />
- } </fieldset>
In the code above we have the following 3 fields:
When the user clicks the "Send" button, the mail will be sent to the specified mail address that you provide in the To TextBox. So add the following code for the [HttpPost] Method for the send button click.
SendMailerController.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Mail;
- using System.Web;
- using System.Web.Mvc;
- namespace SendMail.Controllers {
- public class SendMailerController: Controller {
-
-
- public ActionResult Index() {
- return View();
- }
- [HttpPost]
- public ViewResult Index(SendMail.Models.MailModel _objModelMail) {
- if (ModelState.IsValid) {
- MailMessage mail = new MailMessage();
- mail.To.Add(_objModelMail.To);
- mail.From = new MailAddress(_objModelMail.From);
- mail.Subject = _objModelMail.Subject;
- string Body = _objModelMail.Body;
- mail.Body = Body;
- mail.IsBodyHtml = true;
- SmtpClient smtp = new SmtpClient();
- smtp.Host = "smtp.gmail.com";
- smtp.Port = 587;
- smtp.UseDefaultCredentials = false;
- smtp.Credentials = new System.Net.NetworkCredential("username", "password");
- smtp.EnableSsl = true;
- smtp.Send(mail);
- return View("Index", _objModelMail);
- } else {
- return View();
- }
- }
- }
- }
Understanding the Code
In the code above we have a,
- ViewResult Index(SendMail.Models.MailModel _objModelMail)
user defined method. In this method, we have a parameter of our MailModel object. Now we create a MailMessage object.
- MailMessage mail = new MailMessage();
MailMessage is the main class for sending mail, it is in the System.Net.Mail namespace.
The MailMessage class has properties, the important ones are:
- To
- From
- Cc
- Bcc
- Subject
- Body
So we add our data into specified properties.
- mail.To.Add(_objModelMail.To);
-
- mail.From = new MailAddress(_objModelMail.From);
- mail.Subject = _objModelMail.Subject;
- string Body = _objModelMail.Body;
- mail.Body = Body;
For sending mail we need a SMTP Server, so in ASP.Net we have the SmtpClient class, we set the SMTP settings using the properties of that class.
- SmtpClient smtp = new SmtpClient();
The SMTPClient class has these basic properties:
- Host
- Port
- UseDefaultCredential
- Credentials
- EnableSsl
- Send
- smtp.Host = "smtp.gmail.com";
- smtp.Port = 587;
- smtp.UseDefaultCredentials = false;
- smtp.Credentials = new System.Net.NetworkCredential("username", "password");
- smtp.EnableSsl = true;
In the code above is,
- smtp.Host = "smtp.gmail.com";
That is the SMTP Host address of Gmail, if you want to use any other SMTP host service then please add a different SMTP host protocol, for example for Hotmail it is smtp.live.com.
For example, in,
Smtp.Port=587
587 is the port for Gmail, so for any other service port you need to change the port correspondingly.
- smtp.Credentials = new System.Net.NetworkCredential("username", "password");
Smtp.Credentials specifies the Network Credentials of your Gmail id so please add your username and password instead of ("username", "password");
The following is for a secure mail server, so you enable your SSL layer.
Smtp.Send sends the mail so please add your MailMesssage object here. Then, based on the properties, your mail will be sent.