Skip to content

Escape quotes in MailAddress display name when encoding SMTP headers#128979

Draft
Copilot wants to merge 2 commits into
mainfrom
copilot/support-quotes-in-email-displayname
Draft

Escape quotes in MailAddress display name when encoding SMTP headers#128979
Copilot wants to merge 2 commits into
mainfrom
copilot/support-quotes-in-email-displayname

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jun 4, 2026

MailAddress.Encode — the path SmtpClient uses to write From/To/Cc/etc. headers — wraps the display name in "…" but does not escape embedded ", producing an invalid RFC 5322 quoted-string and corrupting the header when a DisplayName like Henry "The Fonz" Winkler is used. MailAddress.ToString() already escapes quotes; only the SMTP send path was missed when quote support was added in #36752.

Changes

  • MailAddress.Encode (ASCII / allowUnicode=true branch): escape embedded " as \" via .Replace("\"", "\\\""), matching ToString(). The Q-encoded (non-ASCII) branch is unaffected since encoded-words don't use quoted-string syntax.
  • Tests: add regression tests in MailAddressEncodeTest for both MailAddress.Encode and MailAddressCollection.Encode, covering ASCII and Unicode-allowed modes.

Before vs. after for new MailAddress("test@example.com", "Henry \"The Fonz\" Winkler").Encode(0, false):

- "Henry "The Fonz" Winkler" <test@example.com>     // invalid quoted-string
+ "Henry \"The Fonz\" Winkler" <test@example.com>   // RFC 5322-compliant

Copilot AI self-assigned this Jun 4, 2026
Copilot AI review requested due to automatic review settings June 4, 2026 11:51
Copilot AI review requested due to automatic review settings June 4, 2026 11:51
Copilot AI linked an issue Jun 4, 2026 that may be closed by this pull request
@MihaZupan MihaZupan assigned mrek-msft and unassigned MihaZupan Jun 4, 2026
Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot June 4, 2026 12:19
Copilot AI changed the title [WIP] Fix SmtpClient to support quotes in email DisplayName Escape quotes in MailAddress display name when encoding SMTP headers Jun 4, 2026
Copilot AI requested a review from MihaZupan June 4, 2026 12:20
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @karelz, @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for quotes in email address DisplayName

3 participants