Send Mail using Gmail SMTP Server-Asp.net c#

Send Mail using Gmail SMTP Server :

For Sending mail using Gmail SMTP Server,We need one real  gmail account-Username and Password are needed.

And one important Things for this  is u need Internet Connection.

================================================

Code files :–>

================================================

SendEmailDemo.aspx page –>

————————————————————————————-

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”SendEmailDemo.aspx.cs” Inherits=”SendEmail_SendEmailDemo” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;

<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head runat=”server”>
<title></title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>

<br />
<asp:Label ID=”Label1″ runat=”server” Text=”Label”></asp:Label>
<br />
<br />
To ;&nbsp;&nbsp;&nbsp;
<asp:TextBox ID=”txtTo” runat=”server”></asp:TextBox>
<br />
<br />
Subject :
<asp:TextBox ID=”txtSubject” runat=”server”></asp:TextBox>
<br />
<br />
Body :&nbsp;&nbsp;
<asp:TextBox ID=”txtBody” runat=”server” Height=”154px” TextMode=”MultiLine”
Width=”496px”></asp:TextBox>
<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Button ID=”Button1″ runat=”server” onclick=”Button1_Click” Text=”Send” />

</div>
</form>
</body>
</html>

————————————————————————————-

SendMailDemo.aspx.cs –>

—————————————————————————————

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;

public partial class SendEmail_SendEmailDemo : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{

string Username = “pradip@gmail.com”;
string Password = “11121212”;
string To;
string Subject;
string Body;
bool flag1;

To = txtTo.Text;
Body = txtBody.Text;
Subject = txtSubject.Text;

if (Username != “” || Password != “” || Subject != “” || Body != “” || To != “”)
{
//      className   Function
flag1 = MailSender.SendEmail(Username, Password, To, Subject, Body, System.Web.Mail.MailFormat.Html, “”);
if (flag1)
{

Label1.Text = “Mail send succ.”;
}
}
else
{

Label1.Text = “failure…..”;
}

}
}

—————————————————————————————–

MailSender.cs file — >Put this file into App_Code Folder As a Code File.

————————————————————————————–

using System.Web.Mail;
using System;
public class MailSender
{
public static bool SendEmail(
string pGmailEmail,
string pGmailPassword,
string pTo,
string pSubject,
string pBody,
System.Web.Mail.MailFormat pFormat,
string pAttachmentPath)
{
try
{
System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage();
myMail.Fields.Add
(“http://schemas.microsoft.com/cdo/configuration/smtpserver&#8221;,
“smtp.gmail.com”);
myMail.Fields.Add
(“http://schemas.microsoft.com/cdo/configuration/smtpserverport&#8221;,
“465”);
myMail.Fields.Add
(“http://schemas.microsoft.com/cdo/configuration/sendusing&#8221;,
“2”);
//sendusing: cdoSendUsingPort, value 2, for sending the message using
//the network.

//smtpauthenticate: Specifies the mechanism used when authenticating
//to an SMTP
//service over the network. Possible values are:
//- cdoAnonymous, value 0. Do not authenticate.
//- cdoBasic, value 1. Use basic clear-text authentication.
//When using this option you have to provide the user name and password
//through the sendusername and sendpassword fields.
//- cdoNTLM, value 2. The current process security context is used to
// authenticate with the service.
myMail.Fields.Add
(“http://schemas.microsoft.com/cdo/configuration/smtpauthenticate&#8221;,”1″);
//Use 0 for anonymous
myMail.Fields.Add
(“http://schemas.microsoft.com/cdo/configuration/sendusername&#8221;,
pGmailEmail);
myMail.Fields.Add
(“http://schemas.microsoft.com/cdo/configuration/sendpassword&#8221;,
pGmailPassword);
myMail.Fields.Add
(“http://schemas.microsoft.com/cdo/configuration/smtpusessl&#8221;,
“true”);
myMail.From = pGmailEmail;
myMail.To = pTo;
myMail.Subject = pSubject;
myMail.BodyFormat = pFormat;
myMail.Body = pBody;
if (pAttachmentPath.Trim() != “”)
{
MailAttachment MyAttachment =
new MailAttachment(pAttachmentPath);
myMail.Attachments.Add(MyAttachment);
myMail.Priority = System.Web.Mail.MailPriority.High;
}

System.Web.Mail.SmtpMail.SmtpServer = “smtp.gmail.com:465”;
System.Web.Mail.SmtpMail.Send(myMail);
return true;
}
catch (Exception ex)
{
throw;
}
}
}
———————————————————————————————

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s