Skip to content

Commit 8ba6f29

Browse files
committed
Refactor SmtpSession to use predefined SmtpResponse constants
Replaces inline SmtpResponse instantiations in SmtpSession with static predefined response constants from SmtpResponse. Adds new static response properties for common SMTP replies related to TLS, authentication, and recipient/sender validation, improving code clarity and maintainability.
1 parent 2c2caf2 commit 8ba6f29

2 files changed

Lines changed: 32 additions & 7 deletions

File tree

src/Zetian/Internal/SmtpSession.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ private async Task ProcessMailFromAsync(SmtpCommand command)
313313
bool canAccept = await _configuration.MailboxFilter.CanAcceptFromAsync(this, mailFrom, messageSize).ConfigureAwait(false);
314314
if (!canAccept)
315315
{
316-
await SendResponseAsync(new SmtpResponse(550, "Sender rejected")).ConfigureAwait(false);
316+
await SendResponseAsync(SmtpResponse.SenderRejected).ConfigureAwait(false);
317317
return;
318318
}
319319
}
@@ -350,7 +350,7 @@ private async Task ProcessRcptToAsync(SmtpCommand command)
350350

351351
if (_recipients.Count >= _configuration.MaxRecipients)
352352
{
353-
await SendResponseAsync(new SmtpResponse(452, "Too many recipients")).ConfigureAwait(false);
353+
await SendResponseAsync(SmtpResponse.TooManyRecipients).ConfigureAwait(false);
354354
return;
355355
}
356356

@@ -360,7 +360,7 @@ private async Task ProcessRcptToAsync(SmtpCommand command)
360360
bool canDeliver = await _configuration.MailboxFilter.CanDeliverToAsync(this, rcptTo, _mailFrom ?? string.Empty).ConfigureAwait(false);
361361
if (!canDeliver)
362362
{
363-
await SendResponseAsync(new SmtpResponse(550, "Recipient rejected")).ConfigureAwait(false);
363+
await SendResponseAsync(SmtpResponse.RecipientRejected).ConfigureAwait(false);
364364
return;
365365
}
366366
}
@@ -517,7 +517,7 @@ private async Task ProcessStartTlsAsync(CancellationToken cancellationToken)
517517
return;
518518
}
519519

520-
await SendResponseAsync(new SmtpResponse(220, "Ready to start TLS")).ConfigureAwait(false);
520+
await SendResponseAsync(SmtpResponse.ReadyToStartTls).ConfigureAwait(false);
521521

522522
try
523523
{
@@ -557,7 +557,7 @@ private async Task ProcessAuthAsync(SmtpCommand command, CancellationToken cance
557557

558558
if (!IsSecure && !_configuration.AllowPlainTextAuthentication)
559559
{
560-
await SendResponseAsync(new SmtpResponse(538, "Encryption required for authentication")).ConfigureAwait(false);
560+
await SendResponseAsync(SmtpResponse.EncryptionRequiredForAuth).ConfigureAwait(false);
561561
return;
562562
}
563563

@@ -572,7 +572,7 @@ private async Task ProcessAuthAsync(SmtpCommand command, CancellationToken cance
572572

573573
if (!_configuration.AuthenticationMechanisms.Contains(mechanism))
574574
{
575-
await SendResponseAsync(new SmtpResponse(504, "Authentication mechanism not supported")).ConfigureAwait(false);
575+
await SendResponseAsync(SmtpResponse.AuthMechanismNotSupported).ConfigureAwait(false);
576576
return;
577577
}
578578

@@ -590,7 +590,7 @@ private async Task ProcessAuthAsync(SmtpCommand command, CancellationToken cance
590590
{
591591
IsAuthenticated = true;
592592
AuthenticatedIdentity = result.Identity;
593-
await SendResponseAsync(new SmtpResponse(235, "Authentication successful")).ConfigureAwait(false);
593+
await SendResponseAsync(SmtpResponse.AuthenticationSuccessful).ConfigureAwait(false);
594594
}
595595
else
596596
{

src/Zetian/Protocol/SmtpResponse.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,31 @@ private static string GetDefaultMessage(int code)
248248
/// <summary>554 Transaction failed</summary>
249249
public static readonly SmtpResponse TransactionFailed = new(554, "Transaction failed");
250250

251+
#region TLS and Authentication Responses
252+
253+
/// <summary>220 Ready to start TLS</summary>
254+
public static readonly SmtpResponse ReadyToStartTls = new(220, "Ready to start TLS");
255+
256+
/// <summary>235 Authentication successful</summary>
257+
public static readonly SmtpResponse AuthenticationSuccessful = new(235, "Authentication successful");
258+
259+
/// <summary>452 Too many recipients</summary>
260+
public static readonly SmtpResponse TooManyRecipients = new(452, "Too many recipients");
261+
262+
/// <summary>504 Authentication mechanism not supported</summary>
263+
public static readonly SmtpResponse AuthMechanismNotSupported = new(504, "Authentication mechanism not supported");
264+
265+
/// <summary>538 Encryption required for authentication</summary>
266+
public static readonly SmtpResponse EncryptionRequiredForAuth = new(538, "Encryption required for authentication");
267+
268+
/// <summary>550 Sender rejected</summary>
269+
public static readonly SmtpResponse SenderRejected = new(550, "Sender rejected");
270+
271+
/// <summary>550 Recipient rejected</summary>
272+
public static readonly SmtpResponse RecipientRejected = new(550, "Recipient rejected");
273+
274+
#endregion
275+
251276
#endregion
252277
}
253278
}

0 commit comments

Comments
 (0)