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

发布于 2024-08-29 10:30:15 字数 1171 浏览 1 评论 0原文

尝试在我的 C# 应用程序中发送一些电子邮件。我位于代理后面 - 这无疑是代码无法工作的原因。这是我到目前为止所拥有的:

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>

代码

        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);
        }  

每次我收到 System.Net.WebException:无法解析远程名称:'smtp.gmail。 com'

我从哪里/如何开始调试?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷爱 2024-09-05 10:30:15

要调试涉及客户端服务器的任何内容,telnet 是您的朋友。

尝试进入 DOS 并输入:

  telnet smtp.gmail.com 587

您应该看到:

  220 mx.google.com ESMTP 20sm950596pzk.3

如果您不这样做(您会收到“无法连接”或类似的信息),那么您肯定被阻止了。

如果尚未安装,您可以从“Windows 组件”下的添加/删除程序安装 telnet。

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.

笑饮青盏花 2024-09-05 10:30:15

您是对的,在代理后面会阻止您的代码工作。解决方案并不那么简单。据我所知,没有标准的“SMTP 代理”(就像 HTTP 代理一样)。您必须使用 SOCKS 代理并为其找到一些 .NET 客户端 - .NET 框架中没有这样的客户端,但如果您在 google 上搜索“.NET SOCKS 代理”,您应该能够找到一个。

不过,您的网络不太可能运行 SOCKS 代理,因此您很可能不得不放弃这一点,而只使用本地 SMTP 服务器。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文