8

我正在尝试从通过 Internet 选项中设置的 http 代理连接到 Internet 的系统发送电子邮件。

我正在使用 SmtpClient。

有没有办法通过这个代理设置使用 SmtpClient 发送邮件。谢谢

4

4 回答 4

7

Http 代理控制 http 流量,它们与 SMTP 几乎没有任何关系。我之前从未听说过代理 SMTP,毕竟 SMTP 本身本质上支持到目标 SMTP 服务器的“代理”链。

于 2009-05-10T07:08:07.127 回答
4

I understand that you want to use the browsers default settings, i would also like an answer for that.

Meanwhile, you could do it manually.

    MailAddress from = new MailAddress("from@mailserver.com");
    MailAddress to = new MailAddress("to@mailserver.com");

    MailMessage mm = new MailMessage(from, to);
    mm.Subject = "Subject"
    mm.Body = "Body";

    SmtpClient client = new SmtpClient("proxy.mailserver.com", 8080);
    client.Credentials = new System.Net.NetworkCredential("from@mailserver.com", "password");

    client.Send(mm);
于 2009-05-10T07:33:51.590 回答
1

如果您对 Internet 的唯一访问是通过 HTTP,那么您能够做到这一点的唯一方法是在端口 443 上设置一个带有 SSH 的 VPS(或等效)并使用开瓶器(或腻子)隧道 ssh 通过。从那里通过 ssh 隧道转发 smtp 流量是一件简单的事情。

Be aware that you may be violating the companies computing policy if you do this.

于 2009-05-10T07:20:17.533 回答
0

Use MailKit

From Microsoft:

Important

We don't recommend that you use the SmtpClient class for new development because SmtpClient doesn't support many modern protocols. Use MailKit or other libraries instead. For more information, see SmtpClient shouldn't be used on GitHub.

Create a console app and add MailKit

dotnet new console --framework net6.0
dotnet add package MailKit

Code to send through proxy

using MailKit.Net.Proxy;
using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;

var emailFromAddress = "myemail@gmail.com";
var token = "mytoken";
var to = "Someone.Else@gmail.com";

var message = new MimeMessage();
message.From.Add(new MailboxAddress("Me", emailFromAddress));
message.To.Add(MailboxAddress.Parse(to));
message.Subject = "test";

message.Body = new TextPart("plain")
{
    Text = @"This is a test."
};

using (var client = new SmtpClient())
{
    client.ProxyClient = new HttpProxyClient("my-proxy.mydomain.com", 80);   // <-- set proxy
    client.Connect("smtp.gmail.com", 587, SecureSocketOptions.StartTls);
    client.Authenticate(emailFromAddress, token);

    client.Send(message);
    client.Disconnect(true);
}

在此示例中,我使用 Gmail 发送电子邮件。为此,您必须生成一个令牌。转到您的gmail > 单击页面右上角的图标 >管理您的 Google 帐户> 从左侧菜单中选择安全性> 中途选择应用程序密码> 选择邮件并选择您的设备 > 按生成> 复制令牌并替换上面的 mytoken。

于 2021-12-04T05:05:11.830 回答