System.Net.Mail.SmtpException:服务不可用,关闭传输通道.服务器响应是:4.4.2

roc*_*in' 7 timeout smtp transmission channel

当我经常向用户列表发送一些电子邮件时,我收到此错误.假设它发送10封邮件而1则发出错误,然后发送更多邮件并给出相同的错误.

代码如下所示:

public static bool SendEmail(string toMail, string fromname, string from, string subject, string body, string BCC)
    {

        MailMessage mailmessage = new MailMessage("frommail@mail.com", toMail, subject, body);
        mailmessage.IsBodyHtml = true;
        mailmessage.BodyEncoding = Encoding.GetEncoding(1254);
        mailmessage.SubjectEncoding = Encoding.GetEncoding(1254);

        SmtpClient objCompose = new SmtpClient("xxxx");

        try
        {
            objCompose.Send(mailmessage); 

            return true;
        }
        catch (Exception ex) { 

        }

        return false;
    }
Run Code Online (Sandbox Code Playgroud)

而我得到的错误是这样的:

System.Net.Mail.SmtpException:服务不可用,关闭传输通道.服务器响应是:4.4.2 mailer.mailer.com错误:在System.Net.Mail.MailCommand.Send上的System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode,String response)超出超时(SmtpConnection conn,Byte [ ]命令,在System.Net.Mail.SmtpTransport.SendMail(MailAddress发件人,收件人MailAddressCollection,在System.Net.Mail.SmtpClient.Send字符串deliveryNotify,SmtpFailedRecipientException&除外)从字符串)(消息MAILMESSAGE)

任何人都可以请求帮助,这个错误杀了我.

提前致谢.

roc*_*in' 10

处理smtpclient(objCompose)就可以了.

    // Summary:
    //     Sends a QUIT message to the SMTP server, gracefully ends the TCP connection,
    //     and releases all resources used by the current instance of the System.Net.Mail.SmtpClient
    //     class.
    public void Dispose();
Run Code Online (Sandbox Code Playgroud)

  • 不适合我.嗯...它摆脱了异常,但现在它只是在发送一两封电子邮件时挂起. (3认同)

mar*_*net 5

我喜欢将它包装在 using 块中。这将强制处理并且非常优雅。

using(SmtpClient objCompose = new SmtpClient("xxxx"))
{
    objCompose.Send(mailmessage); 
}
Run Code Online (Sandbox Code Playgroud)