Inicio  |  Código  |  Utilidades  |  Enlaces  |  Buscar  |   
Error processing SSI file
Info

Valid XHTML 1.0!

Valid CSS!

Obtener los servidores Mail Exchanger de un dominio

Esta clase permite obtener los servidores mail exchanger de un dominio.
 
Copiar al portapapelesClase Edanmo.Net.Dns
Option Explicit On 
Option Strict On

Imports System
Imports System.ComponentModel
Imports System.Runtime.InteropServices
Imports System.Runtime.InteropServices.Marshal

Namespace Edanmo.Net

    Public NotInheritable Class Dns

        Private Declare Unicode Function DnsQuery Lib "dnsapi" Alias "DnsQuery_W" ( _
            ByVal pszName As String, ByVal wType As DnsQueryTypes, _
            ByVal options As DnsQueryOptions, ByVal aipServers As Integer, _
            ByRef ppQueryResults As IntPtr, ByVal pReserved As Integer) As Integer

        Private Declare Auto Sub DnsRecordListFree Lib "dnsapi" ( _
            ByVal pRecordList As IntPtr, ByVal FreeType As Integer)

        Private Enum DnsQueryOptions As Integer
            DNS_QUERY_STANDARD = &H0
            DNS_QUERY_ACCEPT_TRUNCATED_RESPONSE = &H1
            DNS_QUERY_USE_TCP_ONLY = &H2
            DNS_QUERY_NO_RECURSION = &H4
            DNS_QUERY_BYPASS_CACHE = &H8

            DNS_QUERY_NO_WIRE_QUERY = &H10
            DNS_QUERY_NO_LOCAL_NAME = &H20
            DNS_QUERY_NO_HOSTS_FILE = &H40
            DNS_QUERY_NO_NETBT = &H80

            DNS_QUERY_WIRE_ONLY = &H100
            DNS_QUERY_RETURN_MESSAGE = &H200

            DNS_QUERY_TREAT_AS_FQDN = &H1000
            DNS_QUERY_DONT_RESET_TTL_VALUES = &H100000
            DNS_QUERY_RESERVED = &HFF000000
        End Enum

        Private Enum DnsQueryTypes As Integer
            DNS_TYPE_MX = &HF
        End Enum

        Private Structure DnsRecordMX
            Public pNext As IntPtr
            Public pName As String
            Public wType As Short
            Public wDataLength As Short
            Public flags As Integer
            Public dwTtl As Integer
            Public dwReserved As Integer
            Public pNameExchange As IntPtr
            Public wPreference As Short
            Public Pad As Short
        End Structure

        Public Shared Function GetMailExchangeServers(ByVal domain As String) As String()

            ' Check if the machine is running NT based OS
            If Environment.OSVersion.Platform <> PlatformID.Win32NT Then
                Throw New NotSupportedException
            End If

            Dim ptr As IntPtr
            Dim result As Integer
            Dim servers As New ArrayList

            ' Query the DNS server for the MX records
            result = DnsQuery( _
                domain, _
                DnsQueryTypes.DNS_TYPE_MX, _
                DnsQueryOptions.DNS_QUERY_BYPASS_CACHE, _
                0, ptr, 0)

            If result = 0 Then

                Dim ptrNext As IntPtr = ptr

                ' Enumerate all returned records
                Do Until ptrNext.Equals(IntPtr.Zero)

                    ' Get the record from the pointer
                    Dim record As DnsRecordMX
                    record = DirectCast(PtrToStructure(ptrNext, GetType(DnsRecordMX)), DnsRecordMX)

                    ' Check if the record is a MX record
                    If record.wType = DnsQueryTypes.DNS_TYPE_MX Then

                        ' Get the server name from the pointer
                        Dim server As String = PtrToStringAuto(record.pNameExchange)

                        ' Add the server to the list
                        servers.Add(server)

                    End If

                    ' Get the pointer to the next record
                    ptrNext = record.pNext

                Loop

                ' Release the record list
                DnsRecordListFree(ptr, 0)

            Else

                ' Throw the exception
                Throw New Win32Exception(result)

            End If

            ' Return the server array
            Return DirectCast(servers.ToArray(GetType(String)), String())

        End Function

    End Class

End Namespace