Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Mail Proxy #11221

Merged
merged 2 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/OrchardCore/OrchardCore.Email.Abstractions/SmtpSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public class SmtpSettings : IValidatableObject
/// </summary>
public string Host { get; set; }


/// <summary>
/// Gets or sets the SMTP port number. Defaults to <c>25</c>.
/// </summary>
Expand Down Expand Up @@ -70,6 +69,16 @@ public class SmtpSettings : IValidatableObject
/// </summary>
public string Password { get; set; }

/// <summary>
/// Gets or sets the proxy server.
/// </summary>
public string ProxyHost { get; set; }

/// <summary>
/// Gets or sets the proxy port number.
/// </summary>
public int ProxyPort { get; set; }

/// <inheritdocs />
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
using MailKit.Net.Proxy;
using MailKit.Net.Smtp;
using MailKit.Security;
using Microsoft.Extensions.Localization;
Expand Down Expand Up @@ -235,8 +236,9 @@ private async Task<string> SendOnlineMessage(MimeMessage message)
using (var client = new SmtpClient())
{
client.ServerCertificateValidationCallback = CertificateValidationCallback;

await client.ConnectAsync(_options.Host, _options.Port, secureSocketOptions);
var useDefaultCredentials = _options.RequireCredentials && _options.UseDefaultCredentials;

if (_options.RequireCredentials)
{
if (_options.UseDefaultCredentials)
Expand All @@ -250,6 +252,11 @@ private async Task<string> SendOnlineMessage(MimeMessage message)
}
}

if (!String.IsNullOrEmpty( _options.ProxyHost))
{
client.ProxyClient = new Socks5Client(_options.ProxyHost, _options.ProxyPort);
}

var response = await client.SendAsync(message);

await client.DisconnectAsync(true);
Expand Down