简体   繁体   中英

Sending mail through http proxy

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

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.

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

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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