Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB
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!
Posted

1 solution

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.
 
Share this answer
 
Comments
tang404930947 26-May-12 2:14am    
A bad answer...
Sandeep Mewara 26-May-12 12:45pm    
Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900