Sunday, October 01, 2006

Send Email from Gmail in asp.net 2.0

Simplest Way of Sending Email from Gmail

protected void btnSendEmail_Click(object sender, EventArgs e)
{
//Create Mail Message Object with content that you want to send with mail.
System.Net.Mail.MailMessage MyMailMessage = new System.Net.Mail.MailMessage("dotnetguts@gmail.com","myfriend@yahoo.com",
"This is the mail subject", "Just wanted to say Hello");

MyMailMessage.IsBodyHtml = false;

//Proper Authentication Details need to be passed when sending email from gmail
System.Net.NetworkCredential mailAuthentication = new
System.Net.NetworkCredential("dotnetguts@gmail.com", "myPassword");

//Smtp Mail server of Gmail is "smpt.gmail.com" and it uses port no. 587
//For different server like yahoo this details changes and you can
//get it from respective server.
System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient("smtp.gmail.com",587);

//Enable SSL
mailClient.EnableSsl = true;

mailClient.UseDefaultCredentials = false;

mailClient.Credentials = mailAuthentication;

mailClient.Send(MyMailMessage);
}

39 comments:

Unknown said...

When i use your code i found following 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

Anonymous said...

Worked so well :)

fsdfsd fsdf sdfasdf dsf said...

worked very well for me tooo... thnx for providing the code

nktonu@yahoo.com

epaphrodite said...

This worked very well for me. Like a charm.

Anuja Pawar said...

thanks it really helped me

Hussain said...

i got error "The operation has timed out" when i m executing. Plz help me

Manvinder said...

Thanks for code, it works flawlessly.

But i tried it for yahoo and not found any luck here.

Will you please direct me to do it

Unknown said...

Nice. Helped me with that port.. Thought it was 465.

Anonymous said...

Thank you! )

Anonymous said...

Hey This is veryyyyy helpful to me.

Thank you.

(So i can done my hw now. phewww.)

Anonymous said...

When I used your comment I got the folloing error Message.


The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

Can you please guid me How to rectify this

DotNetGuts said...

@Sakthivel

I have tested the code and it is working perfectly fine.

Make sure that you haven't forget adding following lines.
//Enable SSL
mailClient.EnableSsl = true;

mailClient.UseDefaultCredentials = false;

Also change Email and Password, Incase if you are not changing it.

Anonymous said...

This worked well!

Thanks for the code.

Download Free Photos

MFS said...

Yeeah maaan!
It worked YEEESSS. AAAAaaaaa!! :)))
I spend a 9-8 hours trying to figure out this thing!
THANK YOU VERY-VERY Much!
Hugs& Kisses :))
:)))
Just kidding :))

Abhishek Hingu said...

Its work but its return DeliveryNotification value is fail

but i have received mail

have u any idea about this one

Anonymous said...

No error, worked very cool ! Thanks for sharing.

Unknown said...

Hey Thanks Buddy it is perfect
now what i want is need is CC and BCC

Anonymous said...

great man great It's working so good THanks again

Anonymous said...

Hey cool its workin!!! Thanks 4 code yaa.. :):)

Héctor Pérez said...

Really usefull, thanks a lot, it works fine :)

wael34218 said...

Does anyone know the maximum number of emails i can send per day ?
I read in other pages that there is a limit to the number of emails, but none give me the exact number.

Thanks

DotNetGuts said...

@Wael

I don't know the exact number too, but there is a limit of I guess 50 emails per hour. This limit is to avoid Spamming of email.

RRave said...

Dear Sir,

I hope you are doing well. I got this email address from one of your contribution web site. I have launched a web site www.codegain.com and it is basically aimed C#,JAVA,VB.NET,ASP.NET,AJAX,Sql Server,Oracle,WPF,WCF and etc resources, programming help, articles, code snippet, video demonstrations and problems solving support. I would like to invite you as an author and a supporter.
Looking forward to hearing from you and hope you will join with us soon.

Unknown said...

Thanks so much!!! I spent ages trying to figure this out. Had the wrong port number.

Rony John said...

thank you so much sir...
thank you so much sir...
thank you so much sir...
thank you so much sir...
thank you so much sir...
thank you so much sir...
thank you so much sir...
thank you so much sir...
thank you so much sir...
thank you so much sir...

Anonymous said...

Your Code is very much useful...

gayatri said...

Wow.. amazing. It worked. I was struggling since 2 days to make it work.

Unknown said...

it worked at least!!!, thxxx!!

Duane said...

Excellent code, thank-you very much

priyansh said...

thank you ,it really worked
strugling from 2 days ,trying lots of things that dint work,thank you once again

[A]JumP[S] said...

This help me out. Thank you very much

Deva said...

Awesome man!

Unknown said...

Thanks a lot sir ur code is working perfectly
thanks
thanks
thanks
thanks
thanks
thanks
thanks
thanks
thankst
hanks

Unknown said...

Thank you very much sir
it helped me in completing my project lot.........

Mandip kanjiya said...

thanks buddy really thank u very much

anuragjha said...

i can get the from email from the form textbox and it defaults to the smtp account email i am sending it from

rohin said...

Thank U Very much

krish said...

GOt an error like this: SmtpException was unhandled by user code in the last line of the form mailClient.Send(MyMailMessage);
please reply me.....

Thanks in advance :)

DotNetGuts said...

@krish - SmtpException is very generalize statement, can you plz add more details? As per your comment, I would say try to see whether "MyMailMessage" is defined correctly.

Most Recent Post

Subscribe Blog via Email

Enter your email address:



Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape