Search code examples
c#.netgmail

GMail SMTP via C# .Net errors on all ports


I've been trying for a whlie on this, and have so far been failing miserably. My most recent attempt was lifted from this stack code here: Sending email through Gmail SMTP server with C#, but I've tried all the syntax I could find here on stack and elsewhere. My code currently is:

var client = new SmtpClient("smtp.gmail.com", 587)
{
    Credentials = new NetworkCredential("[email protected]", "mypass"),
    EnableSsl = true
};

client.Send("[email protected]","[email protected]","Test", "test message");

Running that code gives me an immediate exception "Failure sending mail" that has an innerexeption "unable to connect to the remote server".

If I change the port to 465 (as gmail docs suggest), I get a timeout every time.

I've read that 465 isn't a good port to use, so I'm wondering what the deal is w/ 587 giving me failure to connect. My user and pass are right. I've read that I have to have POP service setup on my gmail account, so I did that. No avail.

I was originally trying to get this working for my branded GMail account, but after running into the same problems w/ that I thought going w/ my regular gmail account would be easier... so far that's not the case.


Solution

  • I tried your code, and it works prefectly with port 587, but not with 465.

    Have you checked the fire wall? Try from the command line "Telnet smtp.gmail.com 587" If you get "220 mx.google.com ESMTP...." back, then the port is open. If not, it is something that blocks you call.

    Daniel