VBNET smtp重试发送失败的电子邮件地址

时间:2011-07-26 14:22:56

标签: vb.net smtp sendmail

你好伙伴:)我在如何重试发送失败的电子邮件收件人时遇到麻烦。我正在尝试在vbnet中创建一个应用程序,我可以将电子邮件发送到多个地址。

一些代码段:

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 个答案:

答案 0 :(得分: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/