Skip to content

Added LocalDomain for setting Mailkit parameter of same name. #149

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

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Directory.Version.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<PropertyGroup>
<VersionPrefix>4.1.0</VersionPrefix>
<VersionPrefix>4.1.1</VersionPrefix>
</PropertyGroup>
</Project>
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public static class LoggerConfigurationEmailExtensions
/// events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.</param>
/// <param name="levelSwitch">A switch allowing the pass-through minimum level
/// to be changed at runtime.</param>
/// <param name="localDomain">A string specifying the LocalDomain value for MailKit to use, if provided</param>
/// <returns>
/// Logger configuration, allowing configuration to continue.
/// </returns>
Expand All @@ -70,7 +71,8 @@ public static LoggerConfiguration Email(
string? body = null,
IFormatProvider? formatProvider = null,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
LoggingLevelSwitch? levelSwitch = null)
LoggingLevelSwitch? levelSwitch = null,
string? localDomain = null)
{
if (loggerConfiguration == null) throw new ArgumentNullException(nameof(loggerConfiguration));
if (from == null) throw new ArgumentNullException(nameof(from));
Expand All @@ -86,6 +88,7 @@ public static LoggerConfiguration Email(
ConnectionSecurity = connectionSecurity,
Credentials = credentials,
IsBodyHtml = false, // `MessageTemplateTextFormatter` cannot emit valid HTML; the `EmailSinkOptions` overload must be used for this.
LocalDomain = localDomain,
};

if (subject != null)
Expand Down
6 changes: 6 additions & 0 deletions src/Serilog.Sinks.Email/Sinks/Email/EmailSinkOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,10 @@ public EmailSinkOptions()
/// Provides a method that validates server certificates.
/// </summary>
public System.Net.Security.RemoteCertificateValidationCallback? ServerCertificateValidationCallback { get; set; }

/// <summary>
/// Provides a LocalDomain setting for MailKit in case a custom HELO needs to be used
/// </summary>
/// <value></value>
public string? LocalDomain {get; set;} = null;
}
5 changes: 5 additions & 0 deletions src/Serilog.Sinks.Email/Sinks/Email/MailKitEmailTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ SmtpClient OpenConnectedSmtpClient()
smtpClient.ServerCertificateValidationCallback += options.ServerCertificateValidationCallback;
}

if (!string.IsNullOrWhiteSpace(options.LocalDomain))
{
smtpClient.LocalDomain = options.LocalDomain;
}

smtpClient.Connect(options.Host, options.Port, options.ConnectionSecurity);

if (options.Credentials != null)
Expand Down
4 changes: 3 additions & 1 deletion test/Serilog.Sinks.Email.Tests/EmailSinkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,11 @@ public void WorksWithIBatchTextFormatter()
var sink = new EmailSink(emailConnectionInfo, emailTransport);

using (var emailLogger = new LoggerConfiguration()
.WriteTo.Sink(sink, new BatchingOptions())
.WriteTo.Sink(sink, new BatchingOptions() { EagerlyEmitFirstEvent = false })
.CreateLogger())
{
// The EagerlyEmitFirstEvent may occur fast enough that we get multiple sends (linux, especially),
// set it false
emailLogger.Information("Information");
emailLogger.Warning("Warning");
emailLogger.Error("<Error>");
Expand Down