To make it easy for your .NET applications to send SMTP mail, the .NET Framework includes the System.Web.Mail namespace. The classes in this namespace are not as feature rich as CDO 1.21 or CDO for Exchange. The main difference between System.Web.Mail and CDO is that if you need to send just e-mail, use System.Web.Mail in your .NET applications. If you need to do complex collaborative applications that use calendaring or more advanced features of Exchange, use CDO in your .NET applications.
When working with this namespace, make sure to add a reference to the System.Web namespace. This namespace leverages the Collaboration Data Objects for Windows (CDOSYS) messaging component. Using this namespace, you can quickly construct plain text or HTML mail messages with or without attachments. The namespace includes three main classes: MailMessage , MailAttachment , and SmtpMail .
This class provides the methods and properties to construct an e-mail message. As you would suspect, this class supports the standard properties that an e-mail message needs such as From , To , Cc , Bcc , Priority , Attachments , Fields , and Body formatting properties. The sample application below will show you how to use the MailMessage class.
This class provides the methods and properties for constructing e-mail attachments. By default, attachments are UUEncoded. This class is straightforward and the most common property you will use in the class is the Filename property. This property should be set to the path to the attachment you want to create in the instance of your class. The constructor for this class can also take a string argument. If you pass the path to the file you want to attach when you construct the object, you will achieve the same effect as setting the Filename property. You can have multiple instances of this class in order to add multiple attachments to a mail message. Once you have created your instances, you will want to use the Add method on the Attachments collection on the MailMessage class to add those attachments.
This class provides the properties and methods for sending your mail messages via SMTP. This class allows you to set the SMTP server you want to use to send the message using the SMTPServer property. The class also has a Send method that you can use to send the message.
Since the System.Web.Mail namespace and classes in the namespace are very straightforward, a sample application is the best way to demonstrate how to use the classes and their properties and methods. Figure 20-10 shows the sample application. The application is a simple e-mail program written in Visual Basic that sends a message either as HTML or as plain text with an optional single attachment. The sample also shows how to use the FileDialog control to allow you to select the attachment you want to add.
Private Sub cmdSelectAttachment_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSelectAttachment.Click OpenFileDialog1.CheckFileExists = True OpenFileDialog1.CheckPathExists = True OpenFileDialog1.Multiselect = False Dim dialogresult As DialogResult = OpenFileDialog1.ShowDialog If dialogresult = dialogresult.OK Then 'Get the file path txtAttachment.Text = OpenFileDialog1.FileName End If End Sub Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click On Error Resume Next 'Check the values If txtFrom.Text = "" Then MsgBox("You must fill in the from field!") Exit Sub ElseIf txtTo.Text = "" Then MsgBox("You must fill in the to field!") Exit Sub End If Dim oMailMessage As New System.Web.Mail.MailMessage() 'See if we need to send as HTML If checkSendAsHTML.Checked = True Then oMailMessage.BodyFormat = Web.Mail.MailFormat.Html End If oMailMessage.To = txtTo.Text() oMailMessage.From = txtFrom.Text() oMailMessage.Subject = txtSubject.Text() oMailMessage.Body = txtBody.Text() 'See if there is an attachment If txtAttachment.Text <> "" Then 'Add the attachment Dim oMailAttachment As New _ System.Web.Mail.MailAttachment(txtAttachment.Text()) oMailMessage.Attachments.Add(oMailAttachment) End If Dim oSMTPMail As System.Web.Mail.SmtpMail oSMTPMail.Send(oMailMessage) End Sub