VB.NET - Dns.GetHostEntry issue - Asked By Prem Rajani on 30-Jul-12 04:27 PM

Hi,

I followed the code as shown in MSDN example, but once I get to s.connect...nothing happens and after sometime it errors out saying not able to connect. Below is my code and let me know what exactly I am doing wrong


Public Shared Function Validate_Email_Address(ByVal inputEmail) As Boolean



Try



'Define those variables to be evaluated in the next for loop and



'then used to connect to the server. These variables are defined



'outside the for loop to make them accessible there after.


inputEmail =

"prem_rajani@yahoo.com"



Dim host As String() = (inputEmail.Split("@"c))



Dim hostname As String = host(1)



'Get DNS host information.



Dim IPhst As IPHostEntry = Dns.GetHostEntry("www." & hostname)



'Dim IPhst As IPHostEntry = Dns.GetHostEntry("www.yahoo.com")



'Evaluate the socket and receiving host IPAddress and IPEndPoint.



Dim endPt As New IPEndPoint(IPhst.AddressList(0), 25)



'Creates the Socket to send data over a TCP connection.



Dim s As New Socket(endPt.AddressFamily, SocketType.Stream, ProtocolType.Tcp)



'Connect to the host using its IPEndPoint.


s.Connect(endPt)


'Attempting to connect



If Not Check_Response(s, SMTPResponse.CONNECT_SUCCESS) Then


s.Close()


Return False



End If



'HELO server


Senddata(s,

String.Format("HELO {0}" & vbCr & vbLf, Dns.GetHostName()))



If Not Check_Response(s, SMTPResponse.GENERIC_SUCCESS) Then


s.Close()


Return False



End If



'Identify yourself



'Servers may resolve your domain and check whether



'you are listed in BlackLists etc.


Senddata(s,

String.Format("MAIL From: {0}" & vbCr & vbLf, "testexample@deepak.portland.co.uk"))



If Not Check_Response(s, SMTPResponse.GENERIC_SUCCESS) Then


s.Close()


Return False



End If



'Attempt Delivery (I can use VRFY, but most



'SMTP servers only disable it for security reasons)



Dim _To As String = inputEmail



Dim Tos As String() = _To.Split(New Char() {";"c})



For Each [To] As String In Tos


Senddata(s,

String.Format("RCPT TO: {0}" & vbCr & vbLf, [To]))



If Not Check_Response(s, SMTPResponse.GENERIC_SUCCESS) Then


s.Close()


Return False



End If



Next



'Senddata(s, address)



'If Not Check_Response(s, SMTPResponse.GENERIC_SUCCESS) Then



' s.Close()



' Return False



'End If



'Return (True)


Senddata(s,

"QUIT" & vbCr & vbLf)


Check_Response(s, SMTPResponse.QUIT_SUCCESS)

s.Close()


Return True



Catch ex As SocketException


Console.WriteLine(

"SocketException caught!!!")


Console.WriteLine((

"Source : " + ex.Source))


Console.WriteLine((

"Message : " + ex.Message))



Return False



Catch ex As ArgumentNullException


Console.WriteLine(

"ArgumentNullException caught!!!")


Console.WriteLine((

"Source : " + ex.Source))


Console.WriteLine((

"Message : " + ex.Message))



Return False



Catch ex As NullReferenceException


Console.WriteLine(

"NullReferenceException caught!!!")


Console.WriteLine((

"Source : " + ex.Source))


Console.WriteLine((

"Message : " + ex.Message))



Return False



Catch ex As Exception


Console.WriteLine(

"Exception caught!!!")


Console.WriteLine((

"Source : " + ex.Source))


Console.WriteLine((

"Message : " + ex.Message))



Return False



End Try



End Function


Peter Bromberg replied to Prem Rajani on 30-Jul-12 04:57 PM
Dim host As String() = inputEmail.Split('@')  ' use single quotes for char type.
Prem Rajani replied to Peter Bromberg on 31-Jul-12 10:32 AM
I tried what you said and it doesn't work. Syntax error ...expression expected. This is VB.Net just to let you know.
Peter Bromberg replied to Prem Rajani on 31-Jul-12 11:01 AM
Look, working example code:

Dim inputEmail as String = "pbromberg@gmail.com"
Dim host As String() = inputEmail.Split( "@".ToCharArray())  
Console.WriteLine(host(1))
Prem Rajani replied to Peter Bromberg on 31-Jul-12 01:22 PM
Hi Peter,

I did try what you suggested, but at line S.connect I get the same error. Do I have to mention SMTPServer name anywhere. Not sure why for port 25 it doesn't go beyond connect line.
Prem Rajani replied to Prem Rajani on 01-Aug-12 04:54 PM
Hi,

Any help would be appreciated.