In any web application, we may need to send Email for feedback, for messages, or for any other purpose. In this blog, I will show how to do it step by step.
- Start a new MVC project.
- Give it some name.
- Select ASP.NET template.
- Open your Home Controller (or any controller)
- In Controller, create an ActionResult method SendEmail (you can give it any name).
- public ActionResult SendEmail() {
- return View();
- }
- Add an empty View for this action method and add the following code.
- @ {
- ViewBag.Title = "SendEmail";
- } < h2 > SendEmail < /h2> < br / > < br / > @if(ViewBag.Error != null) { < h2 > @ViewBag.Error < /h2>
- } < form method = "post"
- action = "SendEmail" > < div class = "container" > < span class = "form-control-static" > Receiver: < /span> < input class = "form-control"
- type = "text"
- name = "receiver" / > < br / > < span class = "form-control-static" > Subject: < /span> < input class = "form-control"
- type = "text"
- name = "subject" / > < br / > < span class = "form-control-static" > Message: < /span> < input class = "form-control"
- type = "text"
- name = "message" / > < br / > < input class = "btn btn-primary"
- type = "submit"
- value = "Send" / > < /div> < /form>
- Now, add another action method with the same name and Httppost request as following,
- [HttpPost]
- public ActionResult SendEmail(string receiver, string subject, string message) {
- try {
- if (ModelState.IsValid) {
- var senderEmail = new MailAddress("[email protected]", "Jamil");
- var receiverEmail = new MailAddress(receiver, "Receiver");
- var password = "Your Email Password here";
- var sub = subject;
- var body = message;
- var smtp = new SmtpClient {
- Host = "smtp.gmail.com",
- Port = 587,
- EnableSsl = true,
- DeliveryMethod = SmtpDeliveryMethod.Network,
- UseDefaultCredentials = false,
- Credentials = new NetworkCredential(senderEmail.Address, password)
- };
- using(var mess = new MailMessage(senderEmail, receiverEmail) {
- Subject = subject,
- Body = body
- }) {
- smtp.Send(mess);
- }
- return View();
- }
- } catch (Exception) {
- ViewBag.Error = "Some Error";
- }
- return View();
- }
- Be sure that the strings in the arguments of Httppost method are same with that of name of inputs in the View because with these strings, inputs will be accessed from the View. And, write your password of Gmail Account in the blank space. Emails will be sent using this account.
- Now, save your project. It will look like the following in the browser.

Fill the fields and check for Email. It will work.

Harsh RupaparaPosted Apr 4, 2022, 7:48 PM
I wanted to add CC and BCC, can u elaborate more and help me with the code?
Harsh RupaparaPosted Apr 4, 2022, 7:47 PM
Thank you, its working smoothly.
Rodrigo MacielPosted May 15, 2021, 12:48 AM
Fiz tudo e quando envio.... apresenta o "Some Error".
matin taherzadehPosted Feb 22, 2021, 8:13 PM
How is that possible to turn off the 2 step verification and turn on the less secure app but still getting this error "Gmail Error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required".... any other tips? i almost did all the things i should do but still getting the same error about secure connection
Fáusio LuisPosted Oct 5, 2020, 2:44 AM
For me is not working, the app doent do anything
Xen DestaxPosted Jun 2, 2020, 10:49 PM
For httpost sendemail , this new controller or view?
Jomar PerezPosted Jan 30, 2020, 8:35 AM
I got some error
Serge MbeunePosted Oct 9, 2019, 3:51 AM
I executed the function but i had this error ""La valeur ne peut pas ?tre null.\r\nNom du param?tre?: address""
Debasis MohapatraPosted Oct 3, 2019, 2:31 AM
I am getting the error while create the empty view
Debasis MohapatraPosted Oct 3, 2019, 2:31 AM
Line 5: < h2 > SendEmail < /h2> < br / > < br / > @if (ViewBag.Error != null) {< h2 > @ViewBag.Error < /h2>}< form method = "post"
Danish aliPosted Aug 25, 2019, 9:14 AM
I have follow the step perfectly but it throws an exception of:The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
Amol TeliPosted Nov 15, 2018, 7:55 AM
Working fine for me, thanx
Perez MaribelPosted Sep 22, 2018, 2:02 PM
Thank you very much!! It worked immediately!
Nithish ReddyPosted Sep 20, 2018, 4:01 AM
Same Error Bro ! i did all the changes what you have said in below comment !
Dhimo SimoniPosted Sep 17, 2018, 3:48 AM
It looks Great!
Nithish ReddyPosted Sep 6, 2018, 5:38 AM
Add all the references but still same problem ?
Nithish ReddyPosted Sep 4, 2018, 1:45 AM
Generating an error :;SERVER DOES NOT CONNECT SECURE CONNECTIONS" !
Kashif AsifPosted Jul 18, 2017, 9:29 PM
Great UI. and nice artcle keep it up
Sameer ShaikPosted Jul 13, 2017, 9:02 AM
UI looks nice, but big textbox for Message field will make it even better.