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

SMTP should send the email if the SSL certificate is invalid #14444

Merged
merged 3 commits into from
Oct 19, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public override async Task<IDisplayResult> EditAsync(SmtpSettings settings, Buil
model.UseDefaultCredentials = settings.UseDefaultCredentials;
model.UserName = settings.UserName;
model.Password = settings.Password;
model.IgnoreInvalidSslCertificate = settings.IgnoreInvalidSslCertificate;
}).Location("Content:5").OnGroup(GroupId),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@
</div>
</div>

<div class="mb-3" asp-validation-class-for="IgnoreInvalidSslCertificate">
<div class="form-check">
<input type="checkbox" class="form-check-input" asp-for="IgnoreInvalidSslCertificate" />
<label class="form-check-label" asp-for="IgnoreInvalidSslCertificate">@T["Ignore Invalid SSL Certificate"]</label>
<span class="hint dashed">@T["Ignores SSL certificate check if it's invalid."]</span>
</div>
</div>

<script at="Foot">
$(function () {
function showSelectedCollapse() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public class SmtpSettings : IValidatableObject
/// </summary>
public int ProxyPort { get; set; }

/// <summary>
/// Gets or sets whether invalid SSL certificates should be ignored.
/// </summary>
public bool IgnoreInvalidSslCertificate { get; set; }

/// <inheritdocs />
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
Expand Down
58 changes: 29 additions & 29 deletions src/OrchardCore/OrchardCore.Email.Core/Services/SmtpService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,35 +239,6 @@ private MimeMessage FromMailMessage(MailMessage message, IList<LocalizedString>
return mimeMessage;
}

private bool CertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
const string LogErrorMessage = "SMTP Server's certificate {CertificateSubject} issued by {CertificateIssuer} " +
"with thumbprint {CertificateThumbprint} and expiration date {CertificateExpirationDate} " +
"is considered invalid with {SslPolicyErrors} policy errors";

if (sslPolicyErrors == SslPolicyErrors.None)
{
return true;
}

_logger.LogError(LogErrorMessage,
certificate.Subject,
certificate.Issuer,
certificate.GetCertHashString(),
certificate.GetExpirationDateString(),
sslPolicyErrors);

if (sslPolicyErrors.HasFlag(SslPolicyErrors.RemoteCertificateChainErrors) && chain?.ChainStatus != null)
{
foreach (var chainStatus in chain.ChainStatus)
{
_logger.LogError("Status: {Status} - {StatusInformation}", chainStatus.Status, chainStatus.StatusInformation);
}
}

return false;
}

protected virtual Task OnMessageSendingAsync(SmtpClient client, MimeMessage message) => Task.CompletedTask;

private async Task<string> SendOnlineMessageAsync(MimeMessage message)
Expand Down Expand Up @@ -323,5 +294,34 @@ private static Task SendOfflineMessageAsync(MimeMessage message, string pickupDi
var mailPath = Path.Combine(pickupDirectory, Guid.NewGuid().ToString() + EmailExtension);
return message.WriteToAsync(mailPath, CancellationToken.None);
}

private bool CertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
const string logErrorMessage = "SMTP Server's certificate {CertificateSubject} issued by {CertificateIssuer} " +
"with thumbprint {CertificateThumbprint} and expiration date {CertificateExpirationDate} " +
"is considered invalid with {SslPolicyErrors} policy errors";

if (sslPolicyErrors == SslPolicyErrors.None)
{
return true;
}

_logger.LogError(logErrorMessage,
certificate.Subject,
certificate.Issuer,
certificate.GetCertHashString(),
certificate.GetExpirationDateString(),
sslPolicyErrors);

if (sslPolicyErrors.HasFlag(SslPolicyErrors.RemoteCertificateChainErrors) && chain?.ChainStatus != null)
{
foreach (var chainStatus in chain.ChainStatus)
{
_logger.LogError("Status: {Status} - {StatusInformation}", chainStatus.Status, chainStatus.StatusInformation);
}
}

return _options.IgnoreInvalidSslCertificate;
}
}
}