Sending email through the gmail SMTP server (SSL)

I know that there are many examples of how to send an email using C #, but I really run into some problems and I cannot get it to work.

I always get the error "Failure sending mail" , Unable to connect to the remote server - No connection could be made because the active machine actively refused it (IP address here) .

What does this error mean? And how to fix it?

Any help would be greatly appreciated

Here is the code I used: (although I already did a lot of things)

 string SendersAddress = " test@gmail.com "; string ReceiversAddress = " test1@gmail.com "; const string SendersPassword = "test-pass-here"; const string subject = "Testing"; const string body = "Hi This Is my Mail From Gmail"; try { SmtpClient smtp = new SmtpClient { Host = "smtp.gmail.com", Port = 587, EnableSsl = true, DeliveryMethod = SmtpDeliveryMethod.Network, Credentials = new NetworkCredential(SendersAddress, SendersPassword), Timeout = 3000 }; MailMessage message = new MailMessage(SendersAddress, ReceiversAddress, subject, body); smtp.Send(message); } catch (Exception ex) { } 

Thanks!

+4
source share
4 answers

It looks like you are facing problems with the SMTP server.

Make sure the firewall is open on port 25, and you actually have an SMTP server, wherever you try to establish a connection.

+2
source

You are trying to establish an SSL connection with gmail on port 587 . This port should be used with TLS, not SSL.

Use port 465 instead.

Also note that the Timeout property is expressed in milliseconds, so 3000 may be a little short, depending on your network. Try using a more resolving value, such as 30000 .

+2
source

It is also worth checking that your antivirus is not blocking port 25, I have been caught this before!

+1
source

The same thing for me in the last few days,

A simple solution:

First check if everything is ok with Telnet , and then try to do the same in C #. Here is a good introduction: http://www.wikihow.com/Send-Email-Using-Telnet .

Just beware that some servers use the EHLO instead of HELO

EDIT:

take a look at how to connect to the Google SMTP server here

0
source

All Articles