0

We are using the System.Net.Mail to send email messages as text with attachments. The attachments are Excel and Powerpoint files. The content types are set to the MIME types before sending the email.

A test done with three emails proved that the Exchange server recorded a 26% increase in each of the cases.

Is there a way to stop this increase in message size?

If not, is there a another .NET or open source alternative for this?

Would SMTP Drop overcome this issue?

UPDATE:

var mimeMessage = new MimeMessage();

                mimeMessage.From.Add(new MailboxAddress(string.Empty, fromEmailAddress));    
                mimeMessage.To.Add(new MailboxAddress(string.Empty, toEmailAddress));
                mimeMessage.Cc.Add(new MailboxAddress(string.Empty, copyEmailAddress));
                mimeMessage.Subject = subject;

                var builder = new BodyBuilder { HtmlBody = bodyText };

                MvcApplication.Logger.Info("SendEmail:attachmentFiles:Count=" + attachmentFiles.Count);
                foreach (var attachmentFile in attachmentFiles)
                {
                    var attachment = new MimePart()
                    {
                        ContentObject = new ContentObject(File.OpenRead(attachmentFile)),
                        ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
                        ContentTransferEncoding = ContentEncoding.Binary,
                        FileName = Path.GetFileName(attachmentFile)
                    };

                    builder.Attachments.Add(attachment);
                }
                MvcApplication.Logger.Info("SendEmail:attachmentFiles:Added Count=" + builder.Attachments.Count);

                mimeMessage.Body = builder.ToMessageBody();

                using (var client = new SmtpClient())
                {
                    client.Connect("smtp.domain.com", 25, false);

                    client.Send(mimeMessage);

                    MvcApplication.Logger.Info(
                        client.Capabilities.HasFlag(SmtpCapabilities.BinaryMime)
                            ? "SMTP Server supports BinaryMime"
                            : "SMTP Server does NOT support BinaryMime");


                    client.Disconnect(true);
                }

The above code sends a HTML message successfully.

The SMTP Server Capabilities flag for BinaryMime returns true.

If the ContentTransferEncoding is Base64 it works(9 excel and powerpoint files attached). If I change it Binary then just one corrupt excel file is attached. What am I missing here?

Ranjith Venkatesh
  • 1,322
  • 3
  • 20
  • 57
  • 1
    Binary files must be encoded as text, this causes an increase in size as the available alphabet is smaller than byte ranges, is this what you are seeing? – Alex K. Mar 27 '15 at 13:21
  • I would think if the file encoding is the issue he would have seen a 33% increase in all cases. Base64, which Exchange uses to encode binary files for transmission as an email attachment causes exactly a 33% increase in size. – Kevin Mar 27 '15 at 13:27
  • I think so. I first tried setting as "Octet" and then tried setting specific MIME types and both bloated the email size. Outlook when manually sending the same email with attachments does not bloat the size. – Ranjith Venkatesh Mar 27 '15 at 13:27
  • Thanks for confirming there is an increase. Is there an alternative? – Ranjith Venkatesh Mar 27 '15 at 13:28
  • Mitigate by zipping them up (together or individually) ? – Alex K. Mar 27 '15 at 13:31
  • We have two use cases, one with Zip and and one without Zip so we need to figure this out. Is Zip not a binary file that needs to encoded too? – Ranjith Venkatesh Mar 27 '15 at 13:33
  • So with outlook you don't see the increased size? Are you able to use wireshark? You could inspect what goes on on the wire in both cases (outlook and System.Net.Mail). – Holger Thiemann Mar 27 '15 at 13:39
  • Office files are already zip files. re-zipping isn't going to offer much size decrease (may even increase the overall size a bit depending on the original sizes). – Kevin Mar 27 '15 at 13:55
  • *Is Zip not a binary file that needs to encoded too?* - Yea, but a smaller one. Here, for large files, we just send a secured download link. – Alex K. Mar 27 '15 at 13:56
  • 2
    This has already been indicated but, I'll reiterate just for clarity, email is a text only protocol. In order to transmit binary files they must be encoded into a text friendly format. All binary-to-text encoding schemes add size. Exchange currently uses base64 which causes a 33% increase in the number of bytes transmitted. This is an improvement over Hex encoding, which used to be used and causes a 50% increase in size. The alternative would be to not attach the files, but put in the body of the email a download link or FTP url to access the file. – Kevin Mar 27 '15 at 14:01

1 Answers1

1

If the SMTP server supports the BINARYMIME extension, you could use MailKit to send email and set the Content-Transfer-Encoding of the attachments to ContentEncoding.Binary.

I don't think that System.Net.Mail supports the BINARYMIMNE SMTP extension (at least it didn't look like it did when I reviewed the referencesource on GitHub), hence System.Net.Mail.SmtpClient will always base64 or quoted-printable encode attachments.

The BINARYMIME SMTP extension allows clients to send messages without the need to abse64 or quoted-printable encode them.

jstedfast
  • 35,744
  • 5
  • 97
  • 110
  • Is there a way to get content type automatically or should I set it manually for xls, xlsx, pdf, mp3, wmv? – Ranjith Venkatesh Mar 30 '15 at 07:54
  • I don't know. It works for me, so perhaps your receiving client is broken or something? Maybe it can't handle binary attachments? In what way is it corrupted? What happens if you do `new ContentObject(File.OpenRead(attachmentFile), ContentEncoding.Binary)`? – jstedfast Mar 30 '15 at 11:26
  • I get the error, the file format and extension do not match. In this case it was an excel file. Should mediaType and mediaSubType be set? – Ranjith Venkatesh Mar 30 '15 at 12:12
  • Tried setting the mediaType to "application" and mediaSubType to "vnd.ms-excel" and ContentObject read as ContentEncoding.Binary. The only excel file attached was 570 bytes and has the error message that the file format and extension do not match. I tried opening the message in Internet Explorer and Outlook 2013 with no success. – Ranjith Venkatesh Mar 30 '15 at 12:19
  • How large is the file? is it more or less than 570 bytes? The error to me sounds more like it means the file extension and format do not match and not the mime-type. – jstedfast Mar 30 '15 at 13:17
  • In this case the "file.xls" was 26KB. File extension is ".xls". Mime type is a combination of mediaType: "application" and mediaSubType: "vnd.ms-excel". What is "format" in this case and how do I set it? – Ranjith Venkatesh Mar 30 '15 at 17:25
  • The format is just the format of the content. If the content is being truncated to 570 bytes, that's the problem... What happens if you write the message to disk? Is the content truncated to only 570 bytes? Or does it write the whole thing? – jstedfast Mar 30 '15 at 18:10
  • The only thing that seems to cause the corruption seems to be ContentTransferEncoding. Only Base64 works and the rest corrupt the files. How do I write the message to disk?(MimeMessage is not serializable) – Ranjith Venkatesh Mar 31 '15 at 13:54
  • message.WriteTo (stream); – jstedfast Mar 31 '15 at 14:12