C# 使用SendAsync()异步发送邮件
代码:public static bool SendSystemMail(MyMail mail) {
try {
MailMessage mailobject = new MailMessage();
mailobject.Sender = new MailAddress(mail.UserName);
mailobject.From = new MailAddress(mail.ServiceMail, mail.ServiceName);
mailobject.To.Add(new MailAddress(mail.MailTo));
mailobject.Subject = mail.MailTitle;
mailobject.IsBodyHtml = true;
mailobject.BodyEncoding = System.Text.Encoding.UTF8;
mailobject.Body = mail.MailContent;
if (mailPriority == 0) {
mailobject.Priority = MailPriority.Normal;
}
if (mailPriority == 1) {
mailobject.Priority = MailPriority.Low;
}
if (mailPriority == 2) {
mailobject.Priority = MailPriority.High;
}
//网易163的SLL加密smtp商品为994
SmtpClient client = new SmtpClient(mail.ServiceMailS); // 587;//Gmail使用的端口
client.UseDefaultCredentials = true;
client.Credentials = new NetworkCredential(mail.UserName, mail.ServiceMailP);
//client.EnableSsl = true; //经过ssl加密 gmail是加密的
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Timeout = 10000000;
if (isSendAsync == 0) {
//发生异常,写入日志。
client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
client.SendAsync(mailobject, "");
}
if (isSendAsync == 1) {
client.Send(mailobject);
}
return true;
} catch (Exception ex) {
UtilityFile.AddLogErrMsg("SendMails", "SendMail" + ex);
return false;
}
}
private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e) {
String token = (string)e.UserState;
if (e.Cancelled) {
UtilityFile.AddLogErrMsg("SendMails", "Send Cancelled: " + token);
}
if (e.Error != null) {
Console.WriteLine("[{0}] {1}", token, e.Error.ToString());
UtilityFile.AddLogErrMsg("SendMails", "Send Error:" + e.Error.ToString() + " token: " + token);
}
}
请高手指点下,我使用微软的邮件发送类,异步发送邮件,在2003系统下完全没问题,在xp系统下发送失败
贴出失败日志
2012-6-14 9:20:34 帐号:service43@goodsraffe.com|||zhih_h@163.com
2012-6-14 9:20:34 Send Error:System.Net.Mail.SmtpException: 发送邮件失败。 ---> System.IO.IOException: 无法从传输连接中读取数据: 由于套接字没有连接并且(当使用一个 sendto 调用发送数据报套接字时)没有提供地址,发送或接收数据的请求没有被接受。。 ---> System.Net.Sockets.SocketException: 由于套接字没有连接并且(当使用一个 sendto 调用发送数据报套接字时)没有提供地址,发送或接收数据的请求没有被接受。
在 System.Net.Sockets.Socket.BeginReceive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, AsyncCallback callback, Object state)
在 System.Net.Sockets.NetworkStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)
--- 内部异常堆栈跟踪的结尾 ---
在 System.Net.Mail.SmtpConnection.ConnectAndHandshakeAsyncResult.End(IAsyncResult result)
在 System.Net.Mail.SmtpClient.ConnectCallback(IAsyncResult result)
--- 内部异常堆栈跟踪的结尾 --- token:
补充:.NET技术 , .NET技术前瞻