Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
| Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old March 15th, 2010, 08:13 PM
Registered User
 
Join Date: Mar 2010
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default Sending Mail from ContactForm User Control

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">
      &nbsp;</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">
      &nbsp;</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">
      &nbsp;</td>
    <td class="style2">
      <asp:Button ID="btnSend" runat="server" Text="Send" />
    </td>
    <td class="style5">
      &nbsp;</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("scanlanlv@gmail.com", "Debra Sender")
      myMessage.To.Add(New MailAddress("scanlanlv@gmail.com", "Debra Receiver"))

      Dim mySmtpClient As SmtpClient = New SmtpClient()
      mySmtpClient.Send(myMessage)

      lblMessage.Visible = True
      FormTable.Visible = False
    End If
  End Sub
End Class
  #2 (permalink)  
Old March 16th, 2010, 10:01 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

The code in the e-mail Demo page is almost the same as in the ContactForm user control.So you need to carry out the exact same steps (set EnableSsl to true) in the user control as you did in the demo page.

Does that help?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
debscan (March 18th, 2010)
  #3 (permalink)  
Old March 16th, 2010, 10:02 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

By the way, for book related questions as this one you're better off posting in the book's own forum: http://p2p.wrox.com/book-beginning-a...0-18759-3-389/

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #4 (permalink)  
Old March 18th, 2010, 01:35 AM
Registered User
 
Join Date: Mar 2010
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default

That was exactly it. And I will definitely check out the book site.

Thank you!


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sending mail from ContactForm: how to include a file? vilcene BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 4 September 23rd, 2009 12:18 PM
problems sending mail with java mail gandacuboy J2EE 2 December 20th, 2006 03:05 PM
Add Windows User control in Web User Control agarwalvidhu C# 0 March 30th, 2006 01:17 AM
Sending e-mail to different mail box! Calibus Classic ASP Databases 4 September 3rd, 2004 05:48 PM
Sending both text mail and HTML mail - CDONTS madhukp Classic ASP Basics 1 October 8th, 2003 01:05 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.