Random "Index was outside the array" as a SmtpException

I am getting an exception randomly using the submit method shown below. The exception I am 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 this:

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 check that the List and the attachment list have at least one value and that the value is valid.

The exception occurs on the sever.Send server.

+3


source to share


1 answer


This seems to be the same issue as this question about .NET 4.0 failing to send emails with large attachments . Microsoft has posted a bug fix here .



+5


source







All Articles