Skip to content

Commit 113b65e

Browse files
committed
Based on feedback we changed the default port back to 587 and added an encryption setting
Thanks @caesay :)
1 parent 49fbe55 commit 113b65e

File tree

4 files changed

+52
-22
lines changed

4 files changed

+52
-22
lines changed

src/Exceptionless.Api/Web.config

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@
2222
<!-- Controls whether daily summary emails are sent -->
2323
<add key="EnableDailySummary" value="false" />
2424

25-
<!-- Email Client Settings (Uncomment the section below to configure email settings) -->
2625
<!--
27-
<add key="SmtpHost" value="localhost" />
28-
<add key="SmtpPort" value="25" />
29-
<add key="SmtpEnableSSL" value="false" />
30-
<add key="SmtpUser" value="" />
26+
Email Client Settings (Uncomment the section below to change the default email settings)
27+
There are three valid SmtpEncryption settings: None, SSL and StartTLS
28+
-->
29+
<!--
30+
<add key="SmtpHost" value="domain.com" />
31+
<add key="SmtpPort" value="587" />
32+
<add key="SmtpEncryption" value="StartTLS" />
33+
<add key="SmtpUser" value="user@domain.com" />
3134
<add key="SmtpPassword" value="" />
3235
-->
3336

src/Exceptionless.Core/Settings.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public class Settings : SettingsBase<Settings> {
140140

141141
public int SmtpPort { get; private set; }
142142

143-
public bool SmtpEnableSSL { get; private set; }
143+
public SmtpEncryption SmtpEncryption { get; private set; }
144144

145145
public string SmtpUser { get; private set; }
146146

@@ -203,8 +203,8 @@ public override void Initialize() {
203203
TestEmailAddress = GetString(nameof(TestEmailAddress), "noreply@exceptionless.io");
204204
SmtpFrom = GetString(nameof(SmtpFrom), "Exceptionless <noreply@exceptionless.io>");
205205
SmtpHost = GetString(nameof(SmtpHost), "localhost");
206-
SmtpPort = GetInt(nameof(SmtpPort), String.Equals(SmtpHost, "localhost") ? 25 : 465);
207-
SmtpEnableSSL = GetBool(nameof(SmtpEnableSSL), !String.Equals(SmtpHost, "localhost"));
206+
SmtpPort = GetInt(nameof(SmtpPort), String.Equals(SmtpHost, "localhost") ? 25 : 587);
207+
SmtpEncryption = GetEnum<SmtpEncryption>(nameof(SmtpEncryption), GetDefaultSmtpEncryption(SmtpPort));
208208
SmtpUser = GetString(nameof(SmtpUser));
209209
SmtpPassword = GetString(nameof(SmtpPassword));
210210

@@ -232,8 +232,21 @@ public override void Initialize() {
232232
Version = FileVersionInfo.GetVersionInfo(typeof(Settings).Assembly.Location).ProductVersion;
233233
}
234234

235+
private SmtpEncryption GetDefaultSmtpEncryption(int port) {
236+
switch (port) {
237+
case 465:
238+
return SmtpEncryption.SSL;
239+
case 587:
240+
case 2525:
241+
return SmtpEncryption.StartTLS;
242+
default:
243+
return SmtpEncryption.None;
244+
}
245+
}
246+
235247
public const string JobBootstrappedServiceProvider = "Exceptionless.Insulation.Jobs.JobBootstrappedServiceProvider,Exceptionless.Insulation";
236248

249+
237250
public LoggerFactory GetLoggerFactory() {
238251
return new LoggerFactory();
239252
}
@@ -244,4 +257,10 @@ public enum WebsiteMode {
244257
QA,
245258
Dev
246259
}
247-
}
260+
261+
public enum SmtpEncryption {
262+
None,
263+
StartTLS,
264+
SSL
265+
}
266+
}

src/Exceptionless.Insulation/Mail/MailKitMailSender.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@
66
using Exceptionless.Core.Mail;
77
using Foundatio.Utility;
88
using MailKit.Net.Smtp;
9+
using MailKit.Security;
910
using MimeKit;
1011
using MailMessage = Exceptionless.Core.Queues.Models.MailMessage;
1112

1213
namespace Exceptionless.Insulation.Mail {
1314
public class MailKitMailSender : IMailSender {
14-
private long _totalSent;
15-
16-
public long TotalSent => _totalSent;
17-
1815
public async Task SendAsync(MailMessage model) {
1916
var message = CreateMailMessage(model);
2017
message.Headers.Add("X-Mailer-Machine", Environment.MachineName);
@@ -25,7 +22,7 @@ public async Task SendAsync(MailMessage model) {
2522
using (var client = new SmtpClient()) {
2623
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
2724

28-
await client.ConnectAsync(Settings.Current.SmtpHost, Settings.Current.SmtpPort, Settings.Current.SmtpEnableSSL).AnyContext();
25+
await client.ConnectAsync(Settings.Current.SmtpHost, Settings.Current.SmtpPort, GetSecureSocketOption(Settings.Current.SmtpEncryption)).AnyContext();
2926

3027
// Note: since we don't have an OAuth2 token, disable the XOAUTH2 authentication mechanism.
3128
client.AuthenticationMechanisms.Remove("XOAUTH2");
@@ -34,13 +31,21 @@ public async Task SendAsync(MailMessage model) {
3431
await client.AuthenticateAsync(Settings.Current.SmtpUser, Settings.Current.SmtpPassword).AnyContext();
3532

3633
await client.SendAsync(message).AnyContext();
37-
38-
// we don't care if there is an error at this point.
39-
Interlocked.Increment(ref _totalSent);
4034
await client.DisconnectAsync(true).AnyContext();
4135
}
4236
}
4337

38+
private SecureSocketOptions GetSecureSocketOption(SmtpEncryption encryption) {
39+
switch (encryption) {
40+
case SmtpEncryption.StartTLS:
41+
return SecureSocketOptions.StartTls;
42+
case SmtpEncryption.SSL:
43+
return SecureSocketOptions.SslOnConnect;
44+
default:
45+
return SecureSocketOptions.Auto;
46+
}
47+
}
48+
4449
private MimeMessage CreateMailMessage(MailMessage notification) {
4550
var message = new MimeMessage { Subject = notification.Subject };
4651
var builder = new BodyBuilder();

src/Jobs/App.config

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@
1919
<!-- Controls whether daily summary emails are sent -->
2020
<add key="EnableDailySummary" value="false" />
2121

22-
<!-- Email Client Settings (Uncomment the section below to configure email settings) -->
2322
<!--
24-
<add key="SmtpHost" value="localhost" />
25-
<add key="SmtpPort" value="25" />
26-
<add key="SmtpEnableSSL" value="false" />
27-
<add key="SmtpUser" value="" />
23+
Email Client Settings (Uncomment the section below to change the default email settings)
24+
There are three valid SmtpEncryption settings: None, SSL and StartTLS
25+
-->
26+
<!--
27+
<add key="SmtpHost" value="domain.com" />
28+
<add key="SmtpPort" value="587" />
29+
<add key="SmtpEncryption" value="StartTLS" />
30+
<add key="SmtpUser" value="user@domain.com" />
2831
<add key="SmtpPassword" value="" />
2932
-->
3033

0 commit comments

Comments
 (0)