Sending email through proxy using gmail smtp

心不动则不痛 提交于 2019-12-20 01:45:59

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/2642894/sending-email-through-proxy-using-gmail-smtp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!