Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
| Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB 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 31st, 2011, 12:29 AM
Registered User
 
Join Date: Mar 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Sending Mail from the ContactForm User Control

Hi,

I have been following the Try It Out exercise on Pg. 323, I have basically followed all of the steps but get an error when I try and send an e-mail.

Code:
An invalid character was found in the mail header: ';'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: An invalid character was found in the mail header: ';'.

Source Error:

Line 24:             mailBody = mailBody.Replace("##Comments##", txtComments.Text)
Line 25: 
Line 26:             Dim myMessage As MailMessage = New MailMessage()
Line 27:             myMessage.Subject = "Response from web site"
Line 28:             myMessage.Body = mailBody


Source File: F:\DOSWebSite\Controls\ContactForm.ascx.vb    Line: 26

Stack Trace:

[FormatException: An invalid character was found in the mail header: ';'.]
   System.Net.Mail.MailAddressParser.ParseLocalPart(String data, Int32& index, Boolean expectAngleBracket, Boolean expectMultipleAddresses) +1494824
   System.Net.Mail.MailAddressParser.ParseAddress(String data, Boolean expectMultipleAddresses, Int32& index) +147
   System.Net.Mail.MailAddressParser.ParseAddress(String data) +23
   System.Net.Mail.MailAddress..ctor(String address, String displayName, Encoding displayNameEncoding) +238
   System.Net.Mail.MailMessage..ctor() +133
   Controls_ContactForm.SendButton_Click(Object sender, EventArgs e) in F:\DOSWebSite\Controls\ContactForm.ascx.vb:26
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1
My code behing file looks like this:

Code:
Imports System.IO ' Provides access to the File class for reading the file
Imports System.Net.Mail ' Provides access to the various mail related classes

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 Not String.IsNullOrEmpty(txtPhoneCell.Text) Or Not String.IsNullOrEmpty(txtPhoneBusiness.Text) Then
            args.IsValid = True
        Else
            args.IsValid = False
        End If
    End Sub

    Protected Sub SendButton_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 = File.ReadAllText(fileName)

            mailBody = mailBody.Replace("##Name##", txtName.Text)
            mailBody = mailBody.Replace("##Email##", txtEmail.Text)
            mailBody = mailBody.Replace("##HomePhone##", txtPhoneCell.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("TheDutchOvenShoppe@gmail.com", "Sender Name")
            myMessage.To.Add(New MailAddress("TheDutchOvenShoppe@gmail.com", "Receiver Name"))

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

            Message.Visible = True
            FormTable.Visible = False
        End If
    End Sub

End Class
Any help on this error would be greatly appreciated.

Thank you
  #2 (permalink)  
Old March 31st, 2011, 03:09 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,

How does your ContactForm.txt file look? Try recreating the file with Notepad as it may have seen saved with some strange encoding? Also, can you try it when that file contains just plain text (for example, ##Comments## and nothing else).

Finally, check the settings for the smtp server in web.config. Maybe there's an error in the mail address you configred there?

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!
  #3 (permalink)  
Old April 2nd, 2011, 12:38 AM
Registered User
 
Join Date: Mar 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar,

Thanks for responding so quickly to my question. I actually found a problem in the web.config file. Everything seems to be working correctly now.

Thanks again
  #4 (permalink)  
Old April 2nd, 2011, 06: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

Hi there,

Would you mind sharing *how* you fixed it? E.g. what the error in web.config was and how you resolved it? I am sure a future visitor with a similar problem will appreciate the solution.

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!
  #5 (permalink)  
Old April 3rd, 2011, 11:34 PM
Registered User
 
Join Date: Mar 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default ContactForm User Control

My problem was nothing more than a typo:

<smtp deliveryMethod="Network" from="Your Name &lt;TDOShoppe@gmail.com&gt;">

I had typed * instead of & in front of my e-mail address.

I do have an add on question for the same topic now. The form is sending e-mail correctly but the .Replace command doesn't seem to be working. I receive the e-mail but only receive the original .txt file (the comments from the contact form are not sent).

Name: ##txtName##
E-mail address: ##txtEmail##
Cell phone: ##txtPhoneCell##
Business phone: ##txtPhoneBusiness##
Comments: ##txtComments##

Thanks again for any help.
  #6 (permalink)  
Old April 4th, 2011, 02:30 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,

Thanks for the follow up.

With regard to the message: you're searching for things like ##Name##, but the message body uses (control) prefixes. E.g. ##txtName## instead of just Nane. The two should be the same, or Replace won't be able to find it, and thus cannot replace it.

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!
  #7 (permalink)  
Old April 6th, 2011, 01:44 AM
Registered User
 
Join Date: Mar 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default GridView/Details View with TabContainer Control

Hi Imar,

Thanks again for the help. I am definitely a rookie with a lot still to learn. I have a new question. I have combined the Try It Out exercises from page 437 and 464 into one project. I am using a TabContainer Control and have created a separate form on each Tab. The problem that I am having is that when I, for example, want to add a customer to my database, I click on the Insert button but the RequiredFieldsValidator kicks in on the forms from the other tabs and I am unable to insert data into the database. I believe that the problem stems from the Page_Load Handler. My question is how I would handle the separate forms in the different tabs? My code behind file is as follows:

Code:
Partial Class Management_Default4
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Request.QueryString.Get("Dinner_ID") IsNot Nothing Then
            DetailsView3.DefaultMode = DetailsViewMode.Edit
        End If
        If Request.QueryString.Get("Owner_ID") IsNot Nothing Then
            DetailsView4.DefaultMode = DetailsViewMode.Edit
        End If
        If Request.QueryString.Get("Owner_ID") IsNot Nothing Then
            DetailsView5.DefaultMode = DetailsViewMode.Edit
        End If
    End Sub

    Protected Sub DetailsView3_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertedEventArgs) Handles DetailsView3.ItemInserted
        EndEditing()
    End Sub

    Protected Sub DetailsView3_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles DetailsView3.ItemInserting
        e.Values("UpdateDateTime") = DateTime.Now
    End Sub

    Protected Sub DetailsView3_ItemUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdatedEventArgs) Handles DetailsView3.ItemUpdated
        EndEditing()
    End Sub

    Protected Sub DetailsView3_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdateEventArgs) Handles DetailsView3.ItemUpdating
        e.NewValues("UpdateDateTime") = DateTime.Now
    End Sub

    'Private Sub EndEditing()
    'Response.Redirect("Default4.aspx")
    'End Sub

    Protected Sub DetailsView4_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertedEventArgs) Handles DetailsView4.ItemInserted
        EndEditing()
    End Sub

    Protected Sub DetailsView4_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles DetailsView4.ItemInserting
        e.Values("UpdateDateTime") = DateTime.Now
    End Sub

    Protected Sub DetailsView4_ItemUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdatedEventArgs) Handles DetailsView4.ItemUpdated
        EndEditing()
    End Sub

    Protected Sub DetailsView4_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdateEventArgs) Handles DetailsView4.ItemUpdating
        e.NewValues("UpdateDateTime") = DateTime.Now
    End Sub

    Private Sub EndEditing()
        Response.Redirect("Default4.aspx")
    End Sub
End Class
If this is not the correct forum to be posting this question forgive me. Any help would be greatly appreciated.

Thanks again.
  #8 (permalink)  
Old April 6th, 2011, 07:34 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,

(It would have been easier if you'd started a new thread for a new topic. Makes things easier to find and manage).

Anyway, you can solve this problem using ValidationGroups. Each control such as a TextBox and a Button, and the Validation controls have a ValidationGroup property. You assign the same value to controls you want validated as a unit. This enables you to have different groups for different sets / tabs / DetailsViews etc.

The downside is that you need to edit all the templates of the DetailsView to set the ValidationGroup properties which can be quite some work. Once you go down this path, you might as well completely hand-code the data access pages.

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!


Similar Threads
Thread Thread Starter Forum Replies Last Post
User Control Email Sending papimann ASP.NET 4 General Discussion 7 March 5th, 2011 06:17 AM
Sending Mail from ContactForm Filespit BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 1 July 12th, 2010 03:04 AM
Sending Mail from ContactForm User Control debscan ASP.NET 3.5 Basics 3 March 18th, 2010 01:35 AM
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
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.