1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| Public Function SMTPIsAvailable(ByVal smtp As String, Optional ByVal port As Integer = 25) As Boolean
Try
Dim tcp As New System.Net.Sockets.TcpClient
tcp.Connect(smtp, port)
If tcp.Connected Then
Dim b() As Byte
b = System.Text.Encoding.ASCII.GetBytes("EHLO test.com" & vbCrLf)
tcp.GetStream.Write(b, 0, b.Length)
Dim reponse As String = Nothing
Dim chrono As New System.Diagnostics.Stopwatch
chrono.Start()
While chrono.ElapsedMilliseconds < 800
If tcp.Available = 0 Then Continue While
Dim r(0) As Byte
tcp.GetStream.Read(r, 0, 1)
reponse &= Chr(r(0))
End While
If reponse Like ("220*") Then
tcp.Close()
tcp = Nothing
Return True
Else
Return False
End If
Else
tcp.Close()
tcp = Nothing
Return False
End If
Catch ex As Exception
Return False
End Try
End Function |
Partager