Using the SmtpMail Class to Add E-mail Functionality to an Application

function OpenWin(url, w, h) { if(!w) w = 400; if(!h) h = 300; window.open(url, "_new", "width=" + w + ",height=" + h + ",menubar=no,toobar=no,scrollbars=yes", true); } function Print() { window.focus(); if(window.print) { window.print(); window.setTimeout('window.close();',5000); } }
Team-Fly    

Special Edition Using Microsoft® Visual Basic® .NET
By Brian Siler, Jeff Spotts
Table of Contents
Chapter 25.  Interacting with Other Applications


Visual Basic .NET's SmtpMail class is provided to allow your applications to send e-mail messages. It utilizes the Simple Mail Transfer Protocol (SMTP), which is the Internet standard for sending e-mail messages. The SmtpMail class uses the SMTP service built into Windows 2000 (and later versions) to pass a MailMessage object to an SMTP server, which in turn forwards the message to the intended recipient.

Sending Mail Messages with the MailMessage Class

The workhorse of the SmtpMail class is the MailMessage class, which provides the methods and properties necessary to send an e-mail message. To send an e-mail message, you create an instance of the MailMessage class, set various properties, and then use the Send method of the SmtpMail class to send the message.

Table 25.1 summarizes some of the properties of the MailMessage class that you will use most often.

Table 25.1. Common MailMessage Properties

Property

Purpose

Attachments

A list of attachments to be included with the e-mail message.

Bcc

A list of e-mail addresses to be "blind" carbon-copied on the message. Multiple e-mail addresses separated by semicolons can be included.

Body

The actual text of the message.

BodyFormat

Set to one of the MailFormat enumeration members, Html or Text, to specify whether the format of the message's body is HTML or plain text.

Cc

A list of e-mail addresses to be carbon-copied on the message. Multiple e-mail addresses separated by semicolons can be included.

From

The e-mail address of the sender.

Subject

The subject line of the e-mail message.

To

The e-mail address of the main recipient of the message. Multiple e-mail addresses separated by semicolons can be included.

Creating a Sample Application

Let's demonstrate the use of the SmtpMail class by creating a simple application to send an e-mail message. You must add a reference to the SmtpMail class, which is provided via the System.Web.Mail namespace, to your project's references before using the SmtpMail class. Follow these steps:

  1. Start a new Windows Application project. Name it SendSmtpMailDemo. Set the Text property of Form1 to SmtpMail Demo.

  2. Add two TextBox controls to Form1. Name them txtTo and txtSubject, and clear their Text properties.

  3. Add a third TextBox control to Form1. Name it txtBody, clear its Text property, and set its MultiLine property to True.

  4. Add a Button control named btnGo. Set its Text property to Send Mail.

  5. Add three Label controls to identify the TextBox controls. Set their Text properties to To:, Subject:, and Body:. Arrange the controls in a manner similar to Figure 25.7.

    Figure 25.7. This sample application will allow you to send an e-mail message.

    graphics/25fig07.gif

  6. Right-click the References folder under the name of your project in the Solution Explorer window; then select Add Reference from the Context menu. In the Add Reference dialog box, make sure the .NET tab is selected. Select System.Web.dll under Component Name, click Select to move it to the Selected Components box, and then click OK. Note that a reference to System.Web now appears in your project's References folder.

    This process is similar to adding a reference to Microsoft Excel or Word through the Add Reference dialog box shown in Figure 25.1. Just click the .NET tab instead.

  7. Open the Code window for Form1. At the top of the window, even above Class Form1, enter the line Imports System.Web.Mail. This namespace contains the SmtpMail class.

  8. Enter the following code as the Click event handler for btnGo:

     Dim MyMessage As New MailMessage()  MyMessage.To = txtTo.Text  MyMessage.From = "yourname@yourdomain.com"  MyMessage.Subject = txtSubject.Text  MyMessage.Body = txtBody.Text  MyMessage.BodyFormat = MailFormat.Text  SmtpMail.SmtpServer = "your-smtp-server.yourdomain.com"  SmtpMail.Send(MyMessage)  MessageBox.Show("The message has been sent.") 

Note

Enter your real return e-mail address as MyMessage's From property in the third line of the code, and enter the real name of your SMTP server as the SmtpServer property. You may need to contact your ISP or network administrator to get this information.


When you run this application, enter information for the To e-mail address, subject, and message body. When you click the Send Message button, an e-mail message will be generated and sent using this information.

Using HTML for the Body Format

One enhancement that you may want to make is to use HTML as the message body format, assuming your intended recipient's e-mail program supports HTML messages. To do so, simply change the sixth line of the code to MyMessage.BodyFormat = MailFormat.Html, and include HTML tags in the body of the message. For example, when entering the body of the message, you might include the following:

 Don't forget that the <b>final exam</b> is_  <font size="+1"> this Friday!</font> 

When the BodyFormat property is set to MailFormat.Html, the HTML tags will cause appropriate formatting to be applied to the text in the body of the e-mail message.


    Team-Fly    
    Top
     



    Special Edition Using Visual Basic. NET
    Special Edition Using Visual Basic.NET
    ISBN: 078972572X
    EAN: 2147483647
    Year: 2001
    Pages: 198

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net