当我尝试使用 smtp 发送消息时收到以下错误消息。
连接尝试失败,因为连接方在一段时间后没有正确响应,或者连接失败,因为连接的主机没有响应 67.69.240.69:25 System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: 连接尝试失败,因为连接方在一段时间后没有正确响应,或者连接失败,因为连接的主机未能响应 67.69.240.69:25 在 System.Net.Sockets.Socket.DoConnect(端点 endPointSnapshot,SocketAddress 套接字地址) 在 System.Net.ServicePoint.ConnectSocketInternal(布尔连接失败,套接字 s4,套接字 s6,套接字和套接字,IPAddress 和地址,ConnectSocketState 状态,IAsyncResult asyncResult,Int32 超时,异常和异常) --- 内部异常堆栈跟踪结束 --- 在 System.Net.ServicePoint.GetConnection(PooledStream PooledStream,对象所有者,布尔异步,IPAddress& 地址,Socket& abortSocket,Socket& abortSocket6,Int32 超时) 在 System.Net.PooledStream.Activate(对象 owningObject,布尔异步,Int32 超时,GeneralAsyncDelegate asyncCallback) 在 System.Net.PooledStream.Activate(对象拥有对象,GeneralAsyncDelegate asyncCallback) 在 System.Net.ConnectionPool.GetConnection(对象 owningObject,GeneralAsyncDelegate asyncCallback,Int32 creationTimeout) 在 System.Net.Mail.SmtpConnection.GetConnection(字符串主机,Int32 端口) 在 System.Net.Mail.SmtpClient.Send(MailMessage 消息) --- 内部异常堆栈跟踪结束 --- 在 System.Net.Mail.SmtpClient.Send(MailMessage 消息) 在 Handler.BLL.cSendMail.SendMail(字符串 p_strFrom,字符串 p_strDisplayName,字符串 p_strTo,字符串 p_strSubject,字符串 p_strMessage,字符串 strFileName) [staffID:,21/08/2013 9:49:34 AM]:状态代码:GeneralFailure
代码
public bool SendMail(string p_strFrom, string p_strDisplayName, string p_strTo, string p_strSubject, string p_strMessage , string strFileName)
{
try
{
p_strDisplayName = _DisplayName;
string smtpserver = _SmtpServer;
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress(_From,_DisplayName);
smtpClient.Host = _SmtpServer;
smtpClient.Port = Convert.ToInt32(_Port);
string strAuth_UserName = _UserName;
string strAuth_Password = _Password;
if (strAuth_UserName != null)
{
System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(strAuth_UserName, strAuth_Password);
smtpClient.UseDefaultCredentials = false;
if (_SSL)
{
smtpClient.EnableSsl = true;
}
smtpClient.Credentials = SMTPUserInfo;
}
message.From = fromAddress;
message.Subject = p_strSubject;
message.IsBodyHtml = true;
message.Body = p_strMessage;
message.To.Add(p_strTo);
try
{
smtpClient.Send(message);
Log.WriteSpecialLog("smtpClient mail sending first try success", "");
}
catch (Exception ee)
{
Log.WriteSpecialLog("smtpClient mail sending first try Failed : " + ee.ToString(), "");
return false;
}
return true;
}
catch (Exception ex)
{
Log.WriteLog("smtpClient mail sending overall failed : " + ex.ToString());
return false;
}
}
当 3 封或更多邮件仅发送一两封失败时