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:
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)
str = SenSmtpCmd(tcpc, Convert.ToBase64String(Encoding.Default.GetBytes(_email)))
MsgBox(str)
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()
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!