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


76 comments:

  1. thanks for the code dude!

    ReplyDelete
  2. 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) ??

    ReplyDelete
  3. Can I use this code in Windows C# application

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. @Akshaya:

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

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

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

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

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

    ReplyDelete
  10. 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"}

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

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

    ReplyDelete
  13. @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

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

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

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

    ReplyDelete
  17. 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)

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

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

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

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

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

    by,
    Archana

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

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

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

    ReplyDelete
  26. 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! :)

    ReplyDelete
  27. Very Nice,
    Thanks!

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

    ReplyDelete
  29. To,
    Amit Jain

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

    Worm Regards
    PANKAJ

    ReplyDelete
  30. To,
    Amit Jain

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

    Worm Regards
    PANKAJ

    ReplyDelete
  31. It's really a good article...

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

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

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

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

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

    ReplyDelete
  37. thxs 4 the article!

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

    ReplyDelete
  39. code works very fine thanx for your code

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

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

    ReplyDelete
  42. @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

    ReplyDelete
  43. Nice Article dude!!

    Thanks a lot!

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

    ReplyDelete
  45. I got smtpException was caught

    Failure sending mail.

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

    ReplyDelete
  47. this thing wont workk.....

    ReplyDelete
  48. Thanks for showing your codes its help me a lot.

    ReplyDelete
  49. 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 !!!!!

    ReplyDelete
  50. thanks for the article...

    ReplyDelete
  51. hi , it very usefull for my project.. thanks

    ReplyDelete
  52. Wow Nice Code

    Thanx

    ReplyDelete
  53. Awesomeeeeee!!!!!!!!!

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

    ReplyDelete
  55. @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);

    ReplyDelete
  56. Thanks for code, works well...

    ReplyDelete
  57. This comment has been removed by a blog administrator.

    ReplyDelete
  58. can we send emails from our smtp server without hosting asit

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

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

    ReplyDelete
  61. Hi nice blog....it s possible to send for another email except gmail????

    ReplyDelete
  62. can we send mail without port number,host name?

    ReplyDelete
  63. thanks for code,
    my question is how can send the same message to multi contact at the same time.

    ReplyDelete