Email sender with form collection in Mvc

Manideepgoud
Posted by in ASP.NET MVC category on for Beginner level | Points: 250 | Views : 789 red flag
Rating: 3 out of 5  
 2 vote(s)

In this article we shall learn how to send email using form collection

Introduction

This article explains about form collection with email sender.

Step-1
Here we are seeing the example of contact page, Select Contact in Home Controller  as shown below.

public ActionResult Contact()
{
  ViewBag.Message = "Your contact page";
  return View();
}
Step-2

Then right click near Contact() and select Go To View as shown below.



Then it displays view of the contact page.

Step-3
After  opening View of the Contact Page. we need to create page with details for sending mail such as YourName, Company Name, Email, Contents etc.
<div class="form-horizontal"> 
@using (Html.BeginForm("Contact","Home"))
            {
                @Html.AntiForgeryToken();
                <div class="form-group">
                    <label class="col-md-2 control-label">Category*</label>
                    <div class="col-md-4">
                        <select name="Category" id="Category" class="form-control">
                            <option value="Contact us">Contact Us</option>
                            <option value="Company registration">Company registration</option>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-2 control-label">YourName</label>
                    <div class="col-md-4">
                        <input type="text" name="YourName" class="form-control"  placeholder="YourName" required />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-2 control-label">CompanyName</label>
                    <div class="col-md-4">
                        <input type="text" class="form-control" name="CompanyName" placeholder="Company Name" required />
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-2 control-label">Your Email Id*</label>
                    <div class="col-md-4">
                        <input name="Email" type="email" class="form-control"  placeholder="Your email id" required />
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-md-2">Contents</label>
                    <div class="col-md-4">
                        <textarea class="form-control text-box multi-line" rows="10" name="Contents" placeholder="Submit Content" required></textarea>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-2 control-label"></label>
                    <div class="col-md-7">
                        <div class="g-recaptcha" data-sitekey=" "></div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" value="Submit" class="btn btn-success" />
                    </div>
                </div>
            }
        </div>
Here we have created forms with label names and we  have button in the last form, Here we are sending email by clicking on button, After creating the view page  output appears as 

Step-4

After creating View page we have create action method of the Contact in Home Controller 

public ActionResult Contact()
{
  ViewBag.Message = "Your contact page";
  return View();
}

Step-5

After opening the Home controller we have to create Action method Contact and then we have to create Action method for Contact and we have call the form s created in view page with label names  as shown below.

public ActionResult Contact()
{
  ViewBag.Message = "Your contact page";
  return View();
}
[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Contact(FormCollection form)
        {
            string category = form["Category"];
            string yourName = form["YourName"];
            string companyName = form["CompanyName"];
            string email = form["Email"];
            string contents = form["Contents"];

            EmailSender.SendEmail("webmaster@" + impFunctions.DomainName(), "", "", "", category, contents, System.Net.Mail.MailPriority.High, true);
            ViewBag.SuccessMessage = "Thanks, we have received your email and will get back to you shortly.";
            return View();
        }


Here we have Action method of the Contact , where we have public ActionResult Contact(FormCollection form) binds the view page of the Contact in Controller and we have declared string names as Category, Your Name, CompanyName, Email, Contents with form values and in the next line we are having EmailSender.SendEmail() which is used to send mail to given domainname.

In order to send mail we have to select category, Name, Company Name and Email id, then if we click the submit button mail will be sent to following given mail and after mail has sent it shows a message "Thanks, we have received your email and will get back to you shortly.
 






Page copy protected against web site content infringement by Copyscape

About the Author

Manideepgoud
Full Name: manideep goud
Member Level: Starter
Member Status: Member
Member Since: 6/11/2015 5:55:01 AM
Country: India



Login to vote for this post.

Comments or Responses

Posted by: Sheonarayan on: 12/8/2016 | Points: 25
Thanks for the article Manideep, however there is no code that Send email.

Can you explain EmailSender.SendEmail method?
Posted by: Manideepgoud on: 12/21/2016 | Points: 25
EmailSender.SendEmail() is a method which validates the data and it sends mail to the given mail id for example (abc@yahoo.com)

Login to post response

Comment using Facebook(Author doesn't get notification)