DnSmtp is a managed implementation of an SMTP client written for .NET in C#. It was originally conceived back in 2001 due to .NET 1.1 limitations which required Outlook to be installed for .NET to deliver emails. With .NET 2.0+, Microsoft
has greatly improved their email support so a library like this isn't quite as necessary anymore. The code is still rough around the edges - i was fairly new to C# and .NET when I wrote DnSmtp even though it's been refactored a couple times, there's
still room for improvement.
The DnSmtp library was the reason I created
- since .NET wouldn't do DNS MX record lookups, I needed to write a DNS library. For Background on DnDns (my blog) -
NOTE: DON'T FORGET TO DOWNLOAD
DnDns Release 1 - DnSmtp requires it!
The Mail object can be constructed to deliver mail in a few different ways.
- DeliveryMethod.Direct - the library will do an MX lookup on the domain name of the destination email addresses to resolve their remote email server and then connect directly to that SMTP server to deliver the email directly.
- NOTE: This delivery method requires that the firewall allow output TCP on port 25 for the system sending these emails.
- DeliveryMethod.Relay - this the library will connect to your local SMTP relay server and drop the message there.
In the Mail object constructor - if you provide no constructor parameters, it will attempt to detect your DNS Server by extracting the first DNS serer from your Active NIC's registered DNS server and attempt to deliver all messages using the Direct DNS
method. Otherwise, you can configure the Mail object by providing the IP address of the local SMTP relay server for relay delivery, or the IP address the local DNS server
for direct delivery.
Here's an example of how to use DnSmtp:
static void Main(string args)
// Create the Message
MailMessage message = new MailMessage();
message.ToAddresses.Add(new EmailAddress("firstname.lastname@example.org", "Mr. John Q. Public"));
message.CcAddresses.Add(new EmailAddress("email@example.com", "Bob Masters"));
message.FromAddress = new EmailAddress("firstname.lastname@example.org", "Susie Smith");
message.Subject = "Testing...";
// Several Ways to add a message
// Add a specific Mime type
message.Items.Add(new MessageContent(MimeType.MessageRfc822, "This is a test message."));
// Use a Helper Methods to just add Text or HTML directly
Mail m = new Mail();
catch (RecipientFailedException rfe)
// Loop over all the failed recipients in the To, CC, Bcc lists.
foreach (FailedRecipient recipient in rfe.FailedRecipients)
Console.WriteLine("Recipient failed " + recipient.FailedAddress);
static void PrintExceptions(Exception e)
if (e != null)