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:
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.
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.
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. |
|||
|
|||
71 comments :
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
thanks prasanthi
keep visiting......
Thank u sooooooooooooooooo much........ very useful.......
i send my first mail through asp.net........
Thank you
can u send hoe to send multiple reciepients
Simply Great ! answer to send mail
Hi Suresh
I m getting Error like
"Failure sending mail." in catch.
check whether you enable your mail settings Forwarding and POP/IMAP
in your gmail based on mentioned details in article
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
@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...
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
sir how can we track delivery report of these email ??
thanks
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.
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.
hi..
thks a lot man..
how can i attach a file with mail????
i've used your code bt niether m getting any error nor its sending any mail
Arpeeta ::kindly Set the break POint. .
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......
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
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
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.
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.
when I run this code locally it works .................
but when i uploaded it to server it fails to work..........
sir plz help......
Nice post,bhargav
sir how can i get email on my gmail account from any id and not to write password on the screen.
Superbbb Coding.Simple and easy to Understand.
hi sir .. in local it's work .. i put code in sever means it's not working ..why ...
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.
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....
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);
thank you very very much for help.......
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.
After save the data,the mail will goes to particular manager using C#.net..? can u explain it.?
Thanks,,,,,,,,,,,,
Its straightforward and it works like a charm.
Good job and Thank you
I am geting error "failure to send".. i enabled pop also in gmail.what is the problem
The SMTP server requires a secure connection or the client was not authenticated.
Nice Post Sirji
thaks suresh. it's working fine. simple,short and to the point example. Great to read your articles
Thanks Suresh Brother.
Suresh is awesome
Thank u........................
Thank you.
But I want send mail from Hosted Server. So How can i do this?
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....
HI suresh
can you tell me why that i uanble to send mails when hosted on server.
Thank you
im not seeing system.data.dll in my .net
hi sir .....this code is not working for my project..and also im not getting system.web.dll in my project.
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????????
Nice sir
Thanks man.. This works 100% and I got to learn a thing or two.
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.
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
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.
set "Allow Less secured apps" on your gmail account to "yes" to overcome "Authentication Error 5.5.1"
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
not working
hi
hi
does nothing in my case !!!
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
Thanks Suresh its helpful.
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.
An attempt was made to access a socket in a way forbidden by its access permissions 64.233.167.108:587
this error comes
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
send email to multiple recipients receiver should see his email in To,rather all email Ids in asp.net c# suresh
send email to multiple recipients receiver should see his email in To,rather all email Ids in asp.net c# suresh
Hi Suresh i am Fouzan
this code is not working for godaddy plesk panel can you please help me out...?
Note: Only a member of this blog may post a comment.