2011-12-20 15 views
2

私はasp.netのWebサイトを持っています。 SMTPClientでメールを送信しています。そして時々私はこのエラーを得ている:.NET 2.0で電子メールを送信すると「既存の接続が強制的に閉じられました」エラー

InnerException = System.IO.IOException: Unable to read data from the transport connection: 
    An existing connection was forcibly closed by the remote host. ---> 
     System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host 
    at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) 
    at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) 
    --- End of inner exception stack trace --- 
    at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) 
    at System.Net.DelegatedStream.Read(Byte[] buffer, Int32 offset, Int32 count) 
    at System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset, Int32 count) 
    at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine) 
    at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller) 
    at System.Net.Mail.SmtpReplyReader.ReadLine() 
    at System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String& response) 
    at System.Net.Mail.DataStopCommand.Send(SmtpConnection conn) 
    at System.Net.Mail.SmtpConnection.OnClose(Object sender, EventArgs args) 
    at System.Net.ClosableStream.Close() 
    at System.Net.Mail.MailWriter.Close() 
    at System.Net.Mail.SmtpClient.Send(MailMessage message). 

コード:

SmtpClient smtpClient = new SmtpClient(ConfigurationManager.AppSettings["smtpserverwwwpo"]); 
    try 
    { 
     NetworkCredential basicCredential = 
      new NetworkCredential(ConfigurationManager.AppSettings["smtp_user_name"], ConfigurationManager.AppSettings["smtp_password"]); 
     smtpClient.UseDefaultCredentials = false; 
     smtpClient.Credentials = basicCredential; 
    } 
    catch 
    { 
     smtpClient.UseDefaultCredentials = true; 
    } 

    System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(); 
    msg.Subject = subject; 
    msg.To.Add(new MailAddress(someEmail)); 
    msg.From = new MailAddress(ConfigurationManager.AppSettings["from_email_address"]); 
    msg.Headers.Add("Content-Type", "text/html"); 
    msg.BodyEncoding = System.Text.Encoding.UTF8; 

try{ 
     smtpClient.Send(msg); 
} 
catch(Exception ex) 
{ 
//write log 
} 

}

何の理由、それを引き起こす可能性がありますか?

+5

SMTPサーバーが接続を閉じたように見えます。サーバーのログを確認することがありますか? – David

+2

code ??????????? –

+0

ソケットを使用してメールを送信しているか、smtp/System.Net.Mailを使用してメールを送信していますか?どのようなコードを投稿してください。.NET経由で電子メールを送信するのは簡単で簡単です。 – MethodMan

答えて

0

たぶん、あなたはfalseにSmtpClient.EnableSslを設定する必要があります。

3

MailMessageは使い捨てです。あなたはusingステートメントでそれをラップすることができます。

using(var message = new MailMessage {Subject ="...", Body="..."}) 
{ 
    message.To.Add("email address"); 
    new SmtpClient().Send(message) 
} 
0

ここで、SMTPクライアントのホスト名を割り当てていますか。あなたがネットワーク資格情報が必要な場合、私はあなたの仕様に精通していないですが、私は私のものを使用して試験し、このコードはちょうどそれを試してみましたし、それが動作するかわからない、次を使用するようにコードを変更した場合どのような

..

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); 
message.To.Add("youremailAddress"); 
message.Subject = "This is the Subject line"; 
message.From = new System.Net.Mail.MailAddress("[email protected]"); 
message.Body = "This is the message body"; 
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("yoursmtphost"); 
smtp.Send(message); 
+0

アプリケーションは実稼働中です。 資格情報を設定する必要があります。 – Roman

+0

これは、有効なネットワークユーザでないか、有効なシステムアカウントを持っていれば、送信しようとするとメールが失敗することを意味します。私は間違っている可能性がありますが、あなたがする必要があるすべてがAdd()部分の電子メールアドレスを渡すのであれば、新しいMailAddressをなぜ追加しますか? – MethodMan

1

あなたが投稿した実際のコードや、接続しているサーバーによって解釈されるように、リクエストの量と内容に関係することはほとんどありません。接続を強制的にクローズすることは意図的な行為であり、ランダムエラーではありません。つまり、サーバーはいくつかの条件を満たすことを選択しています。

おそらくあなたは、あなたの知らない、いくつかの制限に達している、上で示唆したように、おそらくリモートサーバーがビジー状態で、接続を閉じる(その場合、適切なエラーコードを返さない)場合があります。

-1

SMTPClientクラスはIDisposableインターフェイスを実装しています。 TypeがIDisposableを実装するときは、(){}ステートメントを使用する必要があります。

EG-

using (SmtpClient SmtpServer = new SmtpClient(smtpConfig.SmtpMailServer)) 
        { 
         //your code 
        } 
関連する問題