MimeKit: Attachments corrupt/broken

Dear Devs,

I have a production issue where i’m sending e-mails that all have the same attachments. The only difference is the subject and receiver, but SOMETIMES some of them end up with Corrupted Attachments.

They are all PDF files and are all the same for each “batch”.

I tried everything from adding them as a memory stream to just sending them the regular suggested way and every other thing i could think of / find on the internet.

I also found one ‘Closed Issue’ about “rewinding the stream position” in one of the versions, so i updated my version but it did not change anything…

I started sending each maill seperate and now i send them all together in a ‘SMTP USING Block’ to be sure there are not to many connections open (just 1) but also this didn’t help.

I even ended up adding the rewind to my own code but also this didn’t work.

PS: Keep in mind they all use the same attachments which are not corrupt when uploading… and even end up without a problem at other receivers.

    try
            {
                var mimeMessages = new List<MimeMessage>();

                foreach (var email in emails)
                {
                    var mimeMessage = new MimeMessage();
                    mimeMessage.From.Add(new MailboxAddress(email.Sender));
                    mimeMessage.To.AddRange(email.Receivers.Select(x => new MailboxAddress(x)));
                    mimeMessage.Subject = email.Subject;

                    var builder = new BodyBuilder { HtmlBody = email.Body };

                    if (email.Attachments.Any())
                    {
                        email.Attachments.ToList().ForEach(attachment =>
                        {
                            var fileName = string.Empty;

                            using (var memoryStream = new MemoryStream())
                            {
                                memoryStream.Position = 0;
                                
                                if (attachment is MimePart)
                                {
                                    ((MimePart)attachment).Content.DecodeTo(memoryStream);
                                    fileName = ((MimePart)attachment).FileName;
                                }
                                else
                                {
                                    ((MessagePart)attachment).Message.WriteTo(memoryStream);
                                    fileName = ((MessagePart)attachment).ContentDisposition?.FileName;
                                }

                                builder.Attachments.Add(fileName, memoryStream.ToArray());
                            }
                        });
                    }

                    mimeMessage.Body = builder.ToMessageBody();
                    mimeMessages.Add(mimeMessage);
                }
 
                using (var emailClient = new SmtpClient())
                {
                    emailClient.Connect(_smtpServer, _smtpPort, MailKit.Security.SecureSocketOptions.None);
                    emailClient.AuthenticationMechanisms.Remove("XOAUTH2");  //Remove any OAuth functionality as we won't be using it.

                    foreach (var message in mimeMessages)
                    {
                        await emailClient.SendAsync(message);
                    }

                    await emailClient.DisconnectAsync(true);
                }

                _logger.LogInformation("{x} Mails send", emails.Count);
            }

Please help !

Platform (please complete the following information):

  • Windows Server
  • NetCore 2.2
  • MimeKit Version: 2.3.1.6

Expected behavior Send email with uncorrupted files.

Additional context tried everything.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 15 (7 by maintainers)

Most upvoted comments

Also a protocol log would be helpful for diagnosing if the issue is in the SmtpClient or your mail server.

using (var emailClient = new SmtpClient(new ProtocolLogger("smtp.log")))

Corrupted how?