I’m using Mandrill as the mail sender system for my site kurslitteratur.se. From time to time I’ve received support cases from the users stating that their emails never get delivered to the recipient.

At that time, I investigated the logs on the site, where no errors show up. I also consulted the logs in the Mandrill system, with no luck. It seems that I’ve been digging at the wrong place the whole time. The mails didn’t get delivered, even though the log messages was written to the success-log.

And it wasn’t Mandrill.

The response from the Mandrill sending system, found in the API logs:

[
	{
		"email": "first.last@recipients.mail.com",
		"status": "rejected",
		"_id": "601f51e7047a4a51988cc545a0f96c93",
		reject_reason": "invalid-sender"
	}
]

Invalid sender? All the mails that is delivered successfully used the same sender, odd behavior. Said and done, a conversation with Mandrill support pointed me in the right direction.

There is a bug in the .NET-framework. And I’m using version 4.6.

In short, the specification states a maximum length of the subject line, or otherwise use line breaks. As .NET by default encodes in UTF-8 and base 64 sometimes a line break shows up in the middle of a character and that’s what causing the invalid-sender error.

The workaround is to set the subject encoding to ISO-8859-1 by using Encoding.GetEncoding(28591). Who knows what problems that might hold in regards of mail clients.

An extensive discussion regarding this issue can be found at https://social.msdn.microsoft.com/Forums/vstudio/en-US/4d1c1752-70ba-420a-9510-8fb4aa6da046/subject-encoding-on-smtpclientmailmessage?forum=wcf

And if you’re interested, a deeper discussion regarding encodings can be found at http://csharpindepth.com/Articles/General/Unicode.aspx