jstedfast/MailKit

Attachment shows 0 Bytes in encrypted email

Closed this issue · 2 comments

Describe the bug
I create a new email via body builder and add attachments to it. After that I encrypt that body with a temporary secure mime context and send the mail. Attachments show up in outlook, can be opened and saved, but outlooks says the size is 0 Bytes. If I save the file to the system, the file explorer shows the correct size. I already looked at #956 but it doesn't work.

If I send the mail unencrypted the file size is shown correctly. So from my point of view I am missing something with the encryption part.

Platform:

  • OS: Windows
  • .NET Framework: .Net 8.0
  • MailKit Version: 4.8.0
  • E-Mail Client: MS Outlook

Expected behavior
Outlook should show the correct file size.

Code Snippets

var message = new MimeMessage();

var builder = new BodyBuilder ();
builder.HtmlBody = "some string";

using MemoryStream memoryStream = new MemoryStream();
await filestream.CopyToAsync(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin); // or Position = 0

builder.Attachments.Add(attachment.FileName, memoryStream);

message.Body = builder.ToMessageBody();

using (var ctx = new TemporarySecureMimeContext())
{
    foreach (var certificate in recipientCertificates)
    {
        ctx.Import(certificate);
    }

    message.Body = ApplicationPkcs7Mime.Encrypt(ctx, message.To.Mailboxes, message.Body);
}

await client.ConnectAsync(SmtpSettings.Address, SmtpSettings.Port);

await client.SendAsync(message);

Attachments show up in outlook, can be opened and saved, but outlooks says the size is 0 Bytes. If I save the file to the system, the file explorer shows the correct size.

This is a bug in Outlook.

What is happening is that it only calculates the size of the attachments in the original message (before it does any decryption).

Once it decrypts, it should recalculate the size of the decrypted attachments, but apparently it does not do that, hence why it displays as 0.

This is not something MimeKit can solve.

What you could try (but I make no guarantees that it would work), is:

var part = builder.Attachments.Add(attachment.FileName, memoryStream) as MimePart;
part.ContentDisposition.Size = fileStream.Length;

Perhaps, if Outlook has code to use the Content-Disposition header's size=... parameter for a display value, maybe it will show the correct size.

MimeKit doesn't set this by default because none of the most popular mail clients set it as far as I've seen.

@jstedfast thank you for your quick response. I tried the header but outlook seems to ignore it. But knowing the issue is not on my side helps already.