Guest User

Untitled

a guest
Sep 20th, 2013
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. [DllImport("dnsapi", EntryPoint = "DnsQuery_W", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
  2. private static extern int DnsQuery([MarshalAs(UnmanagedType.VBByRefStr)]ref string pszName, QueryTypes wType, QueryOptions options, int aipServers, ref IntPtr ppQueryResults, int pReserved);
  3.  
  4. [DllImport("dnsapi", CharSet = CharSet.Auto, SetLastError = true)]
  5. private static extern void DnsRecordListFree(IntPtr pRecordList, int FreeType);
  6.  
  7. public List<string> GetMXRecords(string domain)
  8. {
  9. List<string> records = new List<string>();
  10. IntPtr ptr1 = IntPtr.Zero;
  11. IntPtr ptr2 = IntPtr.Zero;
  12. MXRecord recMx;
  13.  
  14. try
  15. {
  16.  
  17. int result = DnsQuery(ref domain, QueryTypes.DNS_TYPE_MX, QueryOptions.DNS_QUERY_BYPASS_CACHE, 0, ref ptr1, 0);
  18. if (result != 0)
  19. {
  20. if (result == 9003)
  21. {
  22. //No Record Exists
  23. }
  24. else
  25. {
  26. //Some other error
  27. }
  28. }
  29.  
  30. for (ptr2 = ptr1; !ptr2.Equals(IntPtr.Zero); ptr2 = recMx.pNext)
  31. {
  32. recMx = (MXRecord)Marshal.PtrToStructure(ptr2, typeof(MXRecord));
  33. if (recMx.wType == 15)
  34. {
  35. records.Add(Marshal.PtrToStringAuto(recMx.pNameExchange));
  36. }
  37. }
  38. }
  39. finally
  40. {
  41. DnsRecordListFree(ptr1, 0);
  42. }
  43.  
  44. return records;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment