Friday, 2 September 2016

Contact Us Form in MVC5 & MVC6 C#

9/02/2016 - By Pranav Singh 4

This article will show you how you can how you can create an enquiry form or contact us form in mvc5 and mvc6 using gmail as smtp server and c#.net.


So for this article first we will create a new application and add the below code in controller class file.

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 Name, string EmailId, string PhoneNo, string Subject, string Message)
        {
            try
            {
                MailMessage mail = new MailMessage();
                mail.To.Add(EmailId);
                mail.From = new MailAddress("youremailid@gmail.com");
                mail.Subject = Subject;

                string userMessage = "";
                userMessage = "<br/>Name :" + Name;
                userMessage = userMessage + "<br/>Email Id: " + EmailId;
                userMessage = userMessage + "<br/>Phone No: " + PhoneNo;
                userMessage = userMessage + "<br/>Message: " + Message;
                string Body = "Hi, <br/><br/> A new enquiry by user. Detail is as follows:<br/><br/> " + userMessage + "<br/><br/>Thanks";


                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("youremailid@gmail.com", "Password");
                // Smtp Email ID and Password For authentication
                smtp.EnableSsl = true;
                smtp.Send(mail);
                ViewBag.Message = "Thank you for contacting us.";
            }
            catch
            {
                ViewBag.Message = "Error............";
            }

            return View();
        }
    }
}

In above code I have create httpget an httppost attribute. In above code I have used gmail as smtp server to send the detail in email.

Now we will create the view and add the below code.

@{
    ViewBag.Title = "Contact Us Form in MVC5 & MVC6 C#";
}
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <table width="100%" border="1">
        <tr>
            <td align="right" colspan="2" style="text-align: center"><strong>SEND EMAIL USING GMAIL ACCOUNT IN MVC</strong></td>
        </tr>
        <tr>
            <td align="right">&nbsp;</td>
            <td align="left">
                @ViewBag.Message
            </td>
        </tr>
        <tr>
            <td align="right">Name :</td>
            <td align="left">
                <input id="txtName" name="Name" width="250px" />
            </td>
        </tr>
        <tr>
            <td align="right">Your Email id  :</td>
            <td align="left">
                <input id="txttoaddress" name="EmailId" width="250px" />
            </td>
        </tr>
        <tr>
            <td align="right">Phone No :</td>
            <td align="left">
                <input id="txtPhoneno" name="PhoneNo" 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 :</td>
            <td align="left">
                <textarea rows="4" cols="50" name="Message" id="txtmessage"></textarea>
            </td>
        </tr>
        <tr>
            <td align="right">Attachment :</td>
            <td align="left">
                <input type="file" name="file" />
            </td>
        </tr>

        <tr>
            <td align="center" colspan="2">
                <input type="submit" text="Send Message" />
            </td>
        </tr>
    </table>
}

In above code I have added I have create a contact us form. In this we need to take care of the controls name. because in MVC we can only detect the added control value by it’s name.  This function will send the enquiry to the admin of the website.

So in above code I have used the smtp of gmail for sending the email.  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 your to email id


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

4 comments:


  1. I table it shows "Error..." , why it will happen ??
    Mail is not sended
    Reply me soon

    ReplyDelete
    Replies
    1. What the error you are getting. and did you used you gmail id credential or not to send the mail

      Delete
    2. Please let me know what error you are getting

      Delete
  2. Why is the email actually sent to the contact form filler? Not to site admin?

    ReplyDelete

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