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)

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

  • 相关阅读:
    python 时间 时间戳 转换
    jsp mysql
    multi struts config
    mysql start
    struts logic tag
    jsp setProperty
    jstl fn tag
    write jsp tag
    use Bean in JSP
    jsp mysql JavaBean
  • 原文地址:https://www.cnblogs.com/lixipeng/p/7717663.html
Copyright © 2011-2022 走看看