public static string[] GetMXRecords(string domain) { IntPtr ptr1 = IntPtr.Zero; IntPtr ptr2 = IntPtr.Zero; MXRecord recMx; if (Environment.OSVersion.Platform != PlatformID.Win32NT) { throw new NotSupportedException(); } ArrayList list1 = new ArrayList(); int num1 = DnsLookUp.DnsQuery(ref domain, QueryTypes.DNS_TYPE_MX, QueryOptions.DNS_QUERY_BYPASS_CACHE, 0, ref ptr1, 0); if (num1 != 0) { throw new Win32Exception(num1); } for (ptr2 = ptr1; !ptr2.Equals(IntPtr.Zero); ptr2 = recMx.pNext) { recMx = (MXRecord)Marshal.PtrToStructure(ptr2, typeof(MXRecord)); if (recMx.wType == 15) { string text1 = Marshal.PtrToStringAuto(recMx.pNameExchange); list1.Add(text1); } } DnsLookUp.DnsRecordListFree(ptr1, 0); return((string[])list1.ToArray(typeof(string))); }
public static void sendMail(string toAddress, string subject, string message) { MailAddress From = new MailAddress("Dungeon Teller <dungeon-teller@localhost>"); MailAddress To = new MailAddress(toAddress); MailMessage msg = new MailMessage(From, To); msg.Subject = subject; msg.Body = message; try { string address = To.ToString(); string domain = address.Substring(address.IndexOf('@') + 1); string mxRecord = DnsLookUp.GetMXRecords(domain)[0]; SmtpClient client = new SmtpClient(mxRecord); client.SendAsync(msg, "message1"); } catch (Exception ex) { MessageBox.Show(String.Format("Failed to send email with the following error:\n'{0}'", ex.Message), "Mail-Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }