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
答案 0 :(得分:4)
是的,这完全有可能:
1 DNS查找域的MX记录
2 TCP连接到邮件服务器(端口25)
HELO
mail from:<test@example.com>
rcpt to<testaddress@example.com>
OK
或550
错误消息(例如:The email account that you tried to reach does not exist
)但伙计,你想要VB代码吗?您只需要一个DNS谈话片段和TCP连接构建片段(或者可能有一些SMTP库可以为您完成所有这些 - 或者为您提供灵感来自己解决这个问题)。不要忘记你可能会找到一个C#代码示例,并使用Visual Studio的转换工具将其切换到VB。
注意强>
许多域名都有黑洞/捕获所有内容...前者将接受任何电子邮件地址,如果它无效则删除它,后者将接受任何电子邮件地址并将其转发到中央帐户(不保证会发生什么然后......将发件人的地址卖给垃圾邮件发送者?)在这两种情况下,你都不会得到550
邮件,所以你永远无法确定。
答案 1 :(得分:1)
最可行的方法是发送测试电子邮件,让收件人通过点击链接验证收据,然后阅读该链接并将电子邮件标记为有效。
您应该使用正则表达式对电子邮件的语法进行原始检查,但除此之外,验证电子邮件的最可靠方式是尝试传递并确认收据。