Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Asp.net Send Email using Gmail or Gmail SMTP Server

Dec 10, 2010
Introduction:

Here I will explain how to send email using gmail in asp.net or send email using gmail smtp server in asp.net using c#.

Description:

In our system if we don’t have smtp server for sending mails at that time we can send mails by using gmail smtp server in asp.net. I will show to how to implement mail sending concept using Gmail credentials in asp.net. To implement this concept first you need to enable POP enable in your Gmail account for this Settings-->Forwarding and POP/IMAP

To implement this mail concept in your asp.net application first we need to add this reference to our application System.Web.Mail namespace

What is System.Net.Mail

The System.Net.Mail namespace contains classes used to send electronic mail to a Simple Mail Transfer Protocol (SMTP) server for delivery.
How we can get this reference (System.Net.Mail)

for that we need to add System.web.dll reference to our application.
     a) On the Project menu, click Add Reference.
     b) On the .NET tab, locate System.Web.dll, and then click Select.
     c) Click OK in the Add References.

After that design your aspx page like this

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Send Mail using with gmail crendentials in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style=" border:1px solid" align="center">
<tr>
<td colspan="2" align="center">
<b>Send Mail using gmail credentials in asp.net</b>
</td>
</tr>
<tr>
<td>
Gmail Username:
</td>
<td>
<asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Gmail Password:
</td>
<td>
<asp:TextBox ID="txtpwd" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Subject:
</td>
<td>
<asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
To:
</td>
<td>
<asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td valign="top">
Body:
</td>
<td>
<asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Columns="30" Rows="10" ></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnSubmit" Text="Send" runat="server" onclick="btnSubmit_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

After that add this namcespace in your codebehind


using System.Net.Mail;

After that write the following code in button click

protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress(txtUsername.Text);
// Recipient e-mail address.
Msg.To.Add(txtTo.Text);
Msg.Subject = txtSubject.Text;
Msg.Body = txtBody.Text;
// your remote SMTP server IP.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials=new System.Net.NetworkCredential(txtUsername.Text,txtpwd.Text);
smtp.EnableSsl = true;
smtp.Send(Msg);
Msg = null;
Page.RegisterStartupScript("UserMsg", "<script>alert('Mail sent thank you...');if(alert){ window.location='SendMail.aspx';}</script>");
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
}
                   
Here we used smtp.Port=587 this is the port number which is used by gmail that’s why we used this port number. In some situations if you get any error regarding security remove smtp.EnableSsl = true;

Demo



Other related posts are 

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

54 comments :

Prasanthi said...

Hi Suresh Garu,

Nice post regarding Gmail Credentials.I had seen so many sites for this but i didn't find clear explanation like this.It helped me.Thank You

Suresh Dasari said...

thanks prasanthi
keep visiting......

Anonymous said...

Thank u sooooooooooooooooo much........ very useful.......

i send my first mail through asp.net........

Thank you

suhel said...

can u send hoe to send multiple reciepients

Anonymous said...

Simply Great ! answer to send mail

Anonymous said...

Hi Suresh

I m getting Error like

"Failure sending mail." in catch.

Suresh Dasari said...

check whether you enable your mail settings Forwarding and POP/IMAP
in your gmail based on mentioned details in article

Imtiaz said...

how to send email to multiple recipients, like we have list of email ids in gridview and program send email to all the user who are selected by user through checkbox control

Suresh Dasari said...

@Imtiaz..
If you want to send multiple recipients you need to append all the emails with semicolon like test@gmail.com;test@gmail.com and send mail as usual it will send mail to all the recipients...

kranthi said...

hai suresh garu if i check mail in inbox to delte i must get delete button above so can u give suggestion how to write for it my mail id is togaru.kranthi@gmail.com

Deepak Kumar said...
This comment has been removed by the author.
darpan pathak said...

sir how can we track delivery report of these email ??

Amresh ECIMT said...

thanks

madhava reddy said...

hai Suresh Garu ,
I want some help from you , for example
In Gmail we sending the mail if mail id wrong wet get one mail that is Delivery sending failed mail..the same functionality how we implement in Asp.net website.

Raghvendra pratap singh said...
This comment has been removed by the author.
Raghvendra pratap singh said...

hi suresh...i want to send a verfication mail to his email Id when user fills signup form in a website and when he clicks on that link then his account will activate and he will able to navigate in that website.plz help me as soon as possible.

Jana Mani said...

hi..
thks a lot man..
how can i attach a file with mail????

arpeeta joshi said...

i've used your code bt niether m getting any error nor its sending any mail

Anonymous said...

Arpeeta ::kindly Set the break POint. .

Rajesh Kokul said...

Thank you very much..........got mail without any error.
visited lot of sites but cant got any solution.But this code working fine...

Sir,,,Also can you suggest any code for sending SMS...???
any help will be most useful for me.......!!!
thanks in advance......

Anonymous said...

Thanks Man. Your helped me lot. in past i had try various example but had not succeed in sending mail. but your code is working perfectly

Thanking your many times.

Ankit Patel

Anonymous said...

sir when we send email to wrong email address
Then it does not show any message.

Page.RegisterStartupScript("UserMsg", "alert('Mail sent thank you...');if(alert){ window.location='SendMail.aspx';};

Above line show error in my page

Sarbjit Shindu said...

Dear Suresh,

I need your help to sending mail. I have written same code. But i am unable to send email using this code. Please let me know. Where i am wrong.

Shahabuddin shahab said...

Nice Post sir,
I have a problem, i need to send email to multiple email id. I cannot use semi colon to provide email id because i have a bulk data of email id. How can i do this. please help me.

IBOA EZ said...

when I run this code locally it works .................
but when i uploaded it to server it fails to work..........
sir plz help......

Anonymous said...

Nice post,bhargav

Anonymous said...

sir how can i get email on my gmail account from any id and not to write password on the screen.

Raasitha A said...

Superbbb Coding.Simple and easy to Understand.

admin said...

hi sir .. in local it's work .. i put code in sever means it's not working ..why ...

Sujith Ram said...
This comment has been removed by the author.
Sujith Ram said...

Getting 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 " help me with this ....this error occurs when Hosted in a server.

Hari om said...

i am using Asp.net 2010. and at the line

Page.RegisterStartupScript("UserMsg", "alert('Mail sent thank you...');if(alert){ window.location='SendMail.aspx';}");

shows a depricated function msg..... but it is working properly...

Thanks a lot....

Anonymous said...

I get error message ..

CS1061: 'string' does not contain a definition for 'Add' and no extension method 'Add' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)

// Sender e-mail address. Msg.To.Add(txtTo.Text);

akash patel said...

thank you very very much for help.......

Bantu Singh said...

Dear Suresh...

Thanks for this article, But its work only local system, but i upload on server that's not working. Please Solve my Problem ASP.

Rajesh P said...

After save the data,the mail will goes to particular manager using C#.net..? can u explain it.?

chandan kumar said...

Thanks,,,,,,,,,,,,

lmali92 said...

Its straightforward and it works like a charm.
Good job and Thank you

vnk said...

I am geting error "failure to send".. i enabled pop also in gmail.what is the problem

vabhi said...

The SMTP server requires a secure connection or the client was not authenticated.

Vikramraj Patil said...

Nice Post Sirji

Ravi Shanker said...

thaks suresh. it's working fine. simple,short and to the point example. Great to read your articles

Abhilash Boggavarapu said...

Thanks Suresh Brother.

Praveen_Conquer said...

Suresh is awesome

pramod gagare said...

Thank u........................

Pritam Patil said...

Thank you.
But I want send mail from Hosted Server. So How can i do this?

Ashok Mishra said...

Than U sir for your posts.Its very help ful to any new commer like me.
Sir I want to know about How to send a conformation mail regarding user password and user name after newly Signup of a user.
Plz help me out....

Santhosh Kumar said...

HI suresh
can you tell me why that i uanble to send mails when hosted on server.

Thank you

Suresh G said...

im not seeing system.data.dll in my .net

Anonymous said...

hi sir .....this code is not working for my project..and also im not getting system.web.dll in my project.

Preeti Jain said...

Hi Suresh...

what if I want to use any other mail address but not gmail...is there a way that mail can be send from any mail id or smtp server????????

सोमनाथ चतुर्वेदी said...

Nice sir

Lazola Booi said...

Thanks man.. This works 100% and I got to learn a thing or two.

Nevil patel said...

asp.net send email Nice article and nice write up. Thanks.

Give your Valuable Comments

Other Related Posts

© 2010-2012 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.