【发布时间】:2011-06-15 11:24:19
【问题描述】:
protected void SendEmail(object sender, EventArgs e)
{
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress("fromemail", "From Me");
MailAddress toAddress = new MailAddress("toemail", "To You");
message.From = fromAddress;
message.To.Add(toAddress);
message.Subject = "Testing!";
message.Body = "This is the body of a sample message";
smtpClient.Host = "smtp.host.com";
smtpClient.Credentials = new System.Net.NetworkCredential("username", "password");
smtpClient.EnableSsl = true;
smtpClient.Port = 587;
smtpClient.Send(message);
statusLabel.Text = "Email sent.";
}
catch (Exception ex)
{
statusLabel.Text = "Coudn't send the message!\n"+ex.Message;
}
}
错误是
“发送邮件失败。”
我不能发送任何东西。 有什么问题?
内部异常是
System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: 连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立连接失败因为连接的主机未能在 System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP) 在 System.Net.ServicePoint 的 System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) 响应 74.125.39.109:587。 ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception) --- 内部异常堆栈跟踪结束---在 System.Net.ServicePoint.GetConnection( PooledStream PooledStream,对象所有者,布尔异步,IPAddress 和地址,套接字和 abortSocket,套接字和 abortSocket6,Int32 超时)在 System.Net.PooledStream.Activate(对象拥有对象,布尔异步,Int32 超时,常规AsyncDelegate asyncCallback) 在 System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) 在 System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) 在 System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 端口)在 System.Net.Mail.SmtpTransport.GetConnection(字符串主机,Int32 端口)在 System.Net.Mail.SmtpClient.GetConnection() 在 System.Net.Mail.SmtpClient.Send(MailMessage 消息)
【问题讨论】:
-
那么你得到了什么异常(如果有的话)?
-
请包含整个 ex.ToString() 的输出。实际问题可能在内部异常中描述,需要诊断整个异常输出。