Hello, I am a novice programmer following along in Beginning ASP.NET 3.5. In Chapter 9 I was able to successfully navigate through "Sending Email Messages," I did have to add the Ssl line of code for that to work. I have been unsuccessful at "Sending Mail from the ContactForm User Control." There were no notes in the book about things to try if the "Try It Out" instructions didn't work. I have included code from ContactForm.ascx and ContactForm.ascx.
vb below. Please let me know if you would need to view any other code.
Thank you!
Here is the code from the ContactForm.ascx
Code:
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="ContactForm.ascx.vb" Inherits="Controls_ContactForm" %>
<style type="text/css">
.style1
{
width: 125%;
}
.style2
{
width: 330px;
}
.style3
{
}
.style4
{
width: 174px;
}
.style5
{
width: 7px;
}
</style>
<script type="text/javascript">
function ValidatePhoneNumbers(source, args)
{
var txtPhoneHome = document.getElementById('<%= txtPhoneHome.ClientID %>');
var txtPhoneBusiness = document.getElementById('<%= txtPhoneBusiness.ClientID %>');
if (txtPhoneHome.value != '' || txtPhoneBusiness.value != '')
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
</script>
<table class="style1" runat="server" id="FormTable">
<tr>
<td colspan="3">
<br />
Use this contact form to get in touch with us. Enter your name, e-mail address
and your home or business phone number to contact us.</td>
</tr>
<tr>
<td class="style4">
Your Name</td>
<td class="style2">
<asp:TextBox ID="txtName" runat="server" Height="25px" Width="300px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtName" ErrorMessage="Please enter your name">*</asp:RequiredFieldValidator>
</td>
<td class="style5">
</td>
</tr>
<tr>
<td class="style4">
Your e-mail address</td>
<td class="style2">
<asp:TextBox ID="txtEmailAddress" runat="server" Height="25px" Width="300px"></asp:TextBox>
</td>
<td class="style5">
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtEmailAddress" Display="Dynamic"
ErrorMessage="Please enter an e-mail address">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="txtEmailAddress" Display="Dynamic"
ErrorMessage="Please enter a valid e-mail address"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td class="style4">
Your e-mail address again</td>
<td class="style2">
<asp:TextBox ID="txtEmailAddressConfirm" runat="server" Height="25px"
Width="300px"></asp:TextBox>
</td>
<td class="style5">
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="txtEmailAddressConfirm" Display="Dynamic"
ErrorMessage="Please confirm the e-mail adress">*</asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToCompare="txtEmailAddress" ControlToValidate="txtEmailAddressConfirm"
Display="Dynamic" ErrorMessage="Please retype the e-mail address">*</asp:CompareValidator>
</td>
</tr>
<tr>
<td class="style4">
Home phone number</td>
<td class="style2">
<asp:TextBox ID="txtPhoneHome" runat="server" Height="25px" Width="300px"></asp:TextBox>
</td>
<td class="style5">
<asp:CustomValidator ID="CustomValidator1" runat="server"
ClientValidationFunction="ValidatePhoneNumbers" Display="Dynamic"
ErrorMessage="Please enter your home or business phone number">*</asp:CustomValidator>
</td>
</tr>
<tr>
<td class="style4">
Business phone number</td>
<td class="style2">
<asp:TextBox ID="txtPhoneBusiness" runat="server" Height="25px" Width="300px"></asp:TextBox>
</td>
<td class="style5">
</td>
</tr>
<tr>
<td class="style4">
Comments</td>
<td class="style2">
<asp:TextBox ID="txtComments" runat="server" Height="89px" TextMode="MultiLine"
Width="350px"></asp:TextBox>
</td>
<td class="style5">
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ControlToValidate="txtComments" Display="Dynamic"
ErrorMessage="Please enter a comment">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style3" colspan="3">
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
HeaderText="Please correct the following errors before you press the Send button." />
</td>
</tr>
<tr>
<td class="style4">
</td>
<td class="style2">
<asp:Button ID="btnSend" runat="server" Text="Send" />
</td>
<td class="style5">
</td>
</tr>
</table>
<asp:Label ID="lblMessage" runat="server" Text="Message Sent" Visible="false"></asp:Label>
Here is the ContactForm.ascx.vb
Code:
Imports System.Net.Mail
Partial Class Controls_ContactForm
Inherits System.Web.UI.UserControl
Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
If txtPhoneHome.Text IsNot String.Empty Or txtPhoneBusiness.Text IsNot String.Empty Then
args.IsValid = True
Else
args.IsValid = False
End If
End Sub
Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click
If Page.IsValid Then
Dim fileName As String = Server.MapPath("~/App_Data/ContactForm.txt")
Dim mailBody As String = System.IO.File.ReadAllText(fileName)
mailBody = mailBody.Replace("##Name##", txtName.Text)
mailBody = mailBody.Replace("##Email##", txtEmailAddress.Text)
mailBody = mailBody.Replace("##HomePhone##", txtPhoneHome.Text)
mailBody = mailBody.Replace("##BusinessPhone##", txtPhoneBusiness.Text)
mailBody = mailBody.Replace("##Comments##", txtComments.Text)
Dim myMessage As MailMessage = New MailMessage()
myMessage.Subject = "Response from web site"
myMessage.Body = mailBody
myMessage.From = New MailAddress("[email protected]", "Debra Sender")
myMessage.To.Add(New MailAddress("[email protected]", "Debra Receiver"))
Dim mySmtpClient As SmtpClient = New SmtpClient()
mySmtpClient.Send(myMessage)
lblMessage.Visible = True
FormTable.Visible = False
End If
End Sub
End Class