我们使用SmtpClient.SendAsync异步发送电子邮件。 这是完整的源代码SendAsync和SendCompletedCallback
SmtpClient m_SmtpClient = null;
MailMessage m_MailMessage = null;
public void SendAsync(string subject, string body)
{
try
{
m_SmtpClient = new SmtpClient(SMTPHost, Convert.ToInt32(SMTPPort));
m_SmtpClient.EnableSsl = true;
m_SmtpClient.Credentials = new NetworkCredential(SMTPEmailFrom, SMTPPassword);
m_SmtpClient.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
m_MailMessage = new System.Net.Mail.MailMessage(SMTPEmailFrom, SMTPEmailTo, subject, body);
m_SmtpClient.SendAsync(m_MailMessage, null);
log.InfoFormat("SENDING ASYNC E-MAIL to {0} {1},{2}", SMTPEmailTo, subject, body);
}
catch (InvalidOperationException ex)
{
log.ErrorFormat(ex.Message);
}
}
private void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
String token = (string)e.UserState;
if (e.Cancelled)
{
log.InfoFormat("[{0}] Send canceled.", token);
}
if (e.Error != null)
{
log.ErrorFormat("[{0}] {1}", token, e.Error.ToString());
}
else
{
log.InfoFormat("SENDING ASYNC E-MAIL: COMPLETED");
}
m_MailMessage.Dispose();
m_SmtpClient.Dispose();
}
该程序允许发送30-40封电子邮件,然后停止工作。 使用Wireshark嗅探器确认SMTP服务器接受TCP连接但从不发送SMTP 220消息。 在客户端站点上,我们永远不会收到回调,也无法处置现有的smpt客户端对象。在这种情况之后,在我们重新启动程序之前,smpt客户端无法发送任何电子邮件。
感谢您的任何建议。