[Excel VBA] Port Abfrage

  • Excel

Es gibt 3 Antworten in diesem Thema. Der letzte Beitrag () ist von Blaupunkt79.

    [Excel VBA] Port Abfrage

    Hallo Zusammen,

    habe ein Programm, welches u.a. Mails per Excel versendet. Hierbei kann der Absender entsprechend konfiguriert werden. Jetzt möchte ich, dass von Excel aus geprüft werden kann, ob der gewählte SMTP Port "offen" ist, ähnlich folgender Webseite:

    portchecker.co/

    Kann mir dabei jemand helfen?

    Gruß
    Schau dir mal Post #6 hier an: vbaexpress.com/forum/showthrea…ort-is-up-from-VBA-(Excel)

    Du brauchst das Winsock-Steuerelement (gibt es das in Excel?).
    Besucht auch mein anderes Forum:
    Das Amateurfilm-Forum
    Funktioniert auch nur mit WINAPI (winsock dll):

    Spoiler anzeigen

    Visual Basic-Quellcode

    1. 'socket stuff
    2. Private Declare Function socket Lib "wsock32.dll" (ByVal af As Integer, ByVal typesock As Integer, ByVal protocol As Integer) As Integer
    3. Private Declare Function bind Lib "wsock32.dll" (ByVal s As Integer, addr As InputSocketDescriptor, ByVal namelen As Integer) As Integer
    4. Private Declare Function connect Lib "wsock32.dll" (ByVal sock As Integer, sockstruct As InputSocketDescriptor, ByVal structlen As Integer) As Integer
    5. Private Declare Function htons Lib "wsock32.dll" (ByVal a As Integer) As Integer
    6. Private Declare Function closesocket Lib "wsock32.dll" (ByVal sock As Integer) As Integer
    7. Private Declare Function inet_addr Lib "wsock32.dll" (ByVal co As String) As Long
    8. 'wsastuff
    9. Private Declare Function WSAStartup Lib "wsock32.dll" (ByVal a As Integer, B As WSAdata_type) As Integer
    10. Private Declare Function WSACleanup Lib "wsock32.dll" () As Integer
    11. 'const
    12. Private Const SOCKET_ERROR = -1
    13. Private Const INVALID_SOCKET = -1
    14. Private Const AF_INET = 2
    15. Private Const SOCK_STREAM = 1
    16. Private Const AF_UNSPEC = 0
    17. Private Const PF_INET = 2
    18. 'types
    19. Private Type InputSocketDescriptor
    20. Family As Integer
    21. Port As Integer
    22. Address As Long
    23. Fill As String * 8
    24. End Type
    25. Private Type WSAdata_type
    26. wVersion As Integer
    27. wHighVersion As Integer
    28. szDescription As String * 257
    29. szSystemStatus As String * 129
    30. iMaxSockets As Integer
    31. iMaxUdpDg As Integer
    32. lpVendorInfo As String * 200
    33. End Type
    34. Private WSAdata As WSAdata_type
    35. Private CallSocket As InputSocketDescriptor
    36. Function CreateSocket(ByRef TCPSocket As Long, ByVal TCPPort As Long)
    37. 'Get a socket/handle from Winsock
    38. TCPSocket = socket(PF_INET, SOCK_STREAM, AF_UNSPEC)
    39. If TCPSocket = INVALID_SOCKET Then
    40. CreateSocket = False
    41. Exit Function
    42. End If
    43. 'Now bind the socket
    44. 'We pass this structure during a bind.
    45. CallSocket.Family = AF_INET
    46. CallSocket.Port = htons(TCPPort)
    47. CallSocket.Address = 0
    48. CallSocket.Fill = " "
    49. Status = bind(TCPSocket, CallSocket, Len(CallSocket))
    50. If Status = SOCKET_ERROR Then
    51. CreateSocket = False
    52. Exit Function
    53. End If
    54. CreateSocket = True
    55. End Function
    56. Function ConnectSocket(ByRef TCPSocket As Long, ByVal HostAddress As Long, ByVal TCPPort As Long)
    57. CallSocket.Family = AF_INET
    58. CallSocket.Port = htons(TCPPort) 'converts byte order from PC to Network
    59. CallSocket.Address = HostAddress
    60. CallSocket.Fill = " "
    61. 'connect to socket
    62. Status = connect(TCPSocket, CallSocket, Len(CallSocket))
    63. If Status = SOCKET_ERROR Then
    64. ConnectSocket = False
    65. Exit Function
    66. End If
    67. ConnectSocket = True
    68. End Function
    69. Function TestIpPort(IP As String, Port As Integer)
    70. Dim s As Long
    71. Dim RequestedVersion As Integer
    72. Dim Status As Integer
    73. RequestedVersion = &H101 ' Version 1.1
    74. Status = WSAStartup(RequestedVersion, WSAdata) 'start usage of winsock
    75. If Status Then
    76. MsgBox ("Failed to startup Winsock")
    77. Exit Function
    78. End If
    79. If CreateSocket(s, 0) Then
    80. If ConnectSocket(s, inet_addr(IP), Port) Then
    81. MsgBox ("Port " & Port & " on " & IP & " is open")
    82. Status = closesocket(s) 'close socket
    83. Else
    84. MsgBox ("Cant connect to " & IP & " and Port " & Port)
    85. End If
    86. End If
    87. WSACleanup 'for every startup you need one cleanup
    88. End Function
    89. 'Aufruf wäre dann
    90. TestIpPort "192.168.0.1", 25



    LG
    Das ist meine Signatur und sie wird wunderbar sein!
    Danke für die Rückmeldungen, ich habs jetzt etwas "einfacher" gelöst, indem ich die Möglichkeit einer Testmail eingebaut habe. Auch kann der User nun entscheiden, ob er via SMTP oder via Outlook die Mails versenden möchte, beide Optionen kann er nun mit der besagten Testmail prüfen. Bekommt Excel keine Verbindung o.ä. erfolgt eine entsprechende Fehlermeldung, dass die Konfiguration fehlerhaft ist.