vb.net如何检测电子邮件是否在win表单程序中无法传送

时间:2013-12-11 15:13:04

标签: vb.net email

我有以下代码:

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没有发送,但也没有给出错误。

我用google搜索但是示例说我应该使用'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

提前致谢。我希望有人能把我放在正确的方向上。

布赖恩

2 个答案:

答案 0 :(得分:0)

这是一个数据问题,而不是发送电子邮件的实际机制的问题。您可以做的最好的事情是使用正则表达式来确保电子邮件地址符合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地址,因此最终会出错。当发现它是错误的电子邮件地址时,将数据更改为正确的值是唯一正确的操作过程。


  

注意:通常,您在注册网站时验证某人的电子邮件地址,因此系统“知道”该电子邮件地址是合法/正确的,因为他们已成功收到电子邮件并输入验证码或点击验证的链接与他们确实收到电子邮件的网站。这解决了大多数数据问题(拼写错误,错误输入的值等)。

答案 1 :(得分:0)

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

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

你不应该用smtp.Dispose()

关闭smtpClient

回调函数一直收到取消错误。

此外,我使用backgroundworker发送了许多邮件,但async在某种程度上是一个后台工作者。所以我有两个背景工作者,但根本没用。

我花了两天时间......

布赖恩