使用 VB.Net 编码"Pinging"电子邮件地址



有没有办法 VB.Net"ping"电子邮件地址以查看该电子邮件是否是没有给出任何错误的真实电子邮件?

如果是,您能否展示实现这一点的 VB.Net 编码是什么样子的?

我计划在需要客户电子邮件的应用程序中使用它,最好在呼叫接听者在保存客户详细信息之前将其输入表单时对其进行验证。

以下是我们用于向客户表中的所有客户发送电子邮件促销的代码:

Private Sub RibbonButtonSendTestEmail_Click(sender As System.Object, e As System.EventArgs) Handles RibbonButtonSendTestEmail.Click
    Dim SmtpServer As New SmtpClient()
    Dim mail As New MailMessage()
    Dim strSqlStatement As String = "Select CustomerName, Email " & _
                               "From Customers "
    Using objSqlCommand As SqlCommand = New SqlCommand(strSqlStatement, ObjConnection)
        With objSqlCommand
            ' Open the SqlConnection before executing the query.
            '---------------------------------------------------
            Cursor = Cursors.WaitCursor
            ObjConnection.Open()
            Dim objDataReader As SqlDataReader = .ExecuteReader()
            ' Go through all the customers and send out the promotion emails.
            '----------------------------------------------------------------
            If objDataReader.HasRows Then
                SmtpServer.Host = TextBoxSMTPServer.Text
                SmtpServer.Port = TextBoxPort.Text
                If TextBoxUseSSL.Text = "Yes" Then
                    SmtpServer.EnableSsl = True
                Else
                    SmtpServer.EnableSsl = False
                End If
                If TextBoxUseDefaultCredentials.Text = "Yes" Then
                    SmtpServer.UseDefaultCredentials = True
                Else
                    SmtpServer.UseDefaultCredentials = False
                End If
                SmtpServer.Credentials = New Net.NetworkCredential(TextBoxUserName.Text, TextBoxPassword.Text)
                While objDataReader.Read()
                    Try
                        mail.To.Add(objDataReader("Email").ToString)
                        mail.From = New MailAddress(TextBoxEmailFrom.Text)
                        mail.Subject = "Promotion: " & TextBoxID.Text
                        mail.Body = "Dear " & objDataReader("CustomerName") & "," & vbCrLf & vbCrLf & TextBoxPromotionBodyText.Text
                        SmtpServer.Send(mail)
                    Catch exSMTP As SmtpException
                        MessageBox.Show("Sorry, I could not send an email for: " & _
                                    vbCrLf & objDataReader("CustomerName") & "." & vbCrLf & _
                                    "Please make sure it is correct.", _
                                    "Error")
                    Catch exFormat As FormatException
                        MessageBox.Show("Sorry, this customer's email is not properly formatted: " & _
                                    vbCrLf & objDataReader("CustomerName") & "." & vbCrLf & _
                                    "Please make sure it is correct.", _
                                    "Error")
                    End Try
                End While
                LabelEmail.Text = "Sent email promotions to the customers."
            End If
            objDataReader.Close()
            ObjConnection.Close()
            Cursor = Cursors.Default
        End With ' objSqlCommand
    End Using ' objSqlCommand
End Sub

是的,这是完全可能的:

1 DNS 查找域的 MX 记录

  • 可能有多个,您可以选择任何人,尽管从技术上讲,偏好指标最低的那个是首选的。

2 TCP 连接到邮件服务器(端口 25)

  • 打个招呼:HELO
  • 表明自己的身份:mail from:<test@example.com>
  • 说出你要写信给谁:rcpt to<testaddress@example.com>
  • 此时服务器将回复响应,您将收到带有消息的OK550错误(例如:The email account that you tried to reach does not exist
  • 断开连接,消息将被丢弃。

但是伙计,你想让 VB 代码这样做吗? 你只需要一个DNS谈话片段和TCP连接构建片段(或者可能有一些SMTP库可以为你做这一切 - 或者为你提供灵感来自己弄清楚)。 不要忘记,你可能会找到一个C#代码示例来做到这一点,并使用Visual Studio的转换工具将其切换到VB。

注意

许多域名都有黑洞/捕获所有...前者将接受任何电子邮件地址,如果无效则将其删除,后者将接受任何电子邮件地址并将其转发到中央帐户(不保证会发生什么......将发件人的地址出售给垃圾邮件发送者? 在这两种情况下,您都不会收到550消息,因此您永远无法确定。

最可靠的方法是发送测试电子邮件,并让收件人通过单击链接来验证收据,然后您阅读该链接并将电子邮件标记为活动。

您应该使用正则表达式对电子邮件的语法进行原始检查,但除此之外,验证电子邮件的最可靠方法是尝试传递并确认收据。

相关内容

最新更新