vb.net如何在获胜表格程序中检测电子邮件是否无法送达



我有以下代码:

Public Function VerstuurMail(ByVal strFrom As String, ByVal strTo As String, ByVal strSubject As String, ByVal strBody As String, ByVal strMailSMTP As String, ByVal MailUser As String, ByVal MailPassword As String, ByVal MailPort As Integer, Optional ByVal AttachmentFiles As String = "") As String
    Try
        'create the mail message
        Dim mail As New MailMessage()
        Dim basicCredential As New NetworkCredential(MailUser, MailPassword)
        'set the addresses
        mail.From = New MailAddress(strFrom)
        mail.To.Add(strTo)
        'set the content
        mail.Subject = strSubject
        If File.Exists(strBody) = True Then
            Dim objReader As New System.IO.StreamReader(strBody, System.Text.Encoding.GetEncoding(1252))
            mail.Body = objReader.ReadToEnd
            objReader.Close()
        End If
        mail.IsBodyHtml = False
        'send the message
        Dim smtp As New SmtpClient(strMailSMTP)
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network
        smtp.EnableSsl = True
        'smtp.UseDefaultCredentials = True
        smtp.Credentials = basicCredential
        smtp.Port = MailPort
        Dim AttachmentFile As String() = AttachmentFiles.Split("*")
        For Each bestand In AttachmentFile
            If System.IO.File.Exists(bestand) Then
                mail.Attachments.Add(New Attachment(bestand))
            Else
                Call MessageBox.Show("File can't be found")
            End If
        Next
        'Dim userState As Object = mail
        'smtp.SendAsync(mail, userState)
        'AddHandler smtp.SendCompleted, AddressOf SendCompletedCallback
        smtp.Send(mail)
        mailSent = True
        smtp.Dispose()
    Catch ex As Exception
        mailSent = False
        Call MessageBox.Show(ex.Message & vbCrLf & "Didn't sent to: " & strTo & vbCrLf & " with extra error message:" & vbCrLf & ex.ToString)
    Finally
    End Try
    Return mailSent
End Function

该函数用于读取一个文本文件的程序中,参数在一行上,并在有多少行时被调用。(循环)这很好用。

现在,当文本文件有一个错误的电子邮件地址时,该函数不会更正错误,它只是将电子邮件发送给任何人。

示例:将邮件发送到joe@gmail.com工作,发送电子邮件至joe@hmail.com没有发送,但也没有给出错误。

我在谷歌上搜索过,但示例中说我应该使用"smtp.SendAsync(mail,userState)"但是,程序不再遵循循环,也没有发送任何邮件。我无法使用调试器并逐步执行代码。它只是从一个地方跳到另一个地方。

这是另一个功能:

   Private Sub SendCompletedCallback(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
    Dim mail As MailMessage = CType(e.UserState, MailMessage)
    'write out the subject
    Dim subject As String = mail.Subject
    'If e.Cancelled Then
    '    Call MessageBox.Show("Send canceled: " & Now & " token " & subject)
    '    MailLog &= "Send canceled" & vbCrLf
    'End If
    If Not e.Error Is Nothing Then
        Call MessageBox.Show("Foutmelding op: " & Now & " onderwerp " & subject & " met error: " & e.Error.ToString())
        MailLog = MailLog & "Niet verstuurd met als fout melding: " & e.Error.ToString() & vbCrLf
    Else
        'Call MessageBox.Show("Message sent at: " & Now)
        MailLog = MailLog & "Bericht verstuurd op: " & Now & vbCrLf
    End If
    mailSent = True
End Sub

提前谢谢。我希望有人能把我带向正确的方向。

Brian

这是一个数据问题,而不是发送电子邮件的实际机制的问题。你能做的最好的事情就是使用正则表达式来确保电子邮件地址根据RFC 2822的规则是有效的,比如:

string email = txtemail.Text;
Regex regex = new Regex(@"^([w.-]+)@([w-]+)((.(w){2,3})+)$");
Match match = regex.Match(email);
if (match.Success)
{
    Response.Write(email + " is valid.");
}
else
{
    Response.Write(email + " is invalid.");
}

不幸的是,根据上述逻辑,joe@hmail.com是一个有效的电子邮件地址,但不是预期的joe@gmail.com地址,因此它最终出现在错误的位置。当发现它是错误的电子邮件地址时,将数据更改为正确的值是唯一正确的做法。


注意:一般情况下,当某人注册网站时,你会验证他们的电子邮件地址,因此系统";知道";电子邮件地址是合法/正确的,因为他们成功地收到了一封电子邮件,并输入了验证码,或者点击了一个链接,该链接与网站核实了他们确实收到了电子邮件。这解决了大多数数据问题(拼写错误、输入值错误等)

经过整整两天的测试和搜索,我发现在使用时

    Dim userState As Object = mail
    smtp.SendAsync(mail, userState)
    AddHandler smtp.SendCompleted, AddressOf SendCompletedCallback

您不应该使用smtp关闭smtpClient。Dispose()

回调函数不断得到取消错误。

我还使用了后台工作程序来发送许多邮件,但在某种程度上,async是一个后台工作程序。所以我有两个后台工作人员,但根本不起作用。

我花了两天时间。。。。。

Brian

相关内容

最新更新