This article is for ASP.NET 1.x. For a 2.0 snippet read Sending Email in ASP.NET 2.0 (VB.NET).
By now we all know how to send server-side email using classic ASP. We either use Microsoft's CDONTS technology or a third-party component such as ServerObjects's ASPMail or Persist's ASPEmail. With ASP.NET server-side email is built-in and can be accessed using the System.Web.Mail namespace. In this article, I will demonstrate how to generate an email with form validation.
The Example
A lot of people post their resume on their web site. The advantage of having a resume available for recruiters and potential employers on a web site is valuable. The downside is you don't know who is reading your resume. A simple way to resolve that is to set up a form where the user requests that the resume be emailed to them. Then you can blind carbon copy yourself and you'll know exactly who showed interest. No more guessing if an employer pulled your resume, you'll have a receipt.
The Form
Elements we will want to capture are name, email, company, and the format of the resume they wish to receive. All fields will be required to successfully submit the form. Let's code the form using ASP.NET Web Form controls and attach validation controls.
<div id="requestResume" runat="server">
<!-- The values on this FORM will be posted back to the server. -->
<!-- We will add the 'runat="server"' to the FORM and controls -->
<form id="Form1" method="post" runat="server">
<table>
<tr><td>Name</td>
<td><input type="text" id="txtName"
value="" size="30" maxlength="50"
runat="server" name="txtName"/>
<!-- txtName is required. We will attach a
RequiredFieldValidator by assigning
txtName as the ControlToValidate -->
<asp:RequiredFieldValidator id="valRequiredName"
runat="server"
ControlToValidate="txtName"
ErrorMessage="* You must enter your Full Name."
Display="dynamic".>
</td></tr>
<tr><td>Email</td>
<td> <input type="text" id="txtEmail"
value="" size="30" maxlength="50"
runat="server" NAME="txtEmail"/>
<!-- txtEmail is both required and must be a valid email address. -->
<asp:RequiredFieldValidator id="valRequiredEmail"
runat="server"
ControlToValidate="txtEmail"
ErrorMessage="* You must enter your Email address."
Display="dynamic"/>
<!-- txtEmail will be validated using Regular Expression.
It is also attached to txtEmail via the ControlToValidate property. -->
<asp:RegularExpressionValidator id="valValidEmail"
runat="server"
ControlToValidate="txtEmail"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
ErrorMessage="* You must enter a valid Email address"
Display="dynamic"/>
</td></tr>
<tr><td>Company</td>
<td> <!-- txtCompany works exactly like txtName. -->
<input type="text" id="txtCompany" value="" size="30"
maxlength="50" runat="server"
NAME="txtCompany"/>
<asp:RequiredFieldValidator id="valRequiredCompany"
runat="server"
ControlToValidate="txtCompany"
ErrorMessage="* You must enter your Company name."
Display="dynamic"/>
</td></tr>
<tr><td>Resume Format</td>
<td>
<!-- The dropdown list of resume formats
will be assigned on the server -->
<asp:DropDownList id="selResume" Runat="server"/>
</td></tr>
<tr>
<td></td>
<td>
<!-- When the FORM is submitted it will
execute the "btnSubmit_OnClick"
Subroutine on the server. -->
<asp:button type="submit" name="btnSubmit"
onclick="btnSubmit_OnClick"
text="Request Resume"
runat="server"/> </td></tr>
</table>
</form>
</div>
Server-Side Code (VB.NET)
Regardless of what language you utilize, the above FORM will be coded the same way. To proceed on the server-side code, we need to pick a language. C# and Visual Basic are the two most popular at this time. Let's proceed in VB. The first line imports the ASP.NET code needed to perform sending email.
<%@ Import Namespace="System.Web.Mail" %>
<script language="VB" runat="server">
Sub Page_Load()
'=== When the page first loads populate the Resume Format dropdown
If NOT IsPostBack() Then
Call populateResumeFormats()
selResume.DataValueField = "Value"
selResume.DataTextField = "Key"
selResume.DataBind()
End If
End Sub
Public Sub populateResumeFormats
'=== create a HashTable to populate the dropdown.
Dim dropResume As New HashTable(4)
dropResume.Add("Word 97 (.RTF)", "resume.rtf")
dropResume.Add("HTML", "resume.htm")
dropResume.Add("Word 2002", "resume.doc")
dropResume.Add("Text", "resume.txt")
selResume.DataSource = dropResume
End Sub
Sub btnSubmit_OnClick(o as Object, e as EventArgs)
'=== The Page.IsValid call checks all the validation controls.
'=== If they all pass then the form is valid.
If Page.IsValid Then
'=== no longer display the FORM, display a status message.
requestResume.InnerHTML = "You will receive an email shortly with my resume.
Thank you for your interest."
Call sendResume
End If
End Sub
Sub sendResume
'=== create a MailMessage
Dim resumeEmail as New MailMessage
Dim strResumeFilePath as String
'=== To, CC, BCC, From, Subject are self-explainatory
resumeEmail.To = txtEmail.value
resumeEmail.BCC = "larryking@cnn.com"
resumeEmail.From = "larryking@cnn.com"
resumeEmail.Subject = "Resume for Larry King"
resumeEmail.BodyFormat = MailFormat.text
resumeEmail.Body = txtName.value & ", attached is a copy of my resume.
I look forward to working with you. - MAS "
'=== for this example all versions of the resume reside in the
/resume directory
strResumeFilePath = Server.MapPath("/") & "\resume\" & selResume.SelectedItem.value
'=== create an attachment and add the file path to that attachment
Dim resumeAttachment as New MailAttachment(strResumeFilePath)
resumeEmail.Attachments.Add(resumeAttachment)
'=== send the email
SmtpMail.Send(resumeEmail)
End Sub </script>
Last Words
Pretty straightforward isn't it? Now you have a working resume mailer. The employer can specify the preferred format, and the potential employee gets to track who is viewing the resume.
This article was written in 2001 for ASP.NET 1.0. Forgive the non XHTML.Labels: ASP.NET, Email, VB.NET