Introduction
Now in this article I will explain how you can send email using your GMAIL account credentials i.e. gmail email id and password in
asp.net.
Implementation: Let's create a demo web application to understand how we can send email in asp.net using gmail credentials:
- In the design page (.aspx) design the page as:
<table style="width:100%;">
<tr>
<td>
Send To(Email Address)</td>
<td>
<asp:TextBox ID="txtEmailId" runat="server" Columns="60"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Subject</td>
<td>
<asp:TextBox ID="txtSubject" runat="server" Columns="60"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Body</td>
<td>
<asp:TextBox ID="txtBody" runat="server" Columns="60" Rows="5"
TextMode="MultiLine"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnSendEmail" runat="server" onclick="btnSendEmail_Click"
Text="Send
Email" />
</td>
</tr>
</table>
Asp.Net C# Code to send emails in asp.net using Gmail
First of all Include following namespaces:
using System.Configuration;
using System.Net;
using System.Net.Mail;
- Now in the code behind file(.aspx.cs) write the code to send
Email on send mail button as:
protected void btnSendEmail_Click(object sender, EventArgs e)
{
if (SendEmailUsingGmail("YourGmailId@gmail.com", txtEmailId.Text.Trim(),
txtSubject.Text.Trim(), txtBody.Text.Trim(), "YourGmailPassword"))
{
Response.Write("Mail send");
}
else
{
Response.Write("Error in sending mail");
}
}
private static Boolean SendEmailUsingGmail(string fromEmailAddress, stringtoEmailAddress, string subject, string messageBody, string gmailPassword)
{
try
{
SmtpClient smtp = new SmtpClient();
smtp.Credentials = new NetworkCredential(fromEmailAddress,
gmailPassword);
smtp.Port = 587;
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
MailMessage message = new MailMessage();
message.From = new MailAddress(fromEmailAddress);
message.To.Add(toEmailAddress);
message.Subject = subject;
message.Body = messageBody;
smtp.Send(message);
return true;
}
catch
{
return false;
}
}
Asp.net VB Code to send emails in asp.net using Gmail
Include following namespaces:
imports
System.Configuration
imports
System.Net
imports
System.Net.Mail
- Now in the code behind file(.aspx.vb) write
the code to send Email on send mail button as:
Protected Sub btnSendEmail_Click(sender As Object, e As
EventArgs)
If SendEmailUsingGmail("YourGmailId@gmail.com",
txtEmailId.Text.Trim(), txtSubject.Text.Trim(), txtBody.Text.Trim(),
"YourGmailPassword") Then
Response.Write("Mail send")
Else
Response.Write("Error in sending mail")
End If
End
Sub
Private
Shared Function SendEmailUsingGmail(fromEmailAddress As String, toEmailAddress
As String, subject As String, messageBody As String, gmailPassword As String)
As [Boolean]
Try
Dim smtp As New SmtpClient()
smtp.Credentials = New NetworkCredential(fromEmailAddress, gmailPassword)
smtp.Port = 587
smtp.Host = "smtp.gmail.com"
smtp.EnableSsl = True
Dim message As New MailMessage()
message.From = New MailAddress(fromEmailAddress)
message.[To].Add(toEmailAddress)
message.Subject = subject
message.Body = messageBody
smtp.Send(message)
Return True
Catch
Return False
End Try
End
Function
0 comments:
Post a Comment