0

私の質問は重複している可能性がありますが、私の問題に対する適切な解決策がありませんでした.それが私が尋ねている理由です.

私は 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 (Byte[] バッファー、Int32 オフセット、Int32 読み取り、ブール値の readLine) で
   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: リモート サーバーに接続できません ---> System.Net.Sockets.SocketException: 接続先が一定時間後に適切に応答しなかったため、接続の試行が失敗したか、接続されたホストのために確立された接続が失敗しました67.69.240.69:25 に応答できませんでした
   System.Net.Sockets.Socket.DoConnect (エンドポイント endPointSnapshot、SocketAddress socketAddress) で
   System.Net.ServicePoint.ConnectSocketInternal (ブール値の connectFailure、ソケット s4、ソケット s6、Socket& ソケット、IPAddress& アドレス、ConnectSocketState 状態、IAsyncResult asyncResult、Int32 タイムアウト、Exception& 例外) で
   --- 内部例外スタック トレースの終了 ---
   System.Net.ServicePoint.GetConnection (PooledStream PooledStream、オブジェクトの所有者、ブール値の非同期、IPAddress& アドレス、Socket& abortSocket、Socket& abortSocket6、Int32 タイムアウト) で
   System.Net.PooledStream.Activate (オブジェクト owningObject、ブール値の非同期、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, ""); 
         }
       }                 
4

1 に答える 1

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;
                }
    }
于 2013-08-24T08:28:49.933 に答える