Create A Contact Us Form For Your Website

Most of the times we have seen suggestion/complaint form in the footer of many websites but what if you want the same thing in your project?

Then I am here to guide you. As far as I know there are two methods to implement this:

  1. Make a database and save the form details in that so when the site admin logins, he/she can see them.
  2. Generate an email from the code so you can get an email and can retrieve that on the go.

I will explain the second method here. This method will simply guide you how to send an email and then you can use this in anyway you like.

Building the Sample

Firstly, I created a form in html view of an MVC project which looks like the following:

Contact us

HTML for this form is:

  1. @using (Html.BeginForm("email", "Home", FormMethod.Post))   
  2. {   
  4. <div class="row">  
  5.     <div class="col-lg-12">  
  6.         <form name="sentMessage" id="contactForm" novalidate>  
  7.             <div class="row">  
  8.                 <div class="col-md-6">  
  9.                     <div class="form-group">  
  10.                         <input type="text" name="sname" class="form-control" placeholder="Your Name *" id="name" required data-validation-required-message="Please enter your name.">  
  11.                             <p class="help-block text-danger"></p>  
  12.                         </div>  
  13.                         <div class="form-group">  
  14.                             <input type="email" name="semail"class="form-control" placeholder="Your Email *" id="email" required data-validation-required-message="Please enter your email address.">  
  15.                                 <p class="help-block text-danger"></p>  
  16.                             </div>  
  17.                             <div class="form-group">  
  18.                                 <input type="tel" name="sphone"class="form-control" placeholder="Your Phone *" id="phone" required data-validation-required-message="Please enter your phone number.">  
  19.                                     <p class="help-block text-danger"></p>  
  20.                                 </div>  
  21.                             </div>  
  22.                             <div class="col-md-6">  
  23.                                 <div class="form-group">  
  24.                                     <textarea class="form-control" name="smessage" placeholder="Your Message *" id="message" required data-validation-required-message="Please enter a message."></textarea>  
  25.                                     <p class="help-block text-danger"></p>  
  26.                                 </div>  
  27.                             </div>  
  28.                             <div class="clearfix"></div>  
  29.                             <div class="col-lg-12 text-center">  
  30.                                 <div id="success"></div>  
  31.                                 <button type="submit" class="btn btn-xl" onclick="this.form.submit();">Send Message</button>  
  32.                             </div>  
  33.                         </div>  
  34.                     </form>  
  35.                 </div>  
  36.             </div>   
  37. }  
If you copy and paste this html it won’t work as desired due to the css. So don’t get confused with the code this is just to guide you a bit, you can make any kind of form.

Observe the starting line of HTML code I used:
  1. @using (Html.BeginForm("email""Home", FormMethod.Post))  
This is used to call the controller ActionResult method when the user submits this form. "email" is the name of ActionResult method and "Home" is the controller. That is the main thing where the whole stuff happens.

In controller there is a method and an Action Method that calls the method.
  1. public async Task  
  2. <ActionResult> email(FormCollection form)   
  3. {   
  4. var name = form["sname"];   
  5. var email = form["semail"];   
  6. var messages = form["smessage"];   
  7. var phone = form["sphone"];   
  8. var x = await SendEmail(name,email,messages,phone);   
  9. if (x == "sent")   
  10. ViewData["esent"]= "Your Message Has Been Sent";   
  11. return RedirectToAction("Index");   
  12. }   
  13. private async Task  
  14.     <string> SendEmail(string name, string email, string messages, string phone)   
  15. {   
  16. var message = new MailMessage();   
  17. message.To.Add(new MailAddress("[email protected]")); // replace with receiver's email id   
  18. message.From = new MailAddress("[email protected]"); // replace with sender's email id   
  19. message.Subject = "Message From" + email;   
  20. message.Body = "Name: " + name + "\nFrom: " + email + "\nPhone: " + phone + "\n" + messages;   
  21. message.IsBodyHtml = true;   
  22. using (var smtp = new SmtpClient())   
  23. {   
  24. var credential = new NetworkCredential   
  25. {   
  26. UserName = "[email protected]", // replace with sender's email id   
  27. Password = "*****" // replace with password   
  28. };   
  29. smtp.Credentials = credential;   
  30. smtp.Host = "";   
  31. smtp.Port = 587;   
  32. smtp.EnableSsl = true;   
  33. await smtp.SendMailAsync(message);   
  34. return "sent";   
  35. }   
  36. }  
So what's happening here is that i have created an async task of ActionResult type which is called when user clicks the submit button and values from the form are passed into it.
  1. public async Task<ActionResult> email(FormCollection form)   
Async is used when you have to wait for an action to complete before moving ahead as this process requires some time so we need to use async type and this allows us to use await keyword in it.
  1. private async Task<string> SendEmail(string name, string email, string messages, string phone)  
This is a method which returns a string to tell the status. I have passed all the form values which i need in my email.
  1. message.To.Add(new MailAddress("[email protected]")); // replace with receiver's email id  
Here we have to write the email id on which we want to receive the email.
  1. message.From = new MailAddress("[email protected]"); // replace with sender's email id  
Here goes the senders email id, I have used my id in both so I am sending an email from my one id to another which contains the users query/suggestion, contact, name and email, so i can respond later if I want to:
  1. message.Subject = "Message From" + email;  
Here goes the subject, I have written the user's email in the subject, you can write anything you like.
  1. message.Body = "Name: " + name + "\nFrom: " + email + "\nPhone: " + phone + "\n" + messages;  
Here goes the body, I have written users name, email, phone and message in the body. You can get the desired things according to your requirements and form fields.
  1. var credential = new NetworkCredential   
  2. {   
  3. UserName = "[email protected]"// replace with sender's email id   
  4. Password = "*****" // replace with password   
  5. };  
  7. here you have to give your email id and password of that id which you are using to send email. so our code can login using these credentials.  
  9. smtp.Credentials = credential;   
  10. smtp.Host = "";   
  11. smtp.Port = 587;   
  12. smtp.EnableSsl = true;  
These are the setting of hotmail /outlook email service. I was using my hotmail id as a sender and it worked. If you are using live/outlook/hotmail you can use these ones but for other clients you can search Host and Port and replace these with those and it will work like a charm.

More Information

That's all folks, this is very easy to implement and I have tried my best to explain you step by step.

For more information, queries or to contact me kindly comment below.

Up Next
    Ebook Download
    View all
    View all