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
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
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);
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("[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);
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.
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.
© 2022 - 2024 — McMap. All rights reserved.