简体   繁体   English

使用gmail smtp通过代理发送电子邮件

[英]Sending email through proxy using gmail smtp

Trying to send some email in my C# app. 试图在我的C#应用​​程序中发送一些电子邮件。 I am behind a proxy - which is no doubt why the code isn't working. 我支持代理 - 这无疑是代码无效的原因。 This is what I have so far: 这是我到目前为止:

App.Config : 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' 每次我得到System.Net.WebException:无法解析远程名称:'smtp.gmail.com'

Where/how do I start to debug? 我在哪里/如何开始调试?

To debug anything involving client server, telnet is your friend. 要调试涉及客户端服务器的任何内容,telnet是您的朋友。

Try dropping to DOS and typing: 尝试下降到DOS并输入:

  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. 如果您没有安装telnet,可以在'windows components'下的添加/删除程序中安装telnet。

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). 没有我所知道的标准“SMTP代理”(有HTTP代理的方式)。 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. 您将不得不使用SOCKS代理并为其找到一些.NET客户端 - 在.NET框架中没有一个,但如果您谷歌“.NET SOCKS代理”,您应该能够找到一个。

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. 但是,您的网络运行SOCKS代理的可能性相当小,因此您可能不得不放弃这一点,只需使用本地SMTP服务器即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM