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 ISPs SMTP server enclosed in square brackets, as shown in Fig-
ure 14.15.
2
12.
Click OK, then OK again, and finally close the IIS tool.
Sending a Test Email
Later, well add a newsletter section to the Dorknozzle site, but first, lets write
a very simple page to test that everythings working as it should.
Create a new file named SendEmail.aspx in the Learning folder. Dont 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 ISPs SMTP server. You should see a number of lines that
read Reply from…” followed by the IP address of the server.
597
Sending a Test Email
Figure 14.15. Routing email to your ISPs 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 servers address.
598
Chapter 14: Working with Files and 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
Sending a Test 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 youre ready to update the Dorknozzle site!
600
Chapter 14: Working with Files and Email

Get Build Your Own ASP.NET 2.0 Web Site Using C# & VB, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.