Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending email through proxy using gmail smtp

Trying to send some email in my C# app. I am behind a proxy - which is no doubt why the code isn't working. This is what I have so far:

App.Config:

<system.net>
    <defaultProxy enabled="false">
      <proxy proxyaddress="xxx.xxx.xxx.xxx"/>
    </defaultProxy>
    <mailSettings>
      <smtp deliveryMethod="Network">
        <network host="smtp.gmail.com" port="587"/>
      </smtp>
    </mailSettings>
  </system.net>

Code:

        var username = "...";
        var password = "...";

        var fromEmail = "...";
        var toEmail = "...";
        var body = "Test email body";
        var subject = "Test Subject Email";

        var client = new SmtpClient("smtp.gmail.com", 587)
        {
            Credentials = new NetworkCredential(username, password),
            EnableSsl = true
        };

        try
        {
            client.Send(fromEmail, toEmail, subject, body);
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }  

Everytime I get System.Net.WebException: The remote name could not be resolved: 'smtp.gmail.com'

Where/how do I start to debug?

like image 856
baron Avatar asked Nov 24 '25 15:11

baron


2 Answers

To debug anything involving client server, telnet is your friend.

Try dropping to DOS and typing:

  telnet smtp.gmail.com 587

You should see:

  220 mx.google.com ESMTP 20sm950596pzk.3

If you don't (you get a "cannot connect" or some such), you're definitely being blocked.

You can install telnet from your add/remove programs under 'windows components', if you don't have it installed.

like image 199
Eddie Parker Avatar answered Nov 26 '25 04:11

Eddie Parker


You're correct that being behind a proxy would prevent your code from working. The solution is not so simple. There is no standard "SMTP proxy" that I'm aware of (the way that there are HTTP proxies). You would have to use a SOCKS proxy and find some .NET client for it - there isn't one in the .NET framework, but if you google ".NET SOCKS proxy" you should be able to find one.

It's fairly unlikely that your network is running a SOCKS proxy, though, so you might well have to give up on this and just use the local SMTP server.

like image 30
EMP Avatar answered Nov 26 '25 05:11

EMP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!