开发者

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>

开发者_如何学CCode:

        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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜