Merge pull request #5516 from sbwalker/dev

add additional SSL connection options for SMTP
This commit is contained in:
Shaun Walker
2025-08-19 13:16:51 -04:00
committed by GitHub
3 changed files with 52 additions and 10 deletions

View File

@ -224,11 +224,14 @@
</div> </div>
</div> </div>
<div class="row mb-1 align-items-center"> <div class="row mb-1 align-items-center">
<Label Class="col-sm-3" For="smtpssl" HelpText="Specify if SSL is required for your SMTP server" ResourceKey="SmtpSSL">SSL Required: </Label> <Label Class="col-sm-3" For="smtpssl" HelpText="Specify the type of SSL connection for your SMTP server" ResourceKey="SmtpSSL">SSL Options: </Label>
<div class="col-sm-9"> <div class="col-sm-9">
<select id="smtpssl" class="form-select" @bind="@_smtpssl" > <select id="smtpssl" class="form-select" @bind="@_smtpssl">
<option value="True">@SharedLocalizer["Yes"]</option> <option value="None">@Localizer["None"]</option>
<option value="False">@SharedLocalizer["No"]</option> <option value="Auto">@Localizer["Auto"]</option>
<option value="StartTls">@Localizer["StartTls"]</option>
<option value="SslOnConnect">@Localizer["SslOnConnect"]</option>
<option value="StartTlsWhenAvailable">@Localizer["StartTlsWhenAvailable"]</option>
</select> </select>
</div> </div>
</div> </div>
@ -504,7 +507,7 @@
private string _smtpauthentication = "Basic"; private string _smtpauthentication = "Basic";
private string _smtphost = string.Empty; private string _smtphost = string.Empty;
private string _smtpport = string.Empty; private string _smtpport = string.Empty;
private string _smtpssl = "True"; private string _smtpssl = "Auto";
private string _smtpusername = string.Empty; private string _smtpusername = string.Empty;
private string _smtppassword = string.Empty; private string _smtppassword = string.Empty;
private string _smtppasswordtype = "password"; private string _smtppasswordtype = "password";
@ -613,7 +616,9 @@
{ {
_smtphost = SettingService.GetSetting(settings, "SMTPHost", string.Empty); _smtphost = SettingService.GetSetting(settings, "SMTPHost", string.Empty);
_smtpport = SettingService.GetSetting(settings, "SMTPPort", string.Empty); _smtpport = SettingService.GetSetting(settings, "SMTPPort", string.Empty);
_smtpssl = SettingService.GetSetting(settings, "SMTPSSL", "False"); _smtpssl = SettingService.GetSetting(settings, "SMTPSSL", "Auto");
if (_smtpssl == "True") _smtpssl = "SslOnConnect";
if (_smtpssl == "False") _smtpssl = "StartTlsWhenAvailable";
_smtpauthentication = SettingService.GetSetting(settings, "SMTPAuthentication", "Basic"); _smtpauthentication = SettingService.GetSetting(settings, "SMTPAuthentication", "Basic");
_smtpusername = SettingService.GetSetting(settings, "SMTPUsername", string.Empty); _smtpusername = SettingService.GetSetting(settings, "SMTPUsername", string.Empty);
_smtppassword = SettingService.GetSetting(settings, "SMTPPassword", string.Empty); _smtppassword = SettingService.GetSetting(settings, "SMTPPassword", string.Empty);

View File

@ -193,7 +193,7 @@
<value>Enter the port number for the SMTP server. Please note this field is required if you provide a host name.</value> <value>Enter the port number for the SMTP server. Please note this field is required if you provide a host name.</value>
</data> </data>
<data name="SmtpSSL.HelpText" xml:space="preserve"> <data name="SmtpSSL.HelpText" xml:space="preserve">
<value>Specify if SSL is required for your SMTP server</value> <value>Specify the type of SSL connection for your SMTP server</value>
</data> </data>
<data name="SmtpUsername.HelpText" xml:space="preserve"> <data name="SmtpUsername.HelpText" xml:space="preserve">
<value>Enter the username for your SMTP account</value> <value>Enter the username for your SMTP account</value>
@ -241,7 +241,7 @@
<value>Port: </value> <value>Port: </value>
</data> </data>
<data name="SmtpSSL.Text" xml:space="preserve"> <data name="SmtpSSL.Text" xml:space="preserve">
<value>SSL Required: </value> <value>SSL Options:</value>
</data> </data>
<data name="SmtpUsername.Text" xml:space="preserve"> <data name="SmtpUsername.Text" xml:space="preserve">
<value>Username: </value> <value>Username: </value>
@ -489,4 +489,19 @@
<data name="SmtpAuthority.HelpText" xml:space="preserve"> <data name="SmtpAuthority.HelpText" xml:space="preserve">
<value>The Authority Url for the SMTP provider</value> <value>The Authority Url for the SMTP provider</value>
</data> </data>
<data name="None" xml:space="preserve">
<value>None</value>
</data>
<data name="Auto" xml:space="preserve">
<value>Automatic</value>
</data>
<data name="StartTls" xml:space="preserve">
<value>Upgrade To TLS</value>
</data>
<data name="SslOnConnect" xml:space="preserve">
<value>Require SSL/TLS</value>
</data>
<data name="StartTlsWhenAvailable" xml:space="preserve">
<value>Use TLS When Available</value>
</data>
</root> </root>

View File

@ -81,9 +81,31 @@ namespace Oqtane.Infrastructure
// construct SMTP Client // construct SMTP Client
using var client = new SmtpClient(); using var client = new SmtpClient();
var secureSocketOptions = SecureSocketOptions.Auto;
switch (settingRepository.GetSettingValue(settings, "SMTPSSL", "Auto"))
{
case "None":
secureSocketOptions = SecureSocketOptions.None;
break;
case "Auto":
secureSocketOptions = SecureSocketOptions.Auto;
break;
case "StartTls":
secureSocketOptions = SecureSocketOptions.StartTls;
break;
case "SslOnConnect":
case "True": // legacy setting value
secureSocketOptions = SecureSocketOptions.SslOnConnect;
break;
case "StartTlsWhenAvailable":
case "False": // legacy setting value
secureSocketOptions = SecureSocketOptions.StartTlsWhenAvailable;
break;
}
await client.ConnectAsync(settingRepository.GetSettingValue(settings, "SMTPHost", ""), await client.ConnectAsync(settingRepository.GetSettingValue(settings, "SMTPHost", ""),
int.Parse(settingRepository.GetSettingValue(settings, "SMTPPort", "")), int.Parse(settingRepository.GetSettingValue(settings, "SMTPPort", "")),
bool.Parse(settingRepository.GetSettingValue(settings, "SMTPSSL", "False")) ? SecureSocketOptions.StartTls : SecureSocketOptions.None); secureSocketOptions);
if (settingRepository.GetSettingValue(settings, "SMTPAuthentication", "Basic") == "Basic") if (settingRepository.GetSettingValue(settings, "SMTPAuthentication", "Basic") == "Basic")
{ {