Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Build Your Own ASP.NET 2.0 Web Site Using CSharp And VB (2006) [eng]-1.pdf
Скачиваний:
142
Добавлен:
16.08.2013
Размер:
15.69 Mб
Скачать

Sending Email with ASP.NET

Now, click the Browse… button, select a file from your system, and click Upload! again. Some basic file information, like that shown in Figure 14.12, is displayed.

Figure 14.12. Uploading a file

After you’ve uploaded a file successfully, check the Learning folder to ensure the new file has indeed been saved there. As you can see, handling file uploads in ASP.NET 2.0 is very easy.

Sending Email with ASP.NET

Suppose for a moment that you’re the webmaster for an online store, and you want to send an email confirmation to each customer who places an order. Rather than manually firing off an email to every customer about every order, you could automate the process using ASP.NET.

Sending Email with ASP.NET 2.0

If you’ve learned to send email with ASP.NET 1.x, pay attention: things have changed in ASP.NET 2.0! The classes from the System.Web.Mail namespace have been declared obsolete; newer versions now exist in the System.Net.Mail namespace.

The namespace that groups the new .NET 2.0 mail-related classes is System.Net.Mail. The most useful classes in this namespace are:

SmtpClient

contains functionality for sending email

MailMessage

represents an email message

593

Chapter 14: Working with Files and Email

Attachment

represents an email attachment

AttachmentCollection

represents a collection of Attachment objects

MailAddress

represents an email address

MailAddressCollection

represents a collection of email addresses

A core set of features is common to most email programs. For instance, they all enable you to send an email to someone by typing the recipient’s email address in a To field. You are also able to specify who the email is from, the subject of the message, and the body content of the email. All these properties—and more—are available through the MailMessage class. Here’s a partial list of the properties that the MailMessage class supports:

From

specifies the address from which the email message is to be sent

To

specifies the address to which the email message is to be sent

CC

specifies the carbon copy field of the email message

Bcc

specifies the blind carbon copy field of the email message

Attachments

a collection of items or files attached to the email message

Subject

specifies the subject of the email message

Body

defines the body of the email message

IsBodyHtml

True if the message is in HTML format, or False otherwise (defaults to

False)

594

Configuring the SMTP Server

Other properties of MailMessage that you may need to use include

AlternateViews, BodyEncoding, DeliveryNotificationOptions, Headers, Priority, ReplyTo, Sender, and SubjectEncoding.

The From field has the MailAddress type, representing an email address. The To, CC, and Bcc properties are of the MailAddressCollection type, representing a collection of MailAddress objects.

As you can see, there are lots of classes and properties that let you define email messages. However, to be able to send these messages, you need access to a SMTP server.

The standard email protocol of the Internet is Simple Mail Transfer Protocol (SMTP). When you use ASP.NET to send an email, the message is relayed through one or more SMTP servers on the way to its final destination. Most ISPs provide an SMTP server for customers’ use; alternatively, if you’re using IIS, you can make use of Windows’ built-in SMTP Server.

Configuring the SMTP Server

IIS Only

If you’re not using IIS, or you have access to an external SMTP server, you can choose to skip this section.

In order to send an email, you need to connect to an SMTP server. On Windows, an SMTP server is included with IIS, and you’ll need to configure this server if you want to send email from ASP.NET. First, you need to make sure that the

SMTP service is running. Open the IIS applet to check its status. If the server is stopped, you can right-click its icon and select Start to start it, as shown in Figure 14.13.

If the SMTP server entry doesn’t appear, you’ll need to install it using the process outlined below. Note that the SMTP server is a component of IIS, so if you haven’t installed IIS (or if your Windows version doesn’t support IIS, as is the case with Windows XP Home) you won’t be able to install the SMTP server.

1.Launch the Add or Remove Programs applet in Control Panel.

2.Click Add/Remove Windows Components.

3.Select the Internet Information Services item, and click Details….

595

Chapter 14: Working with Files and Email

Figure 14.13. Starting the SMTP Virtual Server

4.Select SMTP Service.

5.Click OK, then Next.

6.Run the Internet Information Services utility again. This time, you should see the SMTP service running.

Now that you have a running SMTP server, you need to configure it. In order for email to be relayed correctly, perform the following steps:

1.Open the Internet Information Services utility.

2.Right-click Default SMTP Virtual Server and choose Properties.

3.Select the Access tab.

4.Click Relay….

5.Click the Add… button.

6.Add the localhost IP address: 127.0.0.1.

7.Uncheck the Allow all computers… checkbox. The dialog should look similar to Figure 14.14.

596