zoukankan      html  css  js  c++  java
  • 发邮件,阿里云,未指定邮件服务器端口导致的报错

    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)

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

  • 相关阅读:
    Linnia学习记录
    漫漫考研路
    ENS的学习记录
    KnockoutJS 3.X API 第四章 数据绑定(4) 控制流with绑定
    KnockoutJS 3.X API 第四章 数据绑定(3) 控制流if绑定和ifnot绑定
    KnockoutJS 3.X API 第四章 数据绑定(2) 控制流foreach绑定
    KnockoutJS 3.X API 第四章 数据绑定(1) 文本及样式绑定
    KnockoutJS 3.X API 第三章 计算监控属性(5) 参考手册
    KnockoutJS 3.X API 第三章 计算监控属性(4)Pure computed observables
    KnockoutJS 3.X API 第三章 计算监控属性(3) KO如何实现依赖追踪
  • 原文地址:https://www.cnblogs.com/lixipeng/p/7717663.html
Copyright © 2011-2022 走看看