Skip to content

Commit 9636c14

Browse files
committed
optimize code
1 parent 92c206f commit 9636c14

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

Src/SmtpServer/ISmtpServerOptions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ public interface ISmtpServerOptions
4444
int NetworkBufferSize { get; }
4545

4646
/// <summary>
47-
/// Gets the custom greeting message sent by the server in response to the initial SMTP connection.
48-
/// This message is returned after the client connects and before any commands are issued (e.g., "220 mail.example.com v1.0 ESMTP ready").
49-
/// If not set, a default greeting will be used.
47+
/// Gets the custom SMTP greeting message that the server sends immediately after a client connects,
48+
/// typically as the initial "220" response. The message can be dynamically generated based on the session context.
49+
/// If not set, a default greeting will be used (e.g., "220 mail.example.com ESMTP ready").
5050
/// </summary>
51-
string CustomGreetingMessage { get; }
51+
Func<ISessionContext, string> CustomSmtpGreeting { get; }
5252
}
5353
}

Src/SmtpServer/SmtpServerOptionsBuilder.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public ISmtpServerOptions Build()
2424
MaxAuthenticationAttempts = 3,
2525
NetworkBufferSize = 128,
2626
CommandWaitTimeout = TimeSpan.FromMinutes(5),
27-
CustomGreetingMessage = null,
27+
CustomSmtpGreeting = null,
2828
};
2929

3030
_setters.ForEach(setter => setter(serverOptions));
@@ -162,11 +162,15 @@ public SmtpServerOptionsBuilder CommandWaitTimeout(TimeSpan value)
162162
/// Sets the custom SMTP greeting message sent to the client upon connection,
163163
/// typically returned as the initial "220" response.
164164
/// </summary>
165-
/// <param name="value">The greeting message to send to the client (e.g., "220 mail.example.com v1.0 ESMTP ready").</param>
165+
/// <param name="smtpGreetingFunc">
166+
/// A delegate that returns the greeting message to send to the client,
167+
/// based on the <see cref="ISessionContext"/> (e.g., client IP, TLS state).
168+
/// Example: <c>ctx => $"220 {sessionContext.ServerOptions.ServerName} ESMTP ready"</c>
169+
/// </param>
166170
/// <returns>An OptionsBuilder to continue building on.</returns>
167-
public SmtpServerOptionsBuilder CustomGreetingMessage(string value)
171+
public SmtpServerOptionsBuilder CustomGreetingMessage(Func<ISessionContext, string> smtpGreetingFunc)
168172
{
169-
_setters.Add(options => options.CustomGreetingMessage = value);
173+
_setters.Add(options => options.CustomSmtpGreeting = smtpGreetingFunc);
170174

171175
return this;
172176
}
@@ -220,7 +224,7 @@ class SmtpServerOptions : ISmtpServerOptions
220224
/// This message is returned after the client connects and before any commands are issued (e.g., "220 mail.example.com v1.0 ESMTP ready").
221225
/// If not set, a default greeting will be used.
222226
/// </summary>
223-
public string CustomGreetingMessage { get; set; }
227+
public Func<ISessionContext, string> CustomSmtpGreeting { get; set; }
224228
}
225229

226230
#endregion

Src/SmtpServer/SmtpSession.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,14 @@ static async Task<bool> ExecuteAsync(SmtpCommand command, SmtpSessionContext con
189189
/// <returns>A task which performs the operation.</returns>
190190
ValueTask<FlushResult> OutputGreetingAsync(CancellationToken cancellationToken)
191191
{
192-
if (_context.ServerOptions.CustomGreetingMessage is null)
192+
if (_context.ServerOptions.CustomSmtpGreeting is null)
193193
{
194194
var serverVersion = AssemblyVersion;
195195
_context.Pipe.Output.WriteLine($"220 {_context.ServerOptions.ServerName} v{serverVersion} ESMTP ready");
196196
}
197197
else
198198
{
199-
_context.Pipe.Output.WriteLine($"220 {_context.ServerOptions.ServerName} {_context.ServerOptions.CustomGreetingMessage}");
199+
_context.Pipe.Output.WriteLine(_context.ServerOptions.CustomSmtpGreeting(_context));
200200
}
201201

202202
return _context.Pipe.Output.FlushAsync(cancellationToken);

0 commit comments

Comments
 (0)