【问题标题】:SmtpException while sending mails from a particular mailing server从特定邮件服务器发送邮件时出现 SmtpException
【发布时间】:2013-08-27 06:28:36
【问题描述】:

我的问题可能是重复的,但我没有为我的问题找到合适的解决方案。这就是我问的原因。

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

例外情况:

1.

 System.IO.IOException:无法从传输连接读取数据:net_io_connectionclosed。
   在 System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(字节 [] 缓冲区,Int32 偏移量,Int32 读取,布尔读取行)
   在 System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader 调用者,布尔 oneLine)
   在 System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader 调用者)
   在 System.Net.Mail.CheckCommand.Send(SmtpConnection 连接,字符串和响应)
   在 System.Net.Mail.SmtpTransport.SendMail(MailAddress 发件人,MailAddressCollection 收件人,字符串 deliveryNotify,SmtpFailedRecipientException& 异常)
   在 System.Net.Mail.SmtpClient.Send(MailMessage 消息)
   --- 内部异常堆栈跟踪结束 ---
   在 System.Net.Mail.SmtpClient.Send(MailMessage 消息)
   在 Handler.BLL.cSendMail.SendMail(字符串 p_strFrom,字符串 p_strDisplayName,字符串 p_strTo,字符串 p_strSubject,字符串 p_strMessage,字符串 strFileName)

2.

System.IO.IOException:无法从传输连接读取数据:net_io_connectionclosed。

3.

连接尝试失败,因为连接方在一段时间后没有正确响应,或者连接失败,因为连接的主机没有响应 67.69.240.69:25 System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: 连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立连接失败,因为连接的主机未能响应 67.69.240.69:25 在 System.Net.Sockets.Socket.DoConnect(端点 endPointSnapshot,SocketAddress socketAddress) 在 System.Net.ServicePoint.ConnectSocketInternal(布尔连接失败、套接字 s4、套接字 s6、套接字和套接字、IPAddress 和地址、ConnectSocketState 状态、IAsyncResult asyncResult、Int32 超时、异常和异常) --- 内部异常堆栈跟踪结束 --- 在 System.Net.ServicePoint.GetConnection(PooledStream PooledStream,对象所有者,布尔异步,IPAddress& 地址,Socket& abortSocket,Socket& abortSocket6,Int32 超时) 在 System.Net.PooledStream.Activate(对象拥有对象,布尔异步,Int32 超时,GeneralAsyncDelegate asyncCallback) 在 System.Net.PooledStream.Activate(对象拥有对象,GeneralAsyncDelegate asyncCallback) 在 System.Net.ConnectionPool.GetConnection(对象 owningObject,GeneralAsyncDelegate asyncCallback,Int32 creationTimeout) 在 System.Net.Mail.SmtpConnection.GetConnection(字符串主机,Int32 端口) 在 System.Net.Mail.SmtpClient.Send(MailMessage 消息) --- 内部异常堆栈跟踪结束 --- 在 System.Net.Mail.SmtpClient.Send(MailMessage 消息) 在 Handler.BLL.cSendMail.SendMail(字符串 p_strFrom,字符串 p_strDisplayName,字符串 p_strTo,字符串 p_strSubject,字符串 p_strMessage,字符串 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, ""); 
         }
       }                 

【问题讨论】:

  • 这是端口错误 smtpClient.Port = Convert.ToInt32(_Port);更改端口号
  • 我们目前正在使用端口 25。我必须尝试哪个端口?
  • 你用的是gmail还是godaddy?
  • 我正在使用 smtp10.on.aibn.com
  • 对不起,我不明白您必须告诉您发送电子邮件的来源,即 gmail 或 godaddy 您自己的电子邮件,或者您可以告诉电子邮件地址@domain.com,然后我可以告诉端口跨度>

标签: asp.net c#-2.0


【解决方案1】:

这段代码对我有用

 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;
                }
    }

【讨论】:

    猜你喜欢
    • 2015-09-13
    • 1970-01-01
    • 2016-01-02
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多