I’m getting an exception randomly with the Send method shown below. The exception I’m getting is:
Exception information:
Exception type: System.Net.Mail.SmtpException
Exception message: Failure sending mail.
Inner exception information (level 1):
Exception type: System.IndexOutOfRangeException
Exception message: Index was outside the bounds of the array.
My method looks like the following:
public void Send(string from, List<string> to, string subject, string body, List<string> attachments)
{
var email = new MailMessage();
var server = new SmtpClient();
// Add each mail property
email.From = new MailAddress(from);
foreach (var t in to)
email.To.Add(t);
email.Subject = subject;
email.IsBodyHtml = true;
email.Body = body;
foreach (var a in attachments)
email.Attachments.Add(new Attachment(a));
server.Send(email);
}
Before calling this override, I’m verifying that the to List and the attachment list both have at least a single value and that the value is valid.
The exception occurs on sever.Send.
This sounds like the same issue as this question about .NET 4.0 failing when sending emails with large attachments. Microsoft has posted a bug fix here.