aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies Send Email using Gmail or Gmail SMTP Server

Dec 10, 2010

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


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 I will show to how to implement mail sending concept using Gmail credentials in 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 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="">
<head id="Head1" runat="server">
<title>Send Mail using with gmail crendentials in</title>
<form id="form1" runat="server">
<table style=" border:1px solid" align="center">
<td colspan="2" align="center">
<b>Send Mail using gmail credentials in</b>
Gmail Username:
<asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
Gmail Password:
<asp:TextBox ID="txtpwd" runat="server" TextMode="Password"></asp:TextBox>
<asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
<asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
<td valign="top">
<asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Columns="30" Rows="10" ></asp:TextBox>
<asp:Button ID="btnSubmit" Text="Send" runat="server" onclick="btnSubmit_Click" />

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)
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress(txtUsername.Text);
// Recipient e-mail address.
Msg.Subject = txtSubject.Text;
Msg.Body = txtBody.Text;
// your remote SMTP server IP.
SmtpClient smtp = new SmtpClient();
smtp.Host = "";
smtp.Port = 587;
smtp.Credentials=new System.Net.NetworkCredential(txtUsername.Text,txtpwd.Text);
smtp.EnableSsl = true;
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;


Other related posts are 

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

Get the latest,, 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


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

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...

If you want to send multiple recipients you need to append all the emails with semicolon like; 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

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...


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 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...

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 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 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 can u explain it.?

chandan kumar said...


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 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 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.

Anonymous said...

Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.

Anonymous said...

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

kamal upadhyay said...

It is working at local host , But when I put my website at server. it shows
//** The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

rashmi ranjan said...

set "Allow Less secured apps" on your gmail account to "yes" to overcome "Authentication Error 5.5.1"

Jignesh Gajera said...

hey im facing this error:- Service not available, closing transmission channel. The server response was: Cannot connect to SMTP server (, connect error 10060

vidhya $agar said...

not working

ForwardChoicer said...


ForwardChoicer said...


Chandra Shakhar said...

does nothing in my case !!!

Anonymous said...

Hello Mr. Suresh
I am Getting this Error...
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required

Mohammed Akram said...

Thanks Suresh its helpful.

charan said...

Very excellent learned and i just implemented for on blog using godaddy host, but some mails are sending in spam. what is the solution? Thanks, Suresh! in advance.

raras said...

An attempt was made to access a socket in a way forbidden by its access permissions
this error comes

Saikh Rakif said...
This comment has been removed by the author.
Saikh Rakif said...

Hello Mr. Suresh
I am Getting this Error...
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required

i am also set allow less secure app to true

Give your Valuable Comments

Other Related Posts

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