随机“索引超出了数组的范围”作为SmtpException

时间:2012-03-14 14:15:35

标签: c# email smtpclient system.net.mail

我使用下面显示的Send方法随机获得异常。我得到的例外是:

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.

我的方法如下所示:

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);
}

在调用此覆盖之前,我正在验证to List和附件列表是否至少有一个值,并且该值是有效的。

在sever.Send上发生异常。

1 个答案:

答案 0 :(得分:5)