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 DnDns - since .NET wouldn't do DNS MX record lookups, I needed to write a DNS library. For Background on DnDns (my blog) - http://choosing-a-blog-url-sucks.blogspot.com/2008/02/dndns-net-dns-client-library-resolver.html

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:
using DnSmtp;
using DnSmtp.Message;

namespace SampleCode
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create the Message
            MailMessage message = new MailMessage();
            message.ToAddresses.Add(new EmailAddress("john@gmail.com", "Mr. John Q. Public"));
            message.CcAddresses.Add(new EmailAddress("bob@hotmail.com", "Bob Masters"));
            message.FromAddress = new EmailAddress("susie@gmail.com", "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."));

            // OR

            // Use a Helper Methods to just add Text or HTML directly
            message.AddHtmlMessage("<B>test</b><br/>\r\nlater,\r\n<br/>susie");

            Mail m = new Mail();

            try
            {
                m.Send(message);
            }
            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);
                    PrintExceptions(recipient.Exception);
                }
            }

            Console.ReadLine();
        }

        static void PrintExceptions(Exception e)
        {
            if (e != null)
            {
                Console.WriteLine(e.Message);

                PrintExceptions(e.InnerException);
            }
        }
    }
}

Last edited Jun 23, 2008 at 8:43 PM by jmonty, version 11