Build Your Own ASP.NET 3.5 Website Using C# & VB (106 page)

Read Build Your Own ASP.NET 3.5 Website Using C# & VB Online

Authors: Cristian Darie,Zak Ruvalcaba,Wyatt Barnett

Tags: #C♯ (Computer program language), #Active server pages, #Programming Languages, #C#, #Web Page Design, #Computers, #Web site development, #internet programming, #General, #C? (Computer program language), #Internet, #Visual BASIC, #Microsoft Visual BASIC, #Application Development, #Microsoft .NET Framework

BOOK: Build Your Own ASP.NET 3.5 Website Using C# & VB
2.25Mb size Format: txt, pdf, ePub

618

Build Your Own ASP.NET 3.5 Web Site Using C# & VB

C#

LearningASP\CS\FileUpload.aspx
(excerpt)


Load the script, and click the
Upload!
button without selecting a file. The message

“No file uploaded!”
is displayed, as shown in Figure 14.11.

Figure 14.11. An error arising as a file has not been specified

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

Licensed to [email protected]

Working with Files and Email

619

After you’ve uploaded a file successfully, check the
LearningASP\VB
or
LearningASP\CS

folder to ensure that the new file has indeed been saved there. As you can see,

handling file uploads in ASP.NET 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 typing an email to every customer about every order, you could automate

the process using ASP.NET.

The namespace that groups the .NET 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

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 MailMessage

supports:

Licensed to [email protected]

620

Build Your Own ASP.NET 3.5 Web Site Using C# & VB

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; False otherwise (defaults to False)

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 which represents an email address. The

To, CC, and Bcc properties are of the MailAddressCollection type, and represent

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

Licensed to [email protected]

Working with Files and Email

621

server for customers’ use; alternatively, if you’re using IIS, you can make use of

Windows’ built-in SMTP Server.

Sending a Test Email

Later, we’ll add a newsletter section to the Dorknozzle site; first, let’s write a very

simple page to test that everything’s working as it should.

Create a new file named
SendEmail.aspx
in the
LearningASP\VB
or
LearningASP\CS

folder. Don’t use a code-behind file. Open it for editing and add the code highlighted

in bold here:

LearningASP\VB\SendEmail_1.aspx

<%@ Page Language="VB" %>

<%@ Import Namespace="System.Net.Mail" %>

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">




<br/><b>Sending Emails with ASP.NET<br/></b>





Text="Send Email!" OnClick="SendEmail" />








Add the following code, making sure you change the To email address to your own,

and that you set the Host property to your SMTP server’s address:

Licensed to [email protected]

622

Build Your Own ASP.NET 3.5 Web Site Using C# & VB

Visual Basic

LearningASP\VB\SendEmail_2.aspx
(excerpt)


C#

LearningASP\CS\SendEmail_2.aspx
(excerpt)


This script simply creates a new MailMessage object to hold the email contents and

a new SmtpClient object that will handle the job of creating a connecting to the

server and sending the email. We’ve also set the Credentials property of the

SmtpClient object, but this is only necessary if the SMTP server requires a username

and password to establish a connection. We wrap the code in a Try-Catch block in

order to catch any potential email errors.

Execute the script, and press the
Send Email
button
, as shown in Figure 14.13
. Figure 14.13. Sending the email

The email should arrive successfully at its destination, looking something like
Fig-

ure 14.14.

Now you’re ready to update the Dorknozzle site!

Creating the Company Newsletters Page

Let’s now extend the Dorknozzle site structure by adding a Newsletters page. This

page will be accessible only to the site administrator, and will provide tools with

which a customized newsletter can be sent to a list of recipients.

Licensed to [email protected]

624

Build Your Own ASP.NET 3.5 Web Site Using C# & VB

Figure 14.14. Viewing the email

Open the Dorknozzle project in Visual Web Developer, and add to it a new web

form named
AdminNewsletter.aspx
, making sure both the
Select master page
and
Create
code in a separate file
checkboxes are checked. When prompted, select the

Dorknozzle.master
master page.

Complete the generated code like this:

Dorknozzle\VB\01_AdminNewsletter.aspx

<%@ Page Language="VB" MasterPageFile="~/Dorknozzle.master"

AutoEventWireup="false" CodeFile="AdminNewsletter.aspx.vb"

Inherits="AdminNewsletter" title="
Dorknozzle Admin Newsletter
"

%>

Runat="Server">


ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

Create Newsletter





To:




Subject:



Licensed to [email protected]

Working with Files and Email

625


Introduction:

TextMode="MultiLine" Width="300" Height="100" />



Employee Of The Month:




Featured Event:


Other books

Major Vices by Mary Daheim
Just a Kiss Away by Jill Barnett
Tara Road by Maeve Binchy
The Texan's Christmas by Linda Warren
Morning Star by Marian Wells
The Best Thing by Margo Lanagan
Una campaña civil by Lois McMaster Bujold
Keeping the Feast by Paula Butturini
Love Over Matter by Maggie Bloom
Mind Calm by Newbigging, Sandy C.