当我经常向用户列表发送电子邮件时,我收到此错误。假设它发送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;
}
我得到的错误是:
任何人都可以请求帮助,这个bug就是杀了我。System.Net.Mail.SmtpException:服务不可用,关闭传输通道。服务器响应是:4.4.2 mailer.mailer.com错误:超时超时 在System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode,String response) 在System.Net.Mail.MailCommand.Send(SmtpConnection conn,Byte []命令,字符串来自) 在System.Net.Mail.SmtpTransport.SendMail(MailAddress sender,MailAddressCollection recipients,String deliveryNotify,SmtpFailedRecipientException& exception) 在System.Net.Mail.SmtpClient.Send(MailMessage消息)
提前致谢。
答案 0 :(得分: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();
答案 1 :(得分:4)
我喜欢将它包装在一个使用块中。这将迫使处置,它非常优雅。
using(SmtpClient objCompose = new SmtpClient("xxxx"))
{
objCompose.Send(mailmessage);
}