jstedfast/MimeKit

Question: Is this fixable with MimeKit

Sicos1977 opened this issue · 4 comments

After long digging around why Exchange Web Services kept throwing an "Invalid Mime Content" error I found out that we received an e-mail message that has 2 mail boundries with the same value: _D0D60A53-E992-4C48-A357-B58F6B94DF27_ and this one is also used for the attachment that is inside the file.

MimeKit seems not having any troubles in reading this mail. But when I write it to an memorystream and try to post it on EWS it will still throw the same error:

       using var memoryStream = StreamHelpers.Manager.GetStream("MailBox.WriteMessage");
       await mimeMessage.WriteToAsync(memoryStream, cancellationToken).ConfigureAwait(false);
       memoryStream.Position = 0;

        var charset = mimeMessage.Body?.ContentType?.Charset;
        if (string.IsNullOrWhiteSpace(charset))
                charset = Encoding.Default.BodyName;

                    WriteProgress($"Connecting to Exchange Web Services and creating new e-mail with subject '{mimeMessage.Subject}' and charset '{charset}'");
                    var emailMessage = new EmailMessage(ExchangeService)
                    {
                        MimeContent = new MimeContent(charset, memoryStream.ToArray()),
                        IsRead = false
                    };

Is there a way with MimeKit to repair this damaged e-mail so that EWS is happy and stops throwing this error.

Date: Tue, 4 Jul 2023 20:52:47 +0200
From: XXXXX XXXX <xxxxxxxxxxxx@xxxxxxxxxx.com>
Subject: alarm film
Thread-Topic: alarm film
Message-ID:
 <AM9P192MB111057EBC19602F25AAC0D5CEB2EA@AM9P192MB1110.EURP192.PROD.OUTLOOK.COM>
To: Alex Janssen <xxxxxxx@xxxxxxx.nl>
Content-Type: multipart/mixed;
	boundary="_D0D60A53-E992-4C48-A357-B58F6B94DF27_"
MIME-Version: 1.0

--_D0D60A53-E992-4C48-A357-B58F6B94DF27_
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset="utf-8"

<html><head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dutf-8"></=
head><body><div><br></div><div id=3D"ms-outlook-mobile-signature" dir=3D"au=
to"><div><br></div>Verzonden vanaf <a href=3D"https://aka.ms/AAb9ysg">Outlo=
ok voor Android</a></div></body></html>=

--_D0D60A53-E992-4C48-A357-B58F6B94DF27_
Content-Type: video/mp4; name="VID-20230704-WA0030.mp4"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="VID-20230704-WA0030.mp4"

AAAAGGZ0eXBtcDQyAAAAAG1wNDJpc29tAAAAGGJlYW0BAAAAAQAAAAAAAAACAAAAAACHMW1vb3YA
AABsbXZoZAAAAAAAAAAAAAAAAAAAAlgAAOsKAAEAAAEAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAA
AAAAAQAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMAADpUdHJh
awAAAFx0a2hkAAAABwAAAAAAAAAAAAAAAQAAAAAAAOsKAAAAAAAAAAAAAAAAAQAAAAABAAAAAAAA
AAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAQAAAAAJgAAAB4AAAAAA58G1kaWEAAAAgbWRoZAAAAAAA

I'm not sure that I understand the issue 100% correctly, so let me know if I have misunderstood the issue:

Are you saying that the messages that have issues have nested multiparts that use identical boundaries? If so, yes, you can iterate over the multiparts and check their Boundary properties to see if they match, and if they do, you can change one of them to something different.

That said, the message that you posted doesn't seem to fit that description which is why I think I am not understanding correctly.

From what I can tell looking at the posted message, it looks fine to me. Every child of the multipart should have the same boundary marker.

Well then I probably made the wrong conclusion and the real problem is somehow in the added attachment. For some reason EWS keeps complaining that there is a mime error and I can't figure out what the real problem is. When I removed the attachment everything works and EWS doesn't complain. But when I open the original email in outlook... The one with attachment then everything also works without any problems. The only thing that does not work is trying to save the EML file to a mailbox with EWS when the attachment is included. And the only thing that is given back from EWS is an Invalid mime type error. So I'm a little bit clueless now what the real problem is

So I'm a little bit clueless now what the real problem is

Well, that makes 2 of us... 😆

So I'm a little bit clueless now what the real problem is

Well, that makes 2 of us... 😆

Good to hear that I'm not the only one. It is already 5pm over here in the Netherlands so this will be something to figure out on Monday. Have a nice weekend