Then along comes 2.0, and thank goodness they clean up sending emails.
2.0 uses the
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" ?>
<section name="EmailSettingsSectionName" type="GranadaCoder.Email.Settings.EmailSmtpSettingsHandler,GranadaCoder.Email.Settings" />
<EmailSettingsSectionName defaultEmailFrom="firstname.lastname@example.org" 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="email@example.com" portNumber="465" authenicationMode="SSL" smtpUserName="firstname.lastname@example.org" smtpUserPassword="mygmailpassword" executeOrder="3"/>
<!–Basic authentication. Passing in a username (and sometimes a password) are used here.–>
<!–None authentication. Nothing but the smtp-server name is provided–>
<smtpServer enabled="false" smtpServerName="smtp.noauthenticationneeded.com" authenicationMode="none" executeOrder="1"/>
I have all 3 scenarios here.
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:
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:
solution. (In VS 2003 of course.)
For 2.0, you will want to open up the:
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