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

71 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

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

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

Unknown said...

thanks

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

Unknown said...
This comment has been removed by the author.
Unknown 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.

Unknown said...

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

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

Unknown 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

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

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

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

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

Unknown said...
This comment has been removed by the author.
Unknown 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);

Unknown said...

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

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

Unknown 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

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

Unknown said...

Nice Post Sirji

ravi said...

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

Abhi said...

Thanks Suresh Brother.

Praveen_Conquer said...

Suresh is awesome

pramod gagare said...

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

Unknown said...

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

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

Unknown said...

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

Thank you

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

Unknown 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

Unknown 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

Unknown 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 ransingh said...

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

Unknown said...

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

vidhya $agar said...

not working

ForwardChoicer said...

hi

ForwardChoicer said...

hi

Unknown 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

Unknown 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 64.233.167.108:587
this error comes

Unknown said...
This comment has been removed by the author.
Unknown 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

robotz said...

send email to multiple recipients receiver should see his email in To,rather all email Ids in asp.net c# suresh

robotz said...

send email to multiple recipients receiver should see his email in To,rather all email Ids in asp.net c# suresh

Unknown said...

Hi Suresh i am Fouzan

this code is not working for godaddy plesk panel can you please help me out...?

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

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