To prevent confusion, in ASP.NET 2.0 the System.Web.Mail
namespace has been marked deprecated. All of the classes are still accessible
using IntelliSense and will still function properly. However, they too are
marked obsolete. Instead, a new namespace found at System.Net.Mail should be
used. This new namespace contains classes to manage your mail client and
messages. The SmtpClient class is used to establish a host, port, and network
credentials for your SMTP server. The MailMessage class is similar to the
MailMessage class found in the old System.Web.Mail namespace. This class
allows a full email message to be built. There is also an Attachment class
that allows an attachment to be generated so it can later be added to the
MailMessage object. In addition to these three most commonly used classes, you
will find that System.Net.Mail contains an AlternateView and LinkedResource
class.
Unfortunately, ASP.NET 2.0 does not provide a clean and easy
way to access the system.net section group from the web.config file within
code. The SmtpClient object contains a UseDefaultCredentials boolean property,
but that specifies whether the SmtpClient object is to use the credentials of
the user currently running the application. Before we begin accessing the
attributes in the web.config file, we will need to determine what credentials
will be required to connect to the SMTP server and ensure that the values we
will use will work. In many cases, the credentials that would be used to
connect to a Microsoft Exchange server would be different than those to connect
to many ISP email systems. In the example to follow we will use a username,
password, and hostname.
The first task on our plate is to read the mailSettings
section group from the web.config file. To accomplish this task, we will need
to create a new object of type System.Configuration.Configuration. This object
will store our web.config file. We will assign this new object to the
applications web.config by using the OpenWebConfiguration method of the
WebConfigurationManager class. The OpenWebConfiguration method will look for a
relative path. We will pass in the HttpContext.Current.Request.ApplicationPath
since we will want to load the web.config file for the current application.
The second task is to load the specific section group from
the object that contains our web.config file into a new object. A new object
of type System.Net.Configuration.MailSettingsSectionGroup will be created. This
object will be assigned to the mailSettings section in the web.config file. We
will use the GetSectionGroup method of our configuration object to obtain this
section. ASP.NET does not know that the mailSettings section is found under
system.net so we must specify the full section group as
system.net/mailSettings. An example of this can be seen in listing 2.
Listing 2
Dim config As System.Configuration.Configuration = _
WebConfigurationManager.OpenWebConfiguration( _
HttpContext.Current.Request.ApplicationPath)
Dim settings As System.Net.Configuration.MailSettingsSectionGroup = _
CType(config.GetSectionGroup("system.net/mailSettings"), _
System.Net.Configuration.MailSettingsSectionGroup)