Send Email Using Gmail In ASP.NET

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
smtp.Host = "smtp.gmail.com,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


If you like this post than join us or share

76 comments:

Anonymous said...

thanks for the code dude!


css said...

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) ??


Akshaya said...

Can I use this code in Windows C# application


Anonymous said...

This comment has been removed by a blog administrator.


steven said...

Professional web psdtemplates,flash templates easy to download at itemplatez.com .


steven said...

Thank you following easy to download


amiT jaiN said...

@Akshaya:

This should work fine with windows application as well , do let me know if you have any problems


webMASTER said...

this code is super dude........


r4 nintendo ds said...

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.


Vimmmiv said...

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?


Anonymous said...

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


Mario said...

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?


jobinelv said...

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"}


Christian Cadag said...

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


Anonymous said...

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


amiT jaiN said...

@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


Abbhas said...

Hi,
I got error while run this program.
'Failure sending mail.'
What should I do further modification in code
by,
Abbhas


ferhan said...

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


C#Freak said...

What about the web.config file. Are we supposed to make any changes to the file?


Anonymous said...

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)


sabuj said...

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


Anonymous said...

Thanx for the code.. it totally saved me..


Nick Matyas said...

Your posting is so valuable. Thanks for your posting.

http://www.webroyalty.com


DuFFeR said...

thanks buddy
it helped me


Anonymous said...

Changing the port was the tip that helped me, as well as providing smtp object with my credentials.
Thank's alot!


Anonymous said...

Good Post......
i like it...
bro can u tell me how to read e-mail from asp.net with c#


Anonymous said...

thanks


Anonymous said...

Hi,
I got error while run this program.
'Failure sending mail.'

by,
Archana


Anand said...

Thank you soooooooo much.


Anant said...

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


Anonymous said...

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


Anonymous said...

Thanks!


Anonymous said...

Hi,
This is very nice code. I was struggling lot finally this code helped me.
Great
Thanks!


Dan said...

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! :)


Anonymous said...

Very Nice,
Thanks!


I said...

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


Anonymous said...

To,
Amit Jain

You are gr8...!
Nice code...
Thank you for code...!

Worm Regards
PANKAJ


Pankaj said...

To,
Amit Jain

Thanks for this Nice Code
It's really helpful to freshers like me

Worm Regards
PANKAJ


Shakil said...

It's really a good article...


Anonymous said...

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


Anonymous said...

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


Anonymous said...

cool


Anonymous said...

cool


Akanksha mishra said...

Thanks


Naupad said...

All those who face the problem of "Failure sending mail" use smtp.port=587
Enjy....


Anonymous said...

cool


Anonymous said...

hey i got FAILURE SENDING MAIL... Unhandled exception... what should i do?? reply soon.. and thanks for code!


Fatma Zayed said...

i have that error
The remote name could not be resolved: 'smtp.gmail.com'


Anonymous said...

thxs 4 the article!


mohan said...

exception occurred:
The remote certificate is invalid according to the validation procedure.
what can i do....


rajesh said...

code works very fine thanx for your code


amiT jaiN said...

@Mohan : seems like your system date is not set with current date and year, plz correct ur system date and then try


king said...

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


amiT jaiN said...

@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


Anonymous said...

Nice Article dude!!

Thanks a lot!


Anggi said...

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


Anonymous said...

I got smtpException was caught

Failure sending mail.


amiT jaiN said...

@above: Please provide me some more details or ur code


Anonymous said...

this thing wont workk.....


Maria said...

Thanks


Big Suit said...

Thanks for showing your codes its help me a lot.


RAM said...

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


Anonymous said...

thanks for the article...


Anonymous said...

its AweSome..!!!


Prakash said...

hi , it very usefull for my project.. thanks


Suman said...

Thanx dude...Awesome...


Anonymous said...

Wow Nice Code

Thanx


Anonymous said...

Awesomeeeeee!!!!!!!!!


Anonymous said...

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.


amiT jaiN said...

@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);


dobinf said...

Thanks for code, works well...


Vikash said...

This comment has been removed by a blog administrator.


archana gaikwad said...

can we send emails from our smtp server without hosting asit


amiT jaiN said...

@archana gaikwad: yes you can use your own smtp server as well


KUMAR.S said...

Really Nice and Super


Anonymous said...

thanks for the code.
yes it really works......


Find More Articles