dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.48k stars 4.77k forks source link

[API Proposal]: Customized date formatting for SmtpClient #110361

Open NXTwoThou opened 1 day ago

NXTwoThou commented 1 day ago

Background and motivation

I've had a long term issue with the built in mail app on my phone(Xiaomi) for a number of years. Any email sent via .NET shows 12/31/1969 for the date. I'm inbetween projects for a few hours this morning and it finally bugged me enough to look into it. So I turned on debug logging on our mail server and captured a few hundred emails from various sources this morning and directly ran some sets with .NET Framework 4.8 and .NET 9.

The likely cause of the issue with Xiaomi's mail app is the format used by the date. I don't think they understood RFC822 where the day of the week is an optional field([]).

Out of the 128 emails I parsed in the logs, the only one not sending the DOW is .NET. Is there some flag I'm missing?

For example, I sent an email to my Outlook email from a .NET 9 WinForm app:

Date: 3 Dec 2024 09:26:24 -0600

I then replied to that email from Outlook.com and got back

Date: Tue, 3 Dec 2024 15:36:32 +0000

I recognize I don't have an issue viewing the non-DOW formatted date with Thunderbird, Outlook client, Outlook.com, Yahoo mail, and GMail. I recognize 128 emails is a pretty small sampling, but if everyone else is doing it, could it be an option to turn on for additional compatibility?

API Proposal

enum SmtpDateFormatting { Standard,Full }; public DateFormatting=SmtpDateFormatting.Standard;

string tz=date.ToString("zzz").Replace(":",""); if(DateFormatting==SmtpDateFormatting.Full) return date.ToString("ddd, d MMM yyyy HH:mm:ss ")+tz; else return date.ToString("d MMM yyyy HH:mm:ss ")+tz;

API Usage

SmtpClient.DateFormatting=SmtpDateFormatting.Full;

Alternative Designs

No response

Risks

No response

Tornhoof commented 1 day ago

The integrated SMTPClient is deprecated. See https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient?view=net-9.0

A modern replacement is Mailkit https://github.com/jstedfast/MailKit

wfurt commented 1 day ago

MailMessage exposes Headers collection. Did you try to set it directly @NXTwoThou? @Tornhoof is right that the class is in maintenance only mode and it is unlikely we would extend it at the moment.

NXTwoThou commented 1 day ago

MailMessage exposes Headers collection. Did you try to set it directly @NXTwoThou? @Tornhoof is right that the class is in maintenance only mode and it is unlikely we would extend it at the moment.

The first thing I did was hunt to see if I could directly set it. https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.mailmessage.headers?view=netframework-4.8 has a long list of headers that are discarded if you try to directly set them. Including Date