MimeKit: MessageDeliveryStatus accessors throws "Failed to parse headers." if content doesn't end with a blank line

When the last status group in a message/delivery-status MIME part does not end with a blank line the MimeParser throws an “Failed to parse headers.” exception when calling the MessageDeliveryStatus.StatusGroups property.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 16 (10 by maintainers)

Commits related to this issue

Most upvoted comments

@jstedfast sure, please do

@alex-jitbit is it ok if I end up including your test message as a unit test in MimeKit? Or should I try to scrub anything?

The issue is that the message/delivery-status content should be multiple blocks of headers (aka status groups), but after the first status group of headers, it’s base64-encoded. Turns out the base64-encoded blob is the second status group of headers.

I’ve got a work-around that checks if the first status group of headers contains a Content-Transfer-Encoding, and if so, passes the remainder of the data thru a base64 decoder to parse the rest.

I’m not 100% sure it’s caused by line breaks or even connected to the original issue. But here’s the .eml that breaks things: https://www.dropbox.com/s/bssh4zalgyo2dv5/bounced.eml?dl=0

It’s a standard bounce message sent by Outlook 365

It has a delivery status report in it, and here’s the code that breaks:

var deliveryReport = message.BodyParts.Where(p => p.ContentType?.MediaSubtype == "delivery-status").FirstOrDefault();
var deliveryStatus = deliveryReport as MessageDeliveryStatus;

//this line causes exception "failed to parse headers"
var statusheaders = deliveryStatus != null
	? deliveryStatus.StatusGroups.SelectMany(g => g).SafeToDictionary(h => h.Field, h => h.Value)
	: new Dictionary<string, string>();