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

Sending a Test Email

Figure 14.14. Configuring the SMTP Server

8.Click OK.

9.Select the Delivery tab.

10.Click the Advanced… button.

11.In the Smart host field of the Advanced Delivery window, type the IP address

of your ISP’s SMTP server enclosed in square brackets, as shown in Figure 14.15.2

12.Click OK, then OK again, and finally close the IIS tool.

Sending a Test Email

Later, we’ll add a newsletter section to the Dorknozzle site, but 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 Learning folder. Don’t use a code-behind file. Open it for editing and add the code highlighted in bold here:

2 To find out the IP address, open the Windows Command Prompt and type ping mail.isp.net, where mail.isp.net is the hostname of your ISP’s SMTP server. You should see a number of lines that read “Reply from…” followed by the IP address of the server.

597

Chapter 14: Working with Files and Email

Figure 14.15. Routing email to your ISP’s SMTP server

File: SendEmail.aspx

<%@ Page Language="VB" %>

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server"> </script>

<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">

<title>Sending Emails with ASP.NET</title>

</head>

<body>

<form id="form1" runat="server">

<asp:Button ID="sendEmailButton" runat="server" Text="Send Email!" OnClick="SendEmail" />

<br />

<asp:Label ID="statusLabel" runat="server" />

</form>

</body>

</html>

Add the following code, making sure you change the To email address to your own, and you set the Host property to your SMTP server’s address.

598

Sending a Test Email

Visual Basic File: SendEmail.aspx (excerpt)

<script runat="server">

Sub SendEmail(ByVal s As Object, ByVal e As EventArgs) Dim smtpClient As SmtpClient = New SmtpClient()

Dim message As MailMessage = New MailMessage() Try

' Prepare two email addresses

Dim fromAddress As New MailAddress( _ "test@cristiandarie.ro", "From Cristian Test")

Dim toAddress As New MailAddress( _ "contact@cristiandarie.ro", "To Cristian Test")

' Prepare the mail message message.From = fromAddress message.To.Add(toAddress) message.Subject = "Testing!"

message.Body = "This is the body of a sample message"

'Set server details smtpClient.Host = "localhost"

'Uncomment for SMTP servers that require authentication 'smtpClient.Credentials = _

'New System.Net.NetworkCredential("user", "password")

'Send the email

smtpClient.Send(message) ' Inform the user

statusLabel.Text = "Email sent." Catch ex As Exception

' Display error message

statusLabel.Text = "Coudn't send the message!"

End Try

 

End Sub

 

</script>

 

C#

File: SendEmail.aspx (excerpt)

<script runat="server">

protected void SendEmail(object sender, EventArgs e)

{

SmtpClient smtpClient = new SmtpClient(); MailMessage message = new MailMessage(); try

{

//Prepare two email addresses MailAddress fromAddress = new MailAddress(

"test@cristiandarie.ro", "From Cristian Test"); MailAddress toAddress = new MailAddress(

"contact@cristiandarie.ro", "From Cristian Test");

//Prepare the mail message

599

Chapter 14: Working with Files and Email

message.From = fromAddress; message.To.Add(toAddress); message.Subject = "Testing!";

message.Body = "This is the body of a sample message";

//Set server details smtpClient.Host = "localhost";

//Uncomment for SMTP servers that require authentication //smtpClient.Credentials = new System.Net.NetworkCredential(

//"user", "password");

//Send the email

smtpClient.Send(message); // Inform the user

statusLabel.Text = "Email sent.";

}

catch (Exception ex)

{

// Display error message

statusLabel.Text = "Coudn't send the message!";

}

}

</script>

Execute the script, and press the Send Email button, as shown in Figure 14.16.

Figure 14.16. Sending the email

The email should arrive successfully at its destination, looking something like Figure 14.17.

Now you’re ready to update the Dorknozzle site!

600