I am sending semi automated emails within my access database using our companies internal smtp server.
One of the issues I had was capturing whether people were connected to the lan to be able to send these emails.
I decided to use a ping to test whether the server was giving a response, if it was then they must be connected and if not then they arent.
The ping code is as follows:
This worked perfeclty fine when I was working from home, the ping was not being returned so ofcourse the smtp server was unavailable.
I also tested the ping against different websites and I was returned pings with as expected latency values.
I have now come in to work and my code, even though connected to the network is returning 0 for the ping of the smtp.
This is causing my email sending script to believe that there is not a connection to the smtp server:
However as the above code shows I explicity state -1 value has to be returned. But for some reason 0 also causes the msgbox to pop up :|
I have tried pinging various websites using the code in the 1st part of this post and they all return -1, whereas they did not when I was at home. Any ideas why this might be happening?
=== Edit
After a bit more testing it seems the value returning 0 jsut means there is 0 latency due to the nserver being within my network:
NUTS1ETA is the SMTP server where the full address is: NUTS1ETA.xxx.xx xxx.net
w7bc-p1595 is my computer. Both pings and lookups via IP cause a value of 0 to be returned. Weirdly though I do not know why I can not ping normal websites such as www.yahoo.com etc.. they all return a -1 yet when I am at home they return a value such as 40, 74, 88 etc etc which I assumed were milliseconds for the ping reply.
One of the issues I had was capturing whether people were connected to the lan to be able to send these emails.
I decided to use a ping to test whether the server was giving a response, if it was then they must be connected and if not then they arent.
The ping code is as follows:
Code:
Option Explicit 'Ping function. Original Source code taken from [url]www.allapi.com[/url]. 'Modified into a function and tested for VBA compatability by Jeminar 22May04 Const SOCKET_ERROR = 0 Private Type WSADATA wVersion As Integer wHighVersion As Integer szDescription(0 To 255) As Byte szSystemStatus(0 To 128) As Byte iMaxSockets As Integer iMaxUdpDg As Integer lpVendorInfo As Long End Type Private Type Hostent h_name As Long h_aliases As Long h_addrtype As Integer h_length As Integer h_addr_list As Long End Type Private Type IP_OPTION_INFORMATION Ttl As Byte Tos As Byte Flags As Byte OptionsSize As Long OptionsData As String * 128 End Type Private Type IP_ECHO_REPLY Address(0 To 3) As Byte status As Long RoundTripTime As Long DataSize As Integer Reserved As Integer Data As Long Options As IP_OPTION_INFORMATION End Type Private Declare Function gethostbyname Lib "wsock32.dll" (ByVal hostname As String) As Long Private Declare Function WSAStartup Lib "wsock32.dll" (ByVal wVersionRequired&, lpWSADATA As WSADATA) As Long Private Declare Function WSACleanup Lib "wsock32.dll" () As Long Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long) Private Declare Function IcmpCreateFile Lib "icmp.dll" () As Long Private Declare Function IcmpCloseHandle Lib "icmp.dll" (ByVal HANDLE As Long) As Boolean Private Declare Function IcmpSendEcho Lib "ICMP" (ByVal IcmpHandle As Long, ByVal DestAddress As Long, ByVal RequestData As String, _ ByVal RequestSize As Integer, RequestOptns As IP_OPTION_INFORMATION, ReplyBuffer As IP_ECHO_REPLY, ByVal ReplySize As Long, _ ByVal TimeOut As Long) As Boolean Public Function Ping(ByVal hostname As String) As Long Dim hFile As Long, lpWSADATA As WSADATA Dim hHostent As Hostent, AddrList As Long Dim Address As Long, rIP As String Dim OptInfo As IP_OPTION_INFORMATION Dim EchoReply As IP_ECHO_REPLY Call WSAStartup(&H101, lpWSADATA) If gethostbyname(hostname + String(64 - Len(hostname), 0)) <> SOCKET_ERROR Then CopyMemory hHostent.h_name, ByVal gethostbyname(hostname + String(64 - Len(hostname), 0)), Len(hHostent) CopyMemory AddrList, ByVal hHostent.h_addr_list, 4 CopyMemory Address, ByVal AddrList, 4 End If hFile = IcmpCreateFile() If hFile = 0 Then MsgBox "Unable to Create File Handle" Exit Function End If OptInfo.Ttl = 255 If IcmpSendEcho(hFile, Address, String(32, "A"), 32, OptInfo, EchoReply, Len(EchoReply) + 8, 2000) Then rIP = CStr(EchoReply.Address(0)) + "." + CStr(EchoReply.Address(1)) + "." + _ CStr(EchoReply.Address(2)) + "." + CStr(EchoReply.Address(3)) Else End If If EchoReply.status = 0 Then Ping = EchoReply.RoundTripTime Else Ping = -1 End If Call IcmpCloseHandle(hFile) Call WSACleanup End Function
I also tested the ping against different websites and I was returned pings with as expected latency values.
I have now come in to work and my code, even though connected to the network is returning 0 for the ping of the smtp.
This is causing my email sending script to believe that there is not a connection to the smtp server:
Code:
SMTPName = ELookup("[SettingValue]", "tblDBSettings", "[SettingName]='SMTPServer'") 'Check to see if we are connected to the smtp server if not then exit this function If Ping(" & SMTPName & ") = -1 Then MsgBox "You are not connected to the network. For further help contact database admin.", vbInformation, "Error in Email" Exit Function Else SMTPUser = ELookup("[SettingValue]", "tblDBSettings", "[SettingName]='SMTPUser'") SMTPPass = ELookup("[SettingValue]", "tblDBSettings", "[SettingName]='SMTPPass'") End If
I have tried pinging various websites using the code in the 1st part of this post and they all return -1, whereas they did not when I was at home. Any ideas why this might be happening?
=== Edit
After a bit more testing it seems the value returning 0 jsut means there is 0 latency due to the nserver being within my network:
Code:
?GetHostNameFromIP("137.xxx.xx.101") NUTS1ETA ?GetHostNameFromIP("137.xxx.xx.83") w7bc-p1595.xxx.xxxxx.net ?ping("w7bc-p1595.xxx.xxxxx.net") output: 0 ?ping("NUTS1ETA") output: 0 ?ping("http://www.yahoo.com") output: -1 ?ping("yahoo.com") output: -1
w7bc-p1595 is my computer. Both pings and lookups via IP cause a value of 0 to be returned. Weirdly though I do not know why I can not ping normal websites such as www.yahoo.com etc.. they all return a -1 yet when I am at home they return a value such as 40, 74, 88 etc etc which I assumed were milliseconds for the ping reply.
Comment