2011-07-26 60 views
2

你好老友:)我有麻煩我怎麼才能重試發送失敗的電子郵件收件人。我正在嘗試在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 

從C#轉換任何代碼,vb.net:http://www.developerfusion.com/tools/convert/csharp-to-vb/

+0

哦,在這個應用程序,我並不需要檢查的反彈電子郵件。我只需要檢查smtp是否成功發送到服務器。我發現這篇文章,但我不知道如何轉換這個C#類。 http://leedumond.com/blog/retrying-mail-operations-in-net/ –