Smarter Email/Smtp setup with DotNet Configuration Sections (1.1 and 2.0)

 
How many times have you written something like this:
 <appSettings>
<add key="smtpserver" value="smtp.myisp.com"/>
 </appSettings>
 
And then, when you need to send email, you read the value from the config file, and write code to send email.
 
Perhaps you deploy to a web hosting company, and realize they have a different smtp authentication routine.
Then you go back to your code, and make adjustments, so you can send from your local setup, and then your hosting company as well.
 
So you put together something for the 1.1 Framework.  Then 2.0 comes out, and you repeat using the new email object in 2.0.
 
1.1 Framework uses the

System.Web.Mail.MailMessage

object.

Then along comes 2.0, and thank goodness they clean up sending emails.

2.0 uses the

System.Net.Mail.MailMessage

object.

 

After looking at the 2.0 library, I realized it would be much cleaner to seperate the code which instantiates these 2 objects, and the smtp server settings.

I basically have created a custom configuration section handler, to encapsulate most/all of the different settings one might need to send Smtp Emails.

The best part of this custom handler, is that I can reuse it for both 1.1 and 2.0 email sending.

I’ve also included a 1.1 sample project, and a 2.0 sample project.

Let’s take a quick look at the App.config (or Web.config) file.

 

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<configSections>

<section name="EmailSettingsSectionName" type="GranadaCoder.Email.Settings.EmailSmtpSettingsHandler,GranadaCoder.Email.Settings" />

</configSections>

<EmailSettingsSectionName defaultEmailFrom="donotreply@donotreply.com" portNumber="25">

<!–You need to add a reference to GranadaCoder.Email.Settings.EmailSmtpSettingsHandler.dll since this dll lives outside this (Presentation) assembly–>

<!–Comments can go here–>

<!–SSL example. Google(gmail)Mail is a good (free) example of this. Naturally, you need to provide a legitimate username and password–>

<!–Note, with the 2.0 Framework, my tests show that gmail likes port 587–>

<smtpServer enabled="true" smtpServerName="smtp.gmail.com" defaultEmailFrom="donotreply@gmail.com" portNumber="465" authenicationMode="SSL" smtpUserName="mygmailaddress@gmail.com" smtpUserPassword="mygmailpassword" executeOrder="3"/>

<!–Basic authentication. Passing in a username (and sometimes a password) are used here.–>

<smtpServer enabled="true" smtpServerName="smtp-server.nc.rr.com" defaultEmailFrom="donotreply@rr.com" portNumber="25" authenicationMode="basic" smtpUserName="myemail@rr.com" executeOrder="2"/>

<!–None authentication. Nothing but the smtp-server name is provided–>

<smtpServer enabled="false" smtpServerName="smtp.noauthenticationneeded.com" authenicationMode="none" executeOrder="1"/>

 

</EmailSettingsSectionName>

</configuration>

 

I have all 3 scenarios here.

(No) authentication.

Basic authenticaton.

SSL authentication.

 

You’ll also notice I have a Collection of SmtpServers.  Why?  One way I use this class is to have back-up email senders.  In case the first smtp server is down, I use the 2nd (or the 3rd or 4th) email to send the email.  If the first one is down, I also send an admin email saying "The primary smtp server was down".  This is why I included the ‘executeOrder’ attribute.  But that is outside the scope of this blog.

 

The config section makes quick work of deploying my solution to a hosting or production server.

 

By seperating the setup information from the 1.1 or 2.0 code to send emails, your 1.1 or 2.0 code becomes cleaner.

 

I also like the Collection of SmtpServers idea.  Using <app key values> means you get one smtp server to use.

Even with 2.0:

 

  <system.net>

    <mailSettings>
      <smtp from="
your@email.com">
        <network host="smtp-server.rr.com" password="" userName="myaccount
@rr.com" />
      </smtp>
    </mailSettings>
   
  </system.net>

 

It looks like a 1 smtp server setup.

 

You can download the code HERE. (Right-Click and "Save As" works best)

 

The code is in 1.1, except for the code in the TestApp_2_0 directory.  There are 2 solution files, one in 1.1 (VS 2003) and one in 2.0 (VS 2005).

 

For 1.1, you will want to open up the:

\Settings\TestApp_1_1\GranadaCoder.Email.Settings.TestApp.sln

solution. (In VS 2003 of course.)

 

For 2.0, you will want to open up the:

\Settings\TestApp_2_0\TestApp_2_0.sln

solution file. (In VS 2005 of course.)

(Note, the 2.0 solution does not refer to the GranadaCoder.Email.Settings.dll file "By Project", so you may need to re-add the reference (as in… browse/file…add reference) to get it to work.

 

This solution offers these advantages:

Encapsulation and seperation of the Smtp Server Settings from the code to send an Email through DotNet.

(Optionally) a Collection of SmtpServer, to allow for a backup plan, if the primary smtp server is unavailable.

Easy setup through a config file.

 

The solution will not work out of the box.  You need to put in your smtp server settings.  You also need to change the To address in the Console.Application code.  And don’t forget the <smtpServer enabled="false"> setting, which needs to be "true" if you want that smtp-server to be considered.

 

Even if you don’t need the smtp server, you can use the project to learn how to write a customized Configuration Handler.

Also included are:

IComparer, to handle different attributes/properties to sort by.

CollectionBase object, and a Sort method using your custom IComparer

Advertisements
This entry was posted in Software Development. Bookmark the permalink.

One Response to Smarter Email/Smtp setup with DotNet Configuration Sections (1.1 and 2.0)

  1. Nitin says:

    i downloaded the sample application but didn’t find the dll to set the reference. pls help

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s