8

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

Ben Collins
  • 20,538
  • 18
  • 127
  • 187
Salar
  • 495
  • 3
  • 6
  • 14

4 Answers4

7

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.

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
  • You could proxy SMTP to say a spam filter. Where the spam filter would evaluate the email and forward it on to the SMTP server. – Chuck Conway May 10 '09 at 07:14
  • @Charles: Like I said in the answer STMP is intrinsically a series of "proxies" to the destination. A spam filter is simply another SMTP server in the chain. – AnthonyWJones May 10 '09 at 08:14
  • I believe it is possible. Clever components implemented one: http://www.clevercomponents.com/products/inetsuitenet/smtpclientnet.asp but it doesn't seem worth buying it – Salar May 10 '09 at 10:06
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);
bahith
  • 441
  • 10
  • 18
3

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.

Robert Brooker
  • 2,148
  • 24
  • 22
0

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.

Luke Chadwick
  • 1,648
  • 14
  • 24