This
article will show you how you can send an email using gmail as smtp server in
asp.net using 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.
So for this
article first we will create a new aasp.net application and create a form. This
form will contain fields to provide information to send in email. So here is
the page code.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm13.aspx.cs" Inherits="WebApplication7.WebForm13" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>send mail in
asp.net c# using gmail</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<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">
<asp:Label ID="lblmessage"
runat="server" Style="color: #FF3300"></asp:Label>
</td>
</tr>
<tr>
<td align="right">To
:</td>
<td align="left">
<asp:TextBox ID="txttoaddress"
runat="server" Width="250px"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right">Subject
:</td>
<td align="left">
<asp:TextBox ID="txtsubject"
runat="server" Width="250px"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right">Message
Body :</td>
<td align="left">
<asp:TextBox ID="txtmessage"
runat="server" Height="103px" TextMode="MultiLine"
Width="250px"></asp:TextBox>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:Button ID="Button1" runat="server"
OnClick="Button1_Click" Text="Send Message"
/></td>
</tr>
</table>
</div>
</form>
</body>
</html>
|
After this
we will create a button click event and add the below code.
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using
System.Web.UI.WebControls;
namespace WebApplication7
{
public partial class WebForm13 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
MailMessage mail
= new MailMessage();
mail.To.Add(txttoaddress.Text);
mail.From = new MailAddress("yourgmailid @gmail.com");
mail.Subject = txtsubject.Text;
string Body =
txtmessage.Text;
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp
= new SmtpClient();
smtp.Host = "smtp.gmail.com"; //SMTP Server Address of
gmail
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("yourgmailid@gmail.com", "password");
// Smtp Email ID and Password For authentication
smtp.EnableSsl = true;
smtp.Send(mail);
lblmessage.Text = "Your
Message Send Successfully.";
}
catch
{
lblmessage.Text = "Error............";
}
}
}
}
|
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
the received email
DOWNLOAD
0 comments:
Please let me know your view