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?