jstedfast / MimeKit

A .NET MIME creation and parser library with support for S/MIME, PGP, DKIM, TNEF and Unix mbox spools.
http://www.mimekit.net
MIT License
1.84k stars 373 forks source link

MimeKit parse/get DATE in wrong format #1088

Closed gisaac85 closed 1 month ago

gisaac85 commented 1 month ago

Describe the bug We have a GraphAPI ( version 4.46.0 ) and MimeKit ( version 4.8.0 ), now we have this line of code ( in C# ) , a method to get the email and converted it as MimeMessage :

image

Now, we have an email with received date Mon 11 Mar 2024 13:49 :

image

Then, in the code we see this date which retrieved by MimeMessage.LoadAsync() method:

image

So, we expect 11 Mar but we get 3 Nov ..

For other emails, Date is fine and has always a right date format, only for specific emails that happens.

Any help please!

Platform (please complete the following information):

jstedfast commented 1 month ago

The issue is that the date format is wrong in the email message and the Date parser struggles to make sense of it.

I can look into seeing if I can modify the "BrokenDateParser" logic to resolve this date correctly.

jstedfast commented 1 month ago

Hmmm, it seems to get the correct value for me. When I added "Mon 11 Mar 2024 13:49" to the unit tests, the date parser (which falls back to the broken date parser) returns a DateTimeOffset value of "Mon, 11 Mar 2024 13:49:00 +0000"

What is the actual raw Date header value?

gisaac85 commented 1 month ago

@jstedfast Thnx for ur fast reaction!

Here is the header data:

Received: from .outlook.com
 () by .outlook.com with
 HTTPS; Mon, 11 Mar 2024 12:49:39 +0000
Received: from .outlook.com (21)
 by .outlook.com () with
 Microsoft SMTP Server (version=TLS1_2,
 cipher=CM_SHA384) id ; Mon, 11 Mar
 2024 12:49:37 +0000
Received: from .outlook.com
 () by .outlook.office365.com
 () with Microsoft SMTP Server (version=TLS1_2,
 cipher=_GCM_SHA384) id  via Frontend
 Transport; Mon, 11 Mar 2024 12:49:37 +0000
Authentication-Results: spf=none (sender IP is )
 smtp.mailfrom=l; dkim=none (message not signed)
 header.d=none;dmarc=none action=none header.from=l;
Received-SPF: None (.outlook.com: al does not designate
 permitted sender hosts)
Received: from .nl (4) by
 .outlook.com (.8) with Microsoft SMTP
 Server (version=TLS1_2, cipher=_GCM_SHA384) id
 2 via Frontend Transport; Mon, 11 Mar 2024 12:49:36 +0000
Received: from anonymous (al [])
    by .nl (Postfix) with SMTP id 908
    for <@.nl>; Mon, 11 Mar 2024 13:49:36 +0100 (CET)
DKIM-Filter: OpenDKIM Filter v2.11.0 .nl 908
From: Faxbevestiging<al>
To: .nl
Subject: Fax is Verzonden
X-Mailer: Fax
Date: 11-3-2024
Content-Type: text/plain; charset=us-ascii
Message-ID:
 <021a7802-1.outlook.com>
Return-Path: al
X-MS-Exchange-Organization-ExpirationStartTime: 11 Mar 2024 12:49:37.0693
 (UTC)
X-MS-Exchange-Organization-ExpirationStartTimeReason: OriginalSubmit
X-MS-Exchange-Organization-ExpirationInterval: 1:00:00:00.0000000
X-MS-Exchange-Organization-ExpirationIntervalReason: OriginalSubmit
X-MS-Exchange-Organization-Network-Message-Id:
 598
X-EOPAttributedMessage: 0
X-MS-Exchange-Organization-MessageDirectionality: Originating
X-MS-PublicTrafficType: Email
X-MS-TrafficTypeDiagnostic:
 766:EE_
X-MS-Exchange-Organization-AuthSource:
 .outlook.com
X-MS-Exchange-Organization-AuthAs: Anonymous
X-OriginatorOrg: .nl
X-MS-Office365-Filtering-Correlation-Id: 9b598
X-MS-Exchange-AtpMessageProperties: SA|SL
X-MS-Exchange-Organization-SCL: -1
X-Microsoft-Antispam: BCL:0;
X-Forefront-Antispam-Report:
 CIP:.84;CTRY:NL;LANG:nl;SCL:-1;SRV:;IPV:NLI;SFV:SKN;H:.nl;PTR:8.nl;CAT:NOFS:;DIR:INB;
X-MS-Exchange-ForwardingLoop:
 .nl;15e3b
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Mar 2024 12:49:36.9912
 (UTC)
X-MS-Exchange-CrossTenant-Network-Message-Id: b598
X-MS-Exchange-CrossTenant-Id: e3b
X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=p=[];Helo=[.nl]
X-MS-Exchange-CrossTenant-AuthSource: .outlook.com
X-MS-Exchange-CrossTenant-AuthAs: Anonymous
X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem
X-MS-Exchange-Transport-CrossTenantHeadersStamped:295
X-MS-Exchange-Transport-EndToEndLatency: 00:00:02.0841909
X-MS-Exchange-Processed-By-BccFoldering: 1.018
X-Microsoft-Antispam-Mailbox-Delivery:
    ucf:0;jmr:0;auth:0;dest:I;ENG:(910001)(944506478)(944626604)(920097)(930097)(140003)(1420198);
X-Microsoft-Antispam-Message-Info:
jstedfast commented 1 month ago

This is all I needed, thanks:

Date: 11-3-2024

The problem is that MimeKit's date parser assumes that if the date is in this format, that it is in MM-DD-YYYY format, but this date is in DD-MM-YYYY and there's no way to distinguish which format it is using because it is valid in either.

Unfortunately, that means there's no solution to this.

gisaac85 commented 1 month ago

@jstedfast Thanks for explaining the point! BUT, I assumed that MimeKit parser may use also the other items in Header to calculate the date , for example this one:

Received: from .outlook.com () by .outlook.com with HTTPS; Mon, 11 Mar 2024 12:49:39 +0000

And as I see, not all emails coming to our Outlook with this kind of format, most of them coming with this:

image

and with this we had no problem at all!

anyway, thnx for ur effort.

jstedfast commented 1 month ago

Well, the MimeMessage.Date property is meant to represent only the Date: header value, not try and do extra smarts.