This example shows how to Send Email Using Gmail In ASP.NET. If you want to send mail using Gmail account or it's SMTP server in ASP.NET application if you don't have a working smtp server to send mails than sending e-mail with Gmail is best option.
you need to write code like this
First of all add below mentioned namespace in code behind of aspx page from which you want to send the mail.
using System.Net.Mail;
Now write this code in click event of button
C# code
protected void Button1_Click(object sender, EventArgs e)
{
MailMessage mail = new MailMessage();
mail.To.Add("Email ID where email is to be send");
mail.To.Add("Another Email ID where you wanna send same email");
mail.From = new MailAddress("YourGmailID@gmail.com");
mail.Subject = "Email using Gmail";
string Body = "Hi, this mail is to test sending mail"+
"using Gmail in ASP.NET";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
("YourUserName@gmail.com","YourGmailPassword");
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
smtp.Send(mail);
}
VB.NET code
Imports System.Net.Mail
Protected Sub Button1_Click
(ByVal sender As Object, ByVal e As EventArgs)
Dim mail As MailMessage = New MailMessage()
mail.To.Add("Email ID where you wanna send email")
mail.To.Add("Email ID where you wanna send copy of email")
mail.From = New MailAddress("YourGmailID@gmail.com")
mail.Subject = "Email using Gmail"
String Body = "Hi, this mail is to test sending mail"+
"using Gmail in ASP.NET"
mail.Body = Body
mail.IsBodyHtml = True
Dim smtp As SmtpClient = New SmtpClient()
smtp.Host = "smtp.gmail.com" //Or Your SMTP Server Address
smtp.Credentials = New System.Net.NetworkCredential
("YourUserName@gmail.com","YourGmailPassword")
smtp.EnableSsl = True
smtp.Send(mail)
End Sub
You also need to enable POP by going to settings > Forwarding and POP in your gmail account
Change YourUserName@gmail.com to your gmail ID and YourGmailPassword to Your password for Gmail account and test the code.
If your are getting error mentioned below
"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required."
than you need to check your Gmail username and password.
If you are behind proxy Server then you need to write below mentioned code in your web.config file
<system.net>
<defaultProxy>
<proxy proxyaddress="YourProxyIpAddress"/>
</defaultProxy>
</system.net>
If you are still having problems them try changing port number to 587
If you still having problems then try changing code as mentioned below
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = False;
smtp.Credentials = new System.Net.NetworkCredential
("YourUserName@gmail.com","YourGmailPassword");
smtp.EnableSsl = true;
smtp.Send(mail);
Hope this helps
Send Email Using Gmail In ASP.NET
Posted by
Unknown
smtp.Host = "smtp.gmail.com,587";
If you like this post than join us or share
Subscribe to:
Post Comments (Atom)
76 comments:
thanks for the code dude!
Dear
sir Can you tell me ..... is this is right procedure to send a mail thruogh a specific web application that i created and i also created a new mail id on gmail and using your code i am sending emial to my specific account over through gmail mail id and server name(smtp.gmail.com) ??
Can I use this code in Windows C# application
This comment has been removed by a blog administrator.
Thank you following easy to download
@Akshaya:
This should work fine with windows application as well , do let me know if you have any problems
this code is super dude........
This class for mailing does not support mails with attachments. To send mail with attachments you may find other libraries form net. Source Fourge might get useful.
i use this coding. but mail sending is failure. what else shall i want to do? shall i change any settings in my from account?
great code.....but is there any way to do this job without using my emaild id and password....pls send me at chakraborty.sumantra@gmail.com
Hello,
This article was very helpful. It all works fine but I don't know why whenever i submit the form I receive 2 copies of the 'email' on the recipient's account. Any ideas?
problem sending mail
[System.Net.Mail.SmtpException] = {"Service not available, closing transmission channel. The server response was: Cannot connect to SMTP server 72.14.221.111 (72.14.221.111:25), connect error 10060"}
this is only work work on .net framework v2.xxx,
Any other version for .net framework v1.xxxx
I would gladly appreciate if there was
It really helpmed me to send mail from gmail..
but when i tries to send mail from other SMTP server address(its a IP Address) and port,it failed
Can anyone help?
-Madhura
@Madhura:
What error you are getting ?
Try using port 587 and if your server doesn't support ssl then remove this line
smtp.EnableSsl = true;
Hope this helps
Hi,
I got error while run this program.
'Failure sending mail.'
What should I do further modification in code
by,
Abbhas
Hi,
Code is working but when user receive mail. He/ she receive mail as name use for NetworkCredential not MAIL.From ..............
than what is MAIL.From used for, how to solve this prob..........?????????
What about the web.config file. Are we supposed to make any changes to the file?
Is there a limit to the number of e-mails that can be sent out at one time this way? (There is a limit of 100 when sending via client software limitation like Outlook or Mozilla)
hi i face some problem,""The remote name could not be resolved: 'smtp.gmail.com'"" this type of error massage show. My system date is ok.
help me plz...
Thanx for the code.. it totally saved me..
thanks buddy
it helped me
Changing the port was the tip that helped me, as well as providing smtp object with my credentials.
Thank's alot!
Good Post......
i like it...
bro can u tell me how to read e-mail from asp.net with c#
thanks
Hi,
I got error while run this program.
'Failure sending mail.'
by,
Archana
Thank you soooooooo much.
Hi
Can I hide my Gmail Id,that is displayed in sender's inbox.
Or is there any other way so that I can replace the 'From' with any other valid email id
Hi
Thanks it is working.But, from here we can send to particular person,if we want to send to next person , we have to change again in the code.it is little bit time consuming. I want to send 'from' and 'to' 'subject' through textbox.I want to write everything in textbox and by clicking button it should work.
How can i do this.
please little bit of help is needed
Thanks!
Hi,
This is very nice code. I was struggling lot finally this code helped me.
Great
Thanks!
I get the error:
"SmtpException Unhandled
Failure Sending Mail"
I've Googled a dozen different forums all suggesting valuable things with extra code, less code, none of them work. Any ideas?
I also have this script in my web.Config
"
Any suggestions to rectified@hotmail.co.uk - Thanks! :)
Very Nice,
Thanks!
Hi I got this error message
connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 74.125.127.109
To,
Amit Jain
You are gr8...!
Nice code...
Thank you for code...!
Worm Regards
PANKAJ
To,
Amit Jain
Thanks for this Nice Code
It's really helpful to freshers like me
Worm Regards
PANKAJ
It's really a good article...
hello!!!
i 've got this error
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 74.125.127.109:587.
Plz how to do .
Best Regards.
Pann
Hello
i 've got this error
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 74.125.127.109:587
plz reply to me
Best Regards
Pann
cool
cool
Thanks
All those who face the problem of "Failure sending mail" use smtp.port=587
Enjy....
cool
hey i got FAILURE SENDING MAIL... Unhandled exception... what should i do?? reply soon.. and thanks for code!
i have that error
The remote name could not be resolved: 'smtp.gmail.com'
thxs 4 the article!
exception occurred:
The remote certificate is invalid according to the validation procedure.
what can i do....
code works very fine thanx for your code
@Mohan : seems like your system date is not set with current date and year, plz correct ur system date and then try
Hello sir
Greetings....
Please Help me... With Gmail A/c We can Send email very easily .. but please tell me how to send email with our own domain to any one.
Ex:From : admin@xyz.in
to : Any@gmail.com
please help me sir ...
@King : u need to know ur smtp server address and change below mentioned code accordingly
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
("YourUserName@gmail.com","YourGmailPassword");
//Or your Smtp Email ID and Password
Nice Article dude!!
Thanks a lot!
Hi..
I've been using your code, and any other code that i find on the internet related how to send email in asp.net. But i got this error: "A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 74.125.127.109:587"
Can you tell me how to solve this error?thank you so much...
I got smtpException was caught
Failure sending mail.
@above: Please provide me some more details or ur code
this thing wont workk.....
Thanks for showing your codes its help me a lot.
hello friends ,
please can any one help me in using attachments with the same coding please !!
normal program works but when i use a attachment it shows the error" Smtp authentication required or the client could not be authenticated , smtp shows 5.5.1 error" !! i hav egiven my password and all other things correct but still there is a problem !!
help me please !!!!!
thanks for the article...
its AweSome..!!!
hi , it very usefull for my project.. thanks
Thanx dude...Awesome...
Wow Nice Code
Thanx
Awesomeeeeee!!!!!!!!!
i m getting error: The SMTP server requires a secure connection or the client was not authenticated,The server response was: 5.5.1 Authentication Required.i have change setting in my gmail account to enable pop and smtp server.i used smtp.EnableSsl = true;smtp.Host = "smtp.gmail.com"; still i m getting same error.
@Above: try this code
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = False;
smtp.Credentials = new System.Net.NetworkCredential
("YourUserName@gmail.com","YourGmailPassword");
smtp.EnableSsl = true;
smtp.Send(mail);
Thanks for code, works well...
This comment has been removed by a blog administrator.
can we send emails from our smtp server without hosting asit
@archana gaikwad: yes you can use your own smtp server as well
Really Nice and Super
thanks for the code.
yes it really works......
Hi nice blog....it s possible to send for another email except gmail????
can we send mail without port number,host name?
thanks for code,
my question is how can send the same message to multi contact at the same time.
Post a Comment