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.
Some of my
previous articles are as follows: Gmail
Smtp Error : 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, Read Outlook Inbox Mail And Email Count Of
Inbox Using C#.Net,
Business Email Validation Using
RegularExpressionValidator Control in Asp.net, Email Validation in Windows Application
C#.Net and VB.Net, Email Address Validation in Javascript in
Asp.Net, How to Add Email Id Hyperlink or Mailto Link
in Asp.net MVC WebGrid, Send Mail In Asp.Net C# Using Gmail, How
to Send Mail in MVC6 Using C# Code With Attachment, Contact
Us Form in Asp.net C#.
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"> </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
ReplyDeleteI table it shows "Error..." , why it will happen ??
Mail is not sended
Reply me soon
What the error you are getting. and did you used you gmail id credential or not to send the mail
DeletePlease let me know what error you are getting
DeleteWhy is the email actually sent to the contact form filler? Not to site admin?
ReplyDelete