dotnet-core_mail-server/MailServer/GetDNSEntries.cs

71 lines
2.0 KiB
C#

using DnsClient;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace SMTPServer
{
public class GetDNSEntries
{
public static async System.Threading.Tasks.Task<string[]> GetMXRecordAsync(string domain) => await GetMXRecordAsync(domain, 0);
public static async System.Threading.Tasks.Task<string[]> GetMXRecordAsync(string domain, int nr)
{
var lookup = new LookupClient();
var result = await lookup.QueryAsync(domain, QueryType.MX);
var records = result.AllRecords.ToArray();
//ToDo priority sorting
var rec = new string[records.Length];
for(int i = 0; i < records.Length; i++)
{
var str = records[i].RecordToString();
var s = str.Split(' ');
if (s.Length == 1)
{
rec[i] = s[0];
} else if (s.Length == 2)
{
rec[i] = s[1];
} else
{
rec[i] = null;
}
}
return rec;
/*
foreach (var r in result.AllRecords)
{
Console.WriteLine(r.RecordToString());
}*/
}
public static async System.Threading.Tasks.Task<string[]> GetIPAddressFromDnsArrayAsync(string[] dnsnames)
{
var ips = new string[dnsnames.Length];
for(int i = 0; i < dnsnames.Length; i++)
{
ips[i] = await GetIpFromDnsNameAsync(dnsnames[i]);
}
return ips;
}
public static async System.Threading.Tasks.Task<string> GetIpFromDnsNameAsync(string dnsname)
{
var lookup = new LookupClient();
var result = await lookup.QueryAsync(dnsname, QueryType.A);
var records = result.AllRecords.ToArray();
if (records.Length < 1) return null;
return records[0].RecordToString();
}
}
}