Skip to content

Commit 259ce91

Browse files
Arthur Chiaarthurchiakh
authored andcommitted
Open MailKit secure socket option setting
1 parent 5fb82c6 commit 259ce91

File tree

3 files changed

+64
-13
lines changed

3 files changed

+64
-13
lines changed

src/Serilog.Sinks.Email/Sinks/Email/EmailConnectionInfo.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Copyright 2014 Serilog Contributors
2-
//
2+
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
55
// You may obtain a copy of the License at
6-
//
6+
//
77
// http://www.apache.org/licenses/LICENSE-2.0
8-
//
8+
//
99
// Unless required by applicable law or agreed to in writing, software
1010
// distributed under the License is distributed on an "AS IS" BASIS,
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -14,6 +14,9 @@
1414

1515
using System.ComponentModel;
1616
using System.Net;
17+
#if MAIL_KIT
18+
using Serilog.Sinks.Email.Sinks.Email;
19+
#endif
1720

1821
namespace Serilog.Sinks.Email
1922
{
@@ -80,7 +83,7 @@ public EmailConnectionInfo()
8083
/// </summary>
8184
/// <remarks>
8285
/// This only works on `netstandard1.3` with `MailKit`. If you
83-
/// are targeting `net45`+, you should add your validation to
86+
/// are targeting `net45`+, you should add your validation to
8487
/// `System.Net.ServicePointManager.ServerCertificateValidationCallback`
8588
/// manually.
8689
/// </remarks>
@@ -95,5 +98,12 @@ public EmailConnectionInfo()
9598
/// Sets whether the body contents of the email is HTML. Defaults to false.
9699
/// </summary>
97100
public bool IsBodyHtml { get; set; }
101+
102+
#if MAIL_KIT
103+
/// <summary>
104+
/// Sets secure socket option for MailKit
105+
/// </summary>
106+
public MailKitSecureSocketOptions? MailKitSecureSocketOption { get; set; }
107+
#endif
98108
}
99109
}

src/Serilog.Sinks.Email/Sinks/Email/MailKitEmailSink.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Copyright 2014 Serilog Contributors
2-
//
2+
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
55
// You may obtain a copy of the License at
6-
//
6+
//
77
// http://www.apache.org/licenses/LICENSE-2.0
8-
//
8+
//
99
// Unless required by applicable law or agreed to in writing, software
1010
// distributed under the License is distributed on an "AS IS" BASIS,
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -25,6 +25,7 @@
2525
using Serilog.Sinks.PeriodicBatching;
2626
using MailKit.Net.Smtp;
2727
using System.Threading.Tasks;
28+
using MailKit.Security;
2829

2930
namespace Serilog.Sinks.Email
3031
{
@@ -61,7 +62,7 @@ public EmailSink(EmailConnectionInfo connectionInfo, ITextFormatter textFormatte
6162
_textFormatter = textFormatter;
6263
_subjectFormatter = subjectLineFormatter;
6364
}
64-
65+
6566
private MimeKit.MimeMessage CreateMailMessage(string payload, string subject)
6667
{
6768
var mailMessage = new MimeKit.MimeMessage();
@@ -71,7 +72,7 @@ private MimeKit.MimeMessage CreateMailMessage(string payload, string subject)
7172
mailMessage.Body = _connectionInfo.IsBodyHtml
7273
? new MimeKit.BodyBuilder { HtmlBody = payload }.ToMessageBody()
7374
: new MimeKit.BodyBuilder { TextBody = payload }.ToMessageBody();
74-
return mailMessage;
75+
return mailMessage;
7576
}
7677

7778
/// <summary>
@@ -126,9 +127,13 @@ private SmtpClient OpenConnectedSmtpClient()
126127
smtpClient.ServerCertificateValidationCallback += _connectionInfo.ServerCertificateValidationCallback;
127128
}
128129

129-
smtpClient.Connect(
130-
_connectionInfo.MailServer, _connectionInfo.Port,
131-
useSsl: _connectionInfo.EnableSsl);
130+
if (_connectionInfo.MailKitSecureSocketOption.HasValue)
131+
smtpClient.Connect(_connectionInfo.MailServer, _connectionInfo.Port,
132+
(SecureSocketOptions) _connectionInfo.MailKitSecureSocketOption.Value);
133+
else
134+
smtpClient.Connect(
135+
_connectionInfo.MailServer, _connectionInfo.Port,
136+
useSsl: _connectionInfo.EnableSsl);
132137

133138
if (_connectionInfo.NetworkCredentials != null)
134139
{
@@ -142,4 +147,4 @@ private SmtpClient OpenConnectedSmtpClient()
142147
}
143148
}
144149
}
145-
#endif
150+
#endif
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace Serilog.Sinks.Email.Sinks.Email
2+
{
3+
public enum MailKitSecureSocketOptions
4+
{
5+
//
6+
// Summary:
7+
// No SSL or TLS encryption should be used.
8+
None = 0,
9+
10+
//
11+
// Summary:
12+
// Allow the MailKit.IMailService to decide which SSL or TLS options to use (default).
13+
// If the server does not support SSL or TLS, then the connection will continue
14+
// without any encryption.
15+
Auto = 1,
16+
17+
//
18+
// Summary:
19+
// The connection should use SSL or TLS encryption immediately.
20+
SslOnConnect = 2,
21+
22+
//
23+
// Summary:
24+
// Elevates the connection to use TLS encryption immediately after reading the greeting
25+
// and capabilities of the server. If the server does not support the STARTTLS extension,
26+
// then the connection will fail and a System.NotSupportedException will be thrown.
27+
StartTls = 3,
28+
29+
//
30+
// Summary:
31+
// Elevates the connection to use TLS encryption immediately after reading the greeting
32+
// and capabilities of the server, but only if the server supports the STARTTLS
33+
// extension.
34+
StartTlsWhenAvailable = 4
35+
}
36+
}

0 commit comments

Comments
 (0)