Friday, 26 August 2016

Send Mail In MVC5 and MVC6 In C# Using Gmail

8/26/2016 - By Pranav Singh 2

This article will show you how you can send an email using gmail as smtp server in mvc5 and mvc6 using c#.net. This article you can use in MVC2, MVC3, MVC4, MVC5, MVC6.


So for this article first we will create a new  asp.net mvc application and add a controller and create the view for creating the form. This form will contain fields to provide information to send in email. Now add the below code in your view.

@model MvcApplication8.Models.CountryModel
@{
    ViewBag.Title = "Send Mail In MVC5 and MVC6 In C# Using Gmail";
}
@using (Html.BeginForm("Index", "Home"))
{
    <table width="100%" border="1">
        <tr>
            <td align="right" colspan="2" style="text-align: center"><strong>SEND EMAIL USING GMAIL ACCOUNT</strong></td>
        </tr>
        <tr>
            <td align="right">&nbsp;</td>
            <td align="left">
                @ViewBag.Message
            </td>
        </tr>
        <tr>
            <td align="right">To :</td>
            <td align="left">
                <input id="txttoaddress" name="ToEmailId" width="250px" />
            </td>
        </tr>
        <tr>
            <td align="right">Subject :</td>
            <td align="left">
                <input id="txtsubject" name="Subject" width="250px"></input>
            </td>
        </tr>
        <tr>
            <td align="right">Message Body :</td>
            <td align="left">
                <textarea rows="4" cols="50" name="Message" id="txtmessage"></textarea>
            </td>
        </tr>
        <tr>
            <td align="center" colspan="2">
                <input type="submit" text="Send Message" />
            </td>
        </tr>
    </table>
}

In above code I have added some textbox and button control. Here we need to take care one thing about the name of the control. You must use a specific name of a control. So we use name of the control to access the value of control.

After this we will the below code to the controller.

using MvcApplication8.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication8.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(string ToEmailId, string Subject, string Message)
        {
            try
            {
                MailMessage mail = new MailMessage();
                mail.To.Add(ToEmailId);
                mail.From = new MailAddress("yourgmailid@gmail.com");
                mail.Subject = Subject;
                string Body = Message;
                mail.Body = Body;
                mail.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                //SMTP Server Address of gmail
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.Credentials = new System.Net.NetworkCredential("yourgmailid@gmail.com", "your password");
                // Smtp Email ID and Password For authentication
                smtp.EnableSsl = true;
                smtp.Send(mail);
                ViewBag.Message = "Your Message Send Successfully";
            }
            catch
            {
                ViewBag.Message = "Error............";
            }

            return View();
        }
    }
}

So in above code I have used the smtp of gmail for sending the email.  In above please check the parameter passed in the httppost method. Keep it same as the control names in your view.Now one this after execution if you are getting error message as shown below:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at

Then follow the below link to resolve the error.


Now we have done. Run the application to check the output.




Now check the received email


About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

2 comments:

  1. till i am getting error :
    Server does not support secure connections
    what should i do please help me ?

    ReplyDelete
    Replies
    1. Hi
      Please refer the below link
      http://www.aspdotnet-pools.com/2016/08/gmail-smtpo-error-smtp-server-requires.html

      Delete

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top