Get SPF records from a Domain

nimi picture nimi · Aug 7, 2012 · Viewed 9k times · Source

What are the ways to Check SPF records on a domain?

There is a website where i can do it manually using - http://www.mxtoolbox.com/SuperTool.aspx

How can i do it via ASP.NET and C#? Basically i want to verify/check SPF records on a domain if its supporting out own mail web server.

Answer

Cristian Lupascu picture Cristian Lupascu · Aug 9, 2012

I have the same problem, and managed to find two three solutions:

The nslookup solution

You can get the SPF by typing the following command in the command line:

nslookup -type=TXT <hostname>

You can automate this in C# using System.Diagonstics.Process, as described in this blog post.

The DNS.NET Resolver project

I found this CodeProject article about DNS resolution. It comes with demo project. I ran the project and got the following result for stackexchange.com:

DNS Dig screeenshot

Note: Make sure that the QType field is set to TXT before you press the Send button

The section highlighted in yellow represents the SPF record. I haven't yet dug into the code to see how it's done, but this seems like a good alternative to the nslookup solution above.

[Update] The ARSoft.Tools.Net project

If all you need to do is check whether a domain supports a mail server, you can use the ARSoft.Tools.Net library (also available as a NuGet Package).

After installing the package, I managed to perform the SPF check with this code:

var spfValidator = new ARSoft.Tools.Net.Spf.SpfValidator();

var mailIpAddress = IPAddress.Parse("X.X.X.X");
var domain = "example.com";
var senderAddress = "[email protected]";

ARSoft.Tools.Net.Spf.SpfQualifier result = 
    spfValidator.CheckHost(mailIpAddress, domain, senderAddress);