.NET 2.0 made some changes in how you send email with AVR. While these changes require using a new namespace (System.Net.Mail) and make sending email a little different than it was in .NET 1.1, these changes provide a substantial benefit: the .NET 2.0 email component no longer relies on the old Collaborative Data Objects (CDO) COM object to send the email. In .NET 2.0, Microsoft has rewritten the underlying email component infrastructure to be 100% managed code. There is a very good site,
www.systemnetmail.com, that provides lots of very good detail about the new System.Net.Mail namespace.
The new ASP.NET 2.0 email classes live in the new System.Net.Mail namespace. While it's probably prudent to rewrite any existing email-related code to use the new classes, the good news is that the .NET 1.x namespace System.Web.Mail and the old classes are still in place. Old 1.x code will compile and work under .NET 2.0, but if you take the time to convert the code (and the changes are relatively minor) the new email engine is faster and more scalable--owing to it abandoning the CDO objects.
It's important to note that email can be sent, using the techniques shown in this article, from either a Windows or Web application. See the end of this article for a discussion about a potential configuration issue you might run into when sending email from a Windows application.
To see how fundamentally similar the two email methods are, compare the AVR 7.2 and .NET 1.1 way to send email:
Using System.Web.Mail
SmtpMail.SmtpServer = "mail.myserver.com"
SmtpMail.Send( "rp@asna.com", "rp@asna.com", "This is the subject.", "This is the body." )with the AVR 8.0 and .NET 2.0 way:
Using System.Net.Mail
DclFld MailObj Type( SmtpClient ) New( "mail.myserver.com" )
MailObj.Send( "rp@asna.com", "rp@asna.com", "This is the subject.", "This is the body." ) As you can see, at its most fundamental use, sending email hasn't changed very much. It's interesting to note that the .NET 1.1 example, the SmtpMail object's properties and methods are static; with .NET 2.0 the SmtpClient needs to be instanced. Although the simple way is really simply, it's also not the way you'll usually send email--the simple way just doesn't offer enough control over the mail being sent.
Let's take a look at the way you'll usually send email with AVR 8.0 and .NET 2.0.
Using System.Net.Mail
BegSr SendMail Access( *Public )
DclFld MailObj Type( SmtpClient )
DclFld MyMessage Type( MailMessage ) New()
MailObj = *New SmtpClient( "mail.asna.com" )
MyMessage.IsBodyHtml = *True
MyMessage.To.Add( "rp@asna.com" )
MyMessage.From = *New MailAddress( "rp@asna.com" )
MyMessage.Subject = "This is the subject"
MyMessage.Body = "This is the body."
Try
MailObj.Send( MyMessage )
Catch e1 System.Exception
Throw e1
EndTry
EndSrIn this case, we're explicitly instancing a new Mail Message object. Having this object explicitly instanced lets you do things like add multiple "to" addressees, set the email to be either regular text or HTML, and add attachments to the email.
There are are some interesting differences between this code and the code you would have used with AVR 7.2 and .NET 1.1. They include:
New SmtpClient object
The System.Net.Mail.SmtpClient object must be instanced before you attempt to send an email. It's Host property must be set to either the SMTP mail server's name or IP address. There is a constructor overload that lets you pass the name of the SMTP server to the constructor (as shown below). This is typically the only property you need to set for the SmtpClient object.
DclFld MailObj Type( SmtpClient )
DclFld MyMessage Type( MailMessage ) New()
MailObj = *New SmtpClient( "mail.asna.com" )
New MailMessage object
.NET 1.1 also had a MailMesssage object, but this new one lives in the System.Net.Mail namespace. It's use is the same as the 1.1 MailMessage object. After instancing this object, you have something you can add attachments to (more on this in a moment) and set the format (either HTML or plain text). The following shows how the MailMessage is instanced and how its format is set to HTML.
DclFld MyMessage Type( MailMessage ) New()
MyMessage.IsBodyHtml = *True
New MailAddress object
The MailMessage's To property is a collection. Rather than setting it directly with a string assignment, you use To's Add method to add a recipient.
MyMessage.To.Add( "rp@asna.com" )
In the case of multiple recipients, you could either do this:
MyMessage.To.Add( "rp@asna.com" )
MyMessage.To.Add( "mm@asna.com" )
or this
MyMessage.To.Add( "rp@asna.com;mike.marlowe@asna.com" )
.NET 2.0 adds a new way to specify recipients (and, as you'll see in a moment, you also must use this way to specify the From address. This new way is using System.Net.Mail's MailAddress object. This little object helps add a friendly name to the email--something that was not possible with .NET 1.1. To use it you add new MailAddress objects to the To collection:
MyMessage.To.Add( *New MailAddress( "rp@asna.com", "Roger" ) )
MyMessage.To.Add( *New MailAddress( "mm@asna.com", "Mike" ) )
where the first value is the email address and the second is the friendly name to show on the email. Notice that the From address is very specifically a MailAddress type and you
must assign it an instance of a MailAddress object (as shown below):
MyMessage.From = *New MailAddress( "rp@asna.com" )
You must use this object for the "From" address even if you don't want to provide a friendly name.
To add attachments
To add attachments to the email, you use the System.Net.Mail.Attachment object. It's used like this:
MyMessage.Attachments.Add( *New Attachment( [FileName] ) )
where [filename] is the name of the file to attachment. Generally, it's better to attach files that live within the root of the Web application--documents living in other parts of the Web server file system are generally prone to violating file access rules imposed by Windows security.
Sending the email
Sending the email is as simple as calling the SmtpClient's Send() method:
MailObj.Send( MyMessage )
You can use a Try/Catch to ensure the mail was successfully sent. For example:
Try
MailObj.Send( MyMessage )
Catch MailError System.Exception
Throw MailError // Or otherwise deal with error here.
EndTry
Sending email from a Windows application
Everything desribed in this article works for both Windows and Web applications. However, when sending mail through your organization's mail server, you may bump into a circumstance where the network is configured to not allow individual PCs to relay email from the local PC on through to the mail server. In this case, you can either have your network administrator configure the mail server to allow relaying from individual PCs or you can use the following line:
MailObj.Credentials = *New System.Net.NetworkCredential( "neil", "shakey" )
to specify to the instance of the SmtpClient what network creditials should be used when connecting to the mail server.
As you can see, sending email with .NET 2.0 is easy and powerful. Even though the mail component has been rewritten for .NET 2.0, my early tests show that it's a little slow for sending lots of bulk emails. But, for anything short of really heavy-duty purposes, sending email with the .NET Framework and AVR 8.0 is quite easy and very useful.