Fix SmtpException.GetMessageForStatus: use distinct messages for unknown and defined-but-unmapped status codes#126602
Open
Fix SmtpException.GetMessageForStatus: use distinct messages for unknown and defined-but-unmapped status codes#126602
Conversation
Contributor
|
Tagging subscribers to this area: @dotnet/runtime-infrastructure |
…t message for unknown status codes Separates the `default` case from `SmtpStatusCode.CommandUnrecognized` in the `GetMessageForStatus` switch, so unknown/undefined status codes produce a generic "Unknown SMTP status code." message rather than the misleading "Syntax error, command unrecognized." message specific to SMTP 500. Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/a9d8afb8-cec0-448d-bc19-1f9b6bfe5b8f Co-authored-by: rzikm <32671551+rzikm@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix misleading message for unknown SmtpStatusCode
Fix SmtpException.GetMessageForStatus default case: use distinct message for unknown status codes
Apr 7, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Updates System.Net.Mail’s SmtpException status-code-to-message mapping so undefined/unknown SmtpStatusCode values no longer report the misleading “command unrecognized” (SMTP 500) message.
Changes:
- Added a new localized resource string for unknown SMTP status codes.
- Updated
SmtpException.GetMessageForStatusto return the new resource from thedefaultswitch case. - Extended the functional test to assert unknown status codes don’t share the
CommandUnrecognizedmessage.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/libraries/System.Net.Mail/src/System/Net/Mail/SmtpException.cs | Changes the default message mapping for unrecognized status codes. |
| src/libraries/System.Net.Mail/src/Resources/Strings.resx | Adds SmtpUnknownStatusCode resource string. |
| src/libraries/System.Net.Mail/tests/Functional/SmtpExceptionTest.cs | Adds assertion ensuring unknown status codes produce a distinct message. |
…GeneralFailure All defined SmtpStatusCode enum members now have explicit case labels in GetMessageForStatus so the `default` case exclusively handles truly undefined/unknown status code values. Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/529c4574-2800-472e-93e6-73c1439e519c Co-authored-by: rzikm <32671551+rzikm@users.noreply.github.com>
Copilot
AI
changed the title
Fix SmtpException.GetMessageForStatus default case: use distinct message for unknown status codes
Fix SmtpException.GetMessageForStatus: use distinct messages for unknown and defined-but-unmapped status codes
Apr 7, 2026
This was referenced Apr 7, 2026
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
SmtpException.GetMessageForStatusfell throughdefaulttoSmtpStatusCode.CommandUnrecognized, so any unrecognized status value (e.g.(SmtpStatusCode)0from an uninitialized_statusCode) produced the misleading message "Syntax error, command unrecognized." — implying an SMTP 500 response when none occurred.Description
Strings.resx: Added three new resource strings:SmtpUnknownStatusCode→"Unknown SMTP status code."(for truly undefined status code values)SmtpCannotVerifyUserWillAttemptDelivery→"Cannot verify user, but will attempt delivery."(for SMTP 252)SmtpGeneralFailure→"General failure."(for the internalGeneralFailure = -1sentinel)SmtpException.cs: Separateddefaultfromcase SmtpStatusCode.CommandUnrecognizedinGetMessageForStatus, and added explicit case labels for all previously unmapped defined enum members (CannotVerifyUserWillAttemptDeliveryandGeneralFailure):SmtpExceptionTest.cs: Added assertion confirming unknown status codes yield a message distinct fromCommandUnrecognized.Customer Impact
Exceptions thrown with undefined
SmtpStatusCodevalues (e.g. during connection failures before any SMTP response is received) produce a confusing "command unrecognized" message, making diagnosis harder — particularly when the real cause is a transport/handshake failure unrelated to SMTP 500. All definedSmtpStatusCodeenum members now map to their own descriptive messages; thedefaultcase exclusively handles truly undefined values.Regression
No — this behavior has been present since the original .NET Framework implementation.
Testing
Existing
SmtpExceptionTestsuite (318 tests, 0 failures). Added assertion toTestConstructorWithStatusCodeArgumentverifying thatnew SmtpException((SmtpStatusCode)666).Messagediffers fromnew SmtpException(SmtpStatusCode.CommandUnrecognized).Message.Risk
Very low. Logic change in a
switchstatement; only affects diagnostic messages. All previously handled enum values retain their existing messages. The two previously unmapped defined enum values (CannotVerifyUserWillAttemptDelivery,GeneralFailure) now return descriptive messages instead of the misleading "command unrecognized" text.