发邮件,阿里云,未指定邮件服务器端口导致的报错

public static void send(string from, string to, string body, string subject, string file_path)
        {
            try
            {
                Service1.logger.Debug("Send Email Start;" + to);
//SmtpClient smtp = new SmtpClient(邮箱服务器ip);//未指定端口导致了下面的报错,改为下面的写法,多了指定port
SmtpClient smtp = new SmtpClient(邮箱服务器ip,邮箱服务器port端口); 
smtp.EnableSsl
= true; smtp.UseDefaultCredentials = false;
smtp.Credentials
= new NetworkCredential(邮箱账号, 邮箱密码);
MailMessage message
= new MailMessage(from, to);
message.Subject
= subject;
message.IsBodyHtml
= true;
message.Body
= body;
Attachment attachment
= new Attachment(file_path);
message.Attachments.Add(attachment);
smtp.Send(message);
Service1.logger.Debug(
"Send Email End;" + to);
}

catch (Exception ex) {
// 记录错误日志 Service1.logger.Debug("Send Email Fail;" + ex.ToString());
}
}

System.Net.Mail.SmtpException: 发送邮件失败。 ---> System.Net.WebException: 无法连接到远程服务器 ---> System.Net.Sockets.SocketException: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。 IP    XXXX:端口xxx
在 System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
在 System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
--- 内部异常堆栈跟踪的结尾 ---
在 System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6)
在 System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback)
在 System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
在 System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
在 System.Net.Mail.SmtpClient.GetConnection()
在 System.Net.Mail.SmtpClient.Send(MailMessage message)
--- 内部异常堆栈跟踪的结尾 ---
在 System.Net.Mail.SmtpClient.Send(MailMessage message)

 

在阿里云上使用时最好指定端口来发邮件

posted @ 2017-10-23 16:50  李大飞  阅读(664)  评论(0编辑  收藏  举报