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.
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.
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"> </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
till i am getting error :
ReplyDeleteServer does not support secure connections
what should i do please help me ?
Hi
DeletePlease refer the below link
http://www.aspdotnet-pools.com/2016/08/gmail-smtpo-error-smtp-server-requires.html