Figure 14.17. Viewing the email
Creating the Company Newsletter Page
Lets 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.
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:
File: AdminNewsletter.aspx
<%@ Page Language="VB" MasterPageFile="~/DorkNozzle.master"
AutoEventWireup="false" CodeFile="AdminNewsletter.aspx.vb"
Inherits="AdminNewsletter" Title="Dorknozzle Admin Newsletter"
%>
<asp:Content ID="Content1"
ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<h1>Create Newsletter</h1>
<asp:Label ID="resultLabel" runat="server" ForeColor="Red"/>
<br />To:<br />
<asp:TextBox ID="toTextBox" runat="server" />
<br />Subject:<br />
<asp:TextBox ID="subjectTextBox" runat="server" />
<br />Introduction:<br />
601
Creating the Company Newsletter Page
<asp:TextBox ID="introTextBox" runat="server"
TextMode="MultiLine" Width="300" Height="100" />
<br />Employee Of The Month:<br />
<asp:TextBox ID="employeeTextBox" runat="server" />
<br />Featured Event:<br />
<asp:TextBox ID="eventTextBox" runat="server" />
<br />
<asp:Button ID="sendNewsletterButton" runat="server"
Text="Send Newsletter" />
</asp:Content>
Switch to Design View. The form should look like the one shown in Figure 14.18.
As you can see, the form contains seven TextBox controls, plus a Button and a
Label. The boxes will allow the administrator to specify who the email is to be
sent to and what the subject is, enter a simple introduction, identify the employee
of the month, and feature a company event and a store item. The Button control
is used to submit the form, while the Label control will display a confirmation
message once the email has been sent.
To ensure that only administrators can send email messages, add the code high-
lighted in bold below, which we discussed in detail in Chapter 13, to Web.config:
File: Web.config (excerpt)
<!-- Only administrators are allowed to access
AdminTools.aspx -->
<location path="AdminTools.aspx">
<system.web>
<authorization>
<allow roles="Administrators" />
<deny users="*" />
</authorization>
</system.web>
</location>
<!-- Only administrators are allowed to send emails -->
<location path="AdminNewsletter.aspx">
<system.web>
<authorization>
<allow roles="Administrators" />
<deny users="*" />
</authorization>
</system.web>
</location>
</configuration>
602
Chapter 14: Working with Files and Email
Figure 14.18. The Create Newsletter form
One hurdle that we need to overcome is that we want to include an image to be
displayed as part of the HTML content of the message. We can use either of two
approaches to solve this problem:
Host the image on our web server and reference it in an <img> tag in the
HTML code of the message (e.g. <img src="http://www.dorknozzle.com/Im-
ages/Newsletter.jpg" >).
Embed the image data in the email.
Well apply the first technique, as it has the benefit of simplicity, and keeps the
message as small as possible. If you want readers to see the image even when
theyre not connected to the Internet, you should look into the second option.
603
Creating the Company Newsletter Page
Developer Mike Pope explained image embedding, and provided sample code,
in a post on his blog.
3
All we need to do is handle the Send Newsletter buttons Click event. While in
Design View, double-click the button to generate the event handler signature. In
the code-behind file, well need to import the System.Net.Mail namespace:
Visual Basic File: AdminNewsletter.aspx.vb (excerpt)
Imports System.Net.Mail
C# File: AdminNewsletter.aspx.cs (excerpt)
using System.Net.Mail;
Then, complete the code of sendNewsletterButton_Click to send your newslet-
ter:
Visual Basic File: AdminNewsletter.aspx.vb (excerpt)
Protected Sub sendNewsletterButton_Click( _
ByVal sender As Object, ByVal e As System.EventArgs) _
Handles sendNewsletterButton.Click
Dim smtpClient As SmtpClient = New SmtpClient()
Dim message As MailMessage = New MailMessage()
' Try to send the message
Try
' Prepare two email addresses
Dim fromAddress As New MailAddress( _
"newsletter@dorknozzle.com", "Your Friends at Dorknozzle")
Dim toAddress As New MailAddress(toTextBox.Text)
' Prepare the mail message
message.From = fromAddress
message.To.Add(toAddress)
message.Subject = subjectTextBox.Text
message.IsBodyHtml = True
message.Body = _
"<html><head><title>" & _
HttpUtility.HtmlEncode(subjectTextBox.Text) & _
"</title></head><body>" & _
"<img src=""http://www.cristiandarie.ro/Dorknozzle" & _
"/Images/newsletter_header.gif"" />" & _
"<p>" & _
HttpUtility.HtmlEncode(introTextBox.Text) & "</p>" & _
"<p>Employee of the month: " & _
HttpUtility.HtmlEncode(employeeTextBox.Text) & "</p>" & _
3
http://www.mikepope.com/blog/DisplayBlog.aspx?permalink=1264
604
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.