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;
}
}
错误是
"Failure sending mail."
我不能发送任何。 有什么问题?
内部异常是
System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: 连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败因为连接的主机未能在 System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) 在 System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP) 在 System.Net.ServicePoint 响应 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,对象所有者, bool 异步,IPAddress&地址,Socket&abortSocket,Socket&abortSocket6,Int32超时)在System.Net.PooledStream.Activate(对象owningObject, bool 异步,Int32超时,GeneralAsyncDelegate asyncCallback)在System.Net.PooledStream.Activate (对象 owningObject,GeneralAsyncDelegate asyncCallback)在 System.Net.ConnectionPool.GetConnection(对象 owningObject,GeneralAsyncDelegate asyncCallback,Int32 creationTimeout)在 System.Net.Mail.SmtpConnection.GetConnection(字符串主机,Int32 端口)在 System.Net.Mail.SmtpTransport System.Net.Mail.SmtpClient.GetConnection() 在 System.Net.Mail.SmtpClient.Send(MailMessage 消息)中的 .GetConnection(String host, Int32 port)
请您参考如下方法:
对于 SmtpException ,您总是必须通过 Exception.InnerException 检查内部异常属性(property)。总是。
