Saturday, March 5, 2011

"Failure sending mail" error message when you send an email message by using a .NET Framework 4.0-based application that uses the "System.Net.Mail.SmtpClient" class if the email attachment is larger than 3 MB

I recently had an issue after upgrading a website from .Net 3.5 to .Net 4.0, particularly when trying to send an email attachment larger than 3 MB using the System.Net.Mail.SmtpClient class.

System.Net.Mail.SmtpException:
Failure sending mail.

System.IndexOutOfRangeException:
Index was outside the bounds of the array.

System.IndexOutOfRangeException stack trace:
at System.Net.Base64Stream.EncodeBytes(Byte[] buffer, Int32 offset, Int32 count, Boolean dontDeferFinalBytes, Boolean shouldAppendSpaceToCRLF) at System.Net.Base64Stream.Write(Byte[] buffer, Int32 offset, Int32 count) at System.Net.Mime.MimePart.Send(BaseWriter writer) at System.Net.Mime.MimeMultiPart.Send(BaseWriter writer) at System.Net.Mail.SmtpClient.Send(MailMessage message)

Turns out this was a known bug with a hotfix already available:

http://support.microsoft.com/kb/2183292

https://connect.microsoft.com/VisualStudio/Downloads/DownloadDetails.aspx?DownloadID=30226

Some other articles I found along the way:

http://stackoverflow.com/questions/2803132/net-4-0-fails-when-sending-emails-with-attachments-larger-than-3mb

http://connect.microsoft.com/VisualStudio/feedback/details/544562/cannot-send-e-mails-with-large-attachments-system-net-mail-smtpclient-system-net-mail-mailmessage

0 comments: