10

当我经常向用户列表发送一些电子邮件时,我收到此错误。假设它发送了 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;
    }

我得到的错误是:

System.Net.Mail.SmtpException:服务不可用,正在关闭传输通道。服务器响应为:4.4.2 mailer.mailer.com 错误:System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[ ] 命令,字符串来自)在 S​​ystem.Net.Mail.SmtpTransport.SendMail(MailAddress 发件人,MailAddressCollection 收件人,字符串 deliveryNotify,SmtpFailedRecipientException& 异常)在 System.Net.Mail.SmtpClient.Send(MailMessage 消息)

任何人都可以请帮助,这个错误正在杀死我。

提前致谢。

4

2 回答 2

11

处理 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();
于 2012-02-15T12:12:57.203 回答
6

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

using(SmtpClient objCompose = new SmtpClient("xxxx"))
{
    objCompose.Send(mailmessage); 
}
于 2013-02-15T14:01:20.867 回答