如何验证指定的SMTP服务器,端口,用户名和密码有效或存在(使用.net语言)? [英] How to validate the specified SMTP server,port,username and password is effective or exist(use .net language)?
本文介绍了如何验证指定的SMTP服务器,端口,用户名和密码有效或存在(使用.net语言)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
How to validate the specified SMTP server,port,username and password is effective or exist(use .net language)?
For example:
smtp.mail.yahoo.com(SMTPserver),465(port),lezatuqg@yahoo.com(username),hb33qtm(password)
If this account is effective or exist ,return true(Can include a return message better),orelse return false.
My code:
''' <summary>
''' validate the specified SMTP server,port,username and password is effective or exist
''' </summary>
''' <param name="_server">smtp server</param>
''' <param name="_port">smtp port</param>
''' <param name="_email">Email Account</param>
''' <param name="_pass">Email pwd</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function ValiateEmail(ByVal _server As String, ByVal _port As String, ByVal _email As String, ByVal _pass As String) As String
Try
Dim str As String = ""
Dim tcpc As New TcpClient()
tcpc.SendTimeout = 3000
tcpc.Connect(_server, _port)
Dim sr As New StreamReader(tcpc.GetStream(), Encoding.Default)
Str = sr.ReadLine()
If CInt(Str.Substring(0, 3)) <> 220 Then
SenSmtpCmd(tcpc, "QUIT")
Return 0 & vbTab & "SMTP server does not exist"
End If
Str = SenSmtpCmd(tcpc, "EHLO " & _server)
If CInt(Str.Substring(0, 3)) <> 250 Then
SenSmtpCmd(tcpc, "QUIT")
Return 0 & vbTab & "EHLO Command not complete, the port may not provide SMTP service"
End If
str = SenSmtpCmd(tcpc, "AUTH LOGIN")
MsgBox(str) 'Return 334,it's ok
str = SenSmtpCmd(tcpc, Convert.ToBase64String(Encoding.Default.GetBytes(_email)))
MsgBox(str) 'Return null,Why is there an empty value
str = SenSmtpCmd(tcpc, Convert.ToBase64String(Encoding.Default.GetBytes(_pass)))
MsgBox(str)
If CInt(Str.Substring(0, 3)) <> 235 Then
SenSmtpCmd(tcpc, "QUIT")
Return 0 & vbTab & "User name or password is incorrect"
End If
SenSmtpCmd(tcpc, "QUIT")
Return 1 & vbTab & "Effective"
Catch ex As Exception
Return 0 & vbTab & ex.Message
End Try
End Function
Private Function SenSmtpCmd(ByVal tcpc As TcpClient, ByVal strCmd As String) As String
Dim arrCmd As Byte()
Dim stateRet As String
Dim sr As StreamReader
Dim s As Stream
s = tcpc.GetStream() 'Sometimes,get null value,why???
strCmd = strCmd & vbCr & vbLf
arrCmd = Encoding.[Default].GetBytes(strCmd.ToCharArray())
s.Write(arrCmd, 0, strCmd.Length)
s.Flush()
sr = New StreamReader(tcpc.GetStream(), Encoding.Default)
stateRet = sr.ReadLine() '
Return stateRet
End Function
VB 2005,Help!
推荐答案
如何验证指定的SMTP服务器,端口,用户名和密码有效或存在
对于SMTP服务器,端口:
如果使用公司服务器,则需要与IT管理员联系.
如果使用第三方服务器,则需要查看第三方的电子邮件配置.例如:GMail,Yahoo等.
对于用户名,密码:
您应该自己使用电子邮件功能.从管理员那里获取或在第三方站点进行注册.
How to validate the specified SMTP server,port,username and password is effective or exist
For SMTP server, Port:
You need to talk to your IT admin for it if you are using a company server.
You need to look at the email configuration of third party if you are using their server. example: GMail, Yahoo, etc.
For Username, Password:
You should have one for yourself to use the email facility. Get from your admin or register at third part site for it.
这篇关于如何验证指定的SMTP服务器,端口,用户名和密码有效或存在(使用.net语言)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文