2011-07-26 9 views
2

hello fellow :)失敗した電子メール受信者の送信を再試行するにはどうすればいいですか?私は複数のアドレスに電子メールを送信できるvbnetでアプリケーションを作成しようとしています。VBNET smtp再試行失敗した電子メールアドレス

いくつかのコードスニペット:

Dim SmtpServer As New SmtpClient() 
SmtpServer.Credentials = New Net.NetworkCredential(xInformation(0), xInformation(1)) 
SmtpServer.Port = CInt(xInformation(2)) 
SmtpServer.Host = xInformation(3) 
SmtpServer.EnableSsl = True 

Dim mail As New MailMessage() 
mail = New MailMessage 
mail.From = New MailAddress(xInformation(4), "Display Name") 
mail.CC.Add(xInformation(5)) ' i will make a loop here to add recipients 
mail.Subject = xInformation(6) 
mail.IsBodyHtml = True 
mail.Body = xInformation(7) 

SmtpServer.Send(mail) 

疑問が生じる:

1.) if i have to send, for instance, email to 5 recipients, and only 
     3 emails have been successfully sent, how can i know the 
     failed email addresses? 
2.) where is the failed email address stored? 
3.) what exceptions are needed to trapped this error? 

答えて

1

私はあなたが、あなたのコード内で必要になります送信されませんメールをこれらの例外をキャッチすることができるとは思いませんsmtpサーバをチェックしたいのですが、inetpub内にメールフォルダがあるはずです

\\ServerName\c$\Inetpub\mailroot

このフォルダの中にBadMailとDropのフォルダがあります。これらのフォルダの内容を見てください。あなたのVBコードは、有効な電子メールアドレスにアクセスすることはできません。SMTPアプリケーションがそれを処理すると失敗すると、SMTPメールを送信しようとします。あなたのコメントパー

Imports System.Net.Mail 
Imports System.Threading 
Imports System.Web.Configuration 

''' <summary> 
''' Provides a method for sending email. 
''' </summary> 
Public NotInheritable Class Email 
    Private Sub New() 
    End Sub 
    ''' <summary> 
    ''' Constructs and sends an email message. 
    ''' </summary> 
    ''' <param name="fromName">The display name of the person the email is from.</param> 
    ''' <param name="fromEmail">The email address of the person the email is from.</param> 
    ''' <param name="subject">The subject of the email.</param> 
    ''' <param name="body">The body of the email.</param> 
    Public Shared Sub Send(fromName As String, fromEmail As String, subject As String, body As String) 
     Dim message As New MailMessage() With { _ 
      Key .IsBodyHtml = False, _ 
      Key .From = New MailAddress(fromEmail, fromName), _ 
      Key .Subject = subject, _ 
      Key .Body = body _ 
     } 
     message.[To].Add(WebConfigurationManager.AppSettings("mailToAddresses")) 

     Dim originalRecipientCount As Integer = message.[To].Count 
     Dim failOnAnyAddress As Boolean = Convert.ToBoolean(WebConfigurationManager.AppSettings("failOnAnyAddress")) 

     Try 
      Send(message) 
     Catch generatedExceptionName As SmtpFailedRecipientException 
      If message.[To].Count = originalRecipientCount Then 
       ' all recipients failed 
       Throw 
      End If 

      If failOnAnyAddress Then 
       ' some (not ALL) recipients failed 
       Throw 
      End If 
     End Try 
    End Sub 

    Private Shared Sub Send(message As MailMessage) 
     Dim client As New SmtpClient() 

     Try 
      client.Send(message) 
     Catch ex As SmtpFailedRecipientsException 
      ' multiple fail 
      message.[To].Clear() 

      For Each sfrEx As SmtpFailedRecipientException In ex.InnerExceptions 
       CheckStatusAndReaddress(message, sfrEx) 
      Next 

      If message.[To].Count > 0 Then 
       ' wait 5 seconds, try a second time 
       Thread.Sleep(5000) 
       client.Send(message) 
      Else 
       Throw 
      End If 
     Catch ex As SmtpFailedRecipientException 
      ' single fail 
      message.[To].Clear() 

      CheckStatusAndReaddress(message, ex) 

      If message.[To].Count > 0 Then 
       ' wait 5 seconds, try a second time 
       Thread.Sleep(5000) 
       client.Send(message) 
      Else 
       Throw 
      End If 
     Finally 
      message.Dispose() 
     End Try 
    End Sub 

    Private Shared Sub CheckStatusAndReaddress(message As MailMessage, exception As SmtpFailedRecipientException) 
     Dim statusCode As SmtpStatusCode = exception.StatusCode 

     If statusCode = SmtpStatusCode.MailboxBusy OrElse statusCode = SmtpStatusCode.MailboxUnavailable OrElse statusCode = SmtpStatusCode.TransactionFailed Then 
      message.[To].Add(exception.FailedRecipient) 
     End If 
    End Sub 
End Class 

はvb.netにC#のから任意のコードを変換しますhttp://www.developerfusion.com/tools/convert/csharp-to-vb/

+0

ああ、このアプリケーションでは、私はバウンスバック、電子メールをチェックする必要はありません。 smtpがサーバーに正常に送信されたかどうかを確認するだけです。私はこの記事を見つけましたが、このC#クラスをどのように変換するのか分かりません。 http://leedumond.com/blog/retrying-mail-operations-in-net/ –

関連する問題