Ad Rian
Ad Rian

Reputation: 49

Writing MimeMessage to MemoryStream length/encoding issues when running application with Debian/Docker

I'm facing issues with a .net Core web app (based on Boilerplate / ASP.NET Zero), where we implemented MailKit and MimeKit. We're downloading MimeMessages via IMAP and save them as byte[] in a database in .eml format.

While everything is fine in the debugger from Visual Studio, the published app in a Debian/Docker environment behaves differently in terms of stream length - the stream is a little bit shorter. As a result, when downloading the .eml file from our DB later, Outlook has trouble with decoding that file, e. g.:

Decoding issues in Outlook

The relevant code is as follows:

mimeMessage = await folder.GetMessageAsync(new MailKit.UniqueId(input.EMailDto.IMAPUId));
using (var memory = new MemoryStream())
{
    mimeMessage.WriteTo(memory);
    dataRow.byteArray = memory.ToArray();
}

When I look at memory.Length in VS debugger, it is e. g. 547.585 (and no issues with Outlook), while run with Docker it's length (I put in some logging to read it at the same position) is only 540.341 and the trouble begins...

Reading/downloading from the DB is fine in both environments though, an email saved via VS debugger is intact also when downloaded via Debian/Docker environment.

Thanks very much for any ideas and hints!

EDIT: I did some comparison with a hex editor and it's 0D0A vs. 0D causing the issues. Still not sure how to fix this properly..?

Upvotes: 0

Views: 505

Answers (1)

Ad Rian
Ad Rian

Reputation: 49

After some testing I can confirm the solution:

var options = FormatOptions.Default.Clone();
options.NewLineFormat = NewLineFormat.Dos;
using (var memory = new MemoryStream())
{
   message.WriteTo(options, memory);
   [...]
}

Thanks again!

Upvotes: 1

Related Questions