从特定邮件服务器发送邮件时发生SmtpException

时间:2013-08-24 06:43:14

标签: asp.net c#-2.0

我的问题可能是重复的问题,但我没有找到解决问题的正确方法。这就是我要问的原因。

我有一个asp网站。我正在使用smtpclient发送消息。我的代码在gmail设置下工作正常。但是在生产服务器中他们使用的是Bellnetwork的(smtp10.on.aibn.com)邮件服务器。他们正在使用端口25,也没有使用SSL(电子邮件提供商不支持SSL)。但是相同的邮件设置在过去的2 - 3年内运行正常,但现在当我们从我们的网站发送一些消息(生产)1或2时失败发送出去,其他人会成功发出。奇怪的是,过去3年没有问题。我收到了失败邮件的以下错误。

例外:

1

 System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.
   at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine)
   at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine)
   at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)
   at System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String& response)
   at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception)
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   --- End of inner exception stack trace ---
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   at Handler.BLL.cSendMail.SendMail(String p_strFrom, String p_strDisplayName, String p_strTo, String p_strSubject, String p_strMessage, String strFileName)

2

System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.

3

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 67.69.240.69:25
System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 67.69.240.69:25
   at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)
   --- End of inner exception stack trace ---
   at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout)
   at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback)
   at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback)
   at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
   at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port)
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   --- End of inner exception stack trace ---
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   at Handler.BLL.cSendMail.SendMail(String p_strFrom, String p_strDisplayName, String p_strTo, String p_strSubject, String p_strMessage, String strFileName)

我的代码:

      public bool SendMail(string p_strFrom, string p_strDisplayName, string p_strTo, string p_strSubject, string p_strMessage , string strFileName)
     {
         try
         {
             p_strDisplayName = _DisplayName;
             string smtpserver = _SmtpServer;
             SmtpClient smtpClient = new SmtpClient();
             MailMessage message = new MailMessage();
             MailAddress fromAddress = new MailAddress(_From,_DisplayName);
             smtpClient.Host = _SmtpServer;
             smtpClient.Port = Convert.ToInt32(_Port);
             string strAuth_UserName = _UserName;
             string strAuth_Password = _Password;
             if (strAuth_UserName != null)
             {
                 System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(strAuth_UserName, strAuth_Password);
                 smtpClient.UseDefaultCredentials = false;
                 if (_SSL)
                 {
                     smtpClient.EnableSsl = true;
                 }
                 smtpClient.Credentials = SMTPUserInfo;
             }
             message.From = fromAddress;

             message.Subject = p_strSubject;
             message.IsBodyHtml = true;
             message.Body = p_strMessage;
             message.To.Add(p_strTo);
             try
             {
                 smtpClient.Send(message);                     
                 return true;
             }
             catch (SmtpException ee)
             {   
          Log.WriteSpecialLog("smtpClient mail sending first try failed : " + ee.ToString(),"");
          Log.WriteSpecialLog("status code : " + ee.StatusCode, ""); 
         }
       }                 

1 个答案:

答案 0 :(得分:0)

此代码对我有用

 public static string sendMail(string fromEmail, string toEmail, string subject, string body, string Name, string bcc="" , string replyTo="")
    {
            MailMessage mailer = new MailMessage
                                     {
                                         IsBodyHtml = true,
                                         From = new MailAddress(fromEmail, "yoursite.com"),
                                         Subject = subject,
                                         Body = body,
                                         BodyEncoding = Encoding.GetEncoding("utf-8")
                                     };

            mailer.To.Add(new MailAddress(toEmail, toEmail));

            //
            if (!string.IsNullOrEmpty(bcc))
            {
                mailer.Bcc.Add(bcc);
            }
            //
            if (!string.IsNullOrEmpty(replyTo))
            {
                mailer.Headers.Add("Reply-To", replyTo);
            }
            //
            AlternateView plainView = AlternateView.CreateAlternateViewFromString(Regex.Replace(body, "<(.|\\n)*?>", string.Empty), null, "text/plain");

            AlternateView htmlView = AlternateView.CreateAlternateViewFromString(body, null, "text/html");

            mailer.AlternateViews.Add(plainView);
            mailer.AlternateViews.Add(htmlView);

            SmtpClient smtp = new SmtpClient(ConfigurationManager.AppSettings["SMTPserver"]);
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            //smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
          NetworkCredential basicAuthenticationInfo = new NetworkCredential(ConfigurationManager.AppSettings["passMail"], ConfigurationManager.AppSettings["passKey"]);
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = basicAuthenticationInfo;
            smtp.EnableSsl = false;
            smtp.Port = ConfigurationManager.AppSettings["Port"];
            // 
                try
                {
                    smtp.Send(mailer);                   
                    return "Email sent successfully !";
                }
                catch (Exception ex)
                {
                      return "Failure in Email Message= !" + ex.message;
                }
    }