通过http代理发送邮件

发布于 2024-07-18 13:39:56 字数 132 浏览 8 评论 0原文

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

我正在使用 SmtpClient。

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

I'm trying to send emails from a system that connects to internet through a http proxy which is set in Internet Options.

i'm using SmtpClient.

Is there any way to send mails with SmtpClient through this proxy setting.
Thanks

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

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

发布评论

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

评论(4

青萝楚歌 2024-07-25 13:39:56

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

Http Proxies control http traffic, they rarely have anything to do with SMTP at all. I've never heard of proxying SMTP before after all SMTP itself is intrinsically supports a chain of "proxies" to the destination SMTP server.

分開簡單 2024-07-25 13:39:56

我知道您想使用浏览器的默认设置,我也想得到答案。

同时,您可以手动完成。

    MailAddress from = new MailAddress("[email protected]");
    MailAddress to = new MailAddress("[email protected]");

    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("[email protected]", "password");

    client.Send(mm);

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("[email protected]");
    MailAddress to = new MailAddress("[email protected]");

    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("[email protected]", "password");

    client.Send(mm);
镜花水月 2024-07-25 13:39:56

使用 MailKit

来自 Microsoft

重要

我们不建议您将 SmtpClient 类用于新的
开发,因为 SmtpClient 不支持许多现代协议。
请改用 MailKit 或其他库。 有关更多信息,请参阅
不应在 GitHub 上使用 SmtpClient。< /p>

创建一个控制台应用程序并添加 MailKit

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

代码以通过代理发送

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

var emailFromAddress = "[email protected]";
var token = "mytoken";
var to = "[email protected]";

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。

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 = "[email protected]";
var token = "mytoken";
var to = "[email protected]";

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

In this example, I've used Gmail to send the email. To do so you have to generate a token.
Go to your gmail > click on your icon on the very top right of the page > Manage your Google Account > from the menu on the left choose Security > half way down choose App Passwords > select Mail and select your device > press Generate > copy the token and replace mytoken above.

随心而道 2024-07-25 13:39:56

如果您对互联网的唯一访问是通过 HTTP,那么您能够做到这一点的唯一方法几乎是通过在端口 443 上设置带有 SSH 的 VPS(或同等设备)并使用 corkscrew(或 putty)来隧道 ssh 通过。 从这里开始,通过 ssh 隧道转发 smtp 流量就变得很简单。

请注意,如果您这样做,您可能会违反公司的计算政策。

If the only access you have to the internet is through HTTP, then pretty much the only way you'll be able to do this is by setting up a VPS (or equiv) with SSH on port 443 and using corkscrew (or putty) to tunnel ssh through. From there it is a simple matter to forward smtp traffic over your ssh tunnel.

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

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