Sending mail through http proxy
Asked Answered
U

4

9

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

Unijugate answered 10/5, 2009 at 6:40 Comment(1)
@Anthony, I believe he is referring the SmtpClient class.Amberjack
D
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("[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);
Dense answered 10/5, 2009 at 7:33 Comment(0)
S
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.

Selinaselinda answered 10/5, 2009 at 7:8 Comment(3)
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.Amberjack
@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.Selinaselinda
I believe it is possible. Clever components implemented one: clevercomponents.com/products/inetsuitenet/smtpclientnet.asp but it doesn't seem worth buying itUnijugate
D
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("[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);
Dense answered 10/5, 2009 at 7:33 Comment(0)
T
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 = "[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.

Thrum answered 4/12, 2021 at 5:5 Comment(0)
S
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.

Strasser answered 10/5, 2009 at 7:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.