Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 3rd, 2012, 07:23 AM
Authorized User
Points: 400, Level: 6
Points: 400, Level: 6 Points: 400, Level: 6 Points: 400, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2009
Posts: 69
Thanks: 6
Thanked 0 Times in 0 Posts
Default ContactUs

Hi Imar,

My contact page and sending email was fine until I've changed the code as in Chapter 19. I followed the instructions carefully and keep getting this error:
"The specified string is not in the form required for an e-mail address"

The code in web.config:
Code:
<configuration>
  
  <appSettings>
    <add key="FromAddress" value="info@eleganceaccounting.com" />
    <add key="FromName" value="Elegance Accounting" />
    <add key="ToAddress" value="info@eleganceaccounting.com" />
    <add key="ToName" value="Elegance Accounting" />
    <add key="SendMailOnError" value="false" />
  </appSettings>
  <system.web>
    <pages theme="Yellow"></pages>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
        <add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
      </assemblies>
    </compilation>
  </system.web>
  <system.net>
    <mailSettings>
      <smtp deliveryMethod="SpecifiedPickupDirectory" from="Elegance Accounting &lt;info@eleganceaccounting.com&gt;">
        <specifiedPickupDirectory pickupDirectoryLocation="C:\TempMail" />
        <network host="smtp.eleganceaccounting.com"/>
      </smtp>
    </mailSettings>
  </system.net>
</configuration>
The code in AppConfiguration.vb
Code:
Public Class AppConfiguration
  Public Shared ReadOnly Property FromAddress() As String
    Get
      Dim result As String =
      WebConfigurationManager.AppSettings.Get("FromAddress")
      If Not String.IsNullOrEmpty(result) Then
        Return result
      End If
      Throw New Exception("AppSetting FromAddress not found in web.config file.")
    End Get
  End Property

  Public Shared ReadOnly Property FromName() As String
    Get
      Dim result As String =
      WebConfigurationManager.AppSettings.Get("FromName")
      If Not String.IsNullOrEmpty(result) Then
        Return result
      End If
      Throw New Exception("AppSetting FromName not found in web.config file.")
    End Get
  End Property

  Public Shared ReadOnly Property ToAddress() As String
    Get
      Dim result As String =
      WebConfigurationManager.AppSettings.Get("ToAddress")
      If Not String.IsNullOrEmpty(result) Then
        Return result
      End If
      Throw New Exception("AppSetting ToAddress not found in web.config file.")
    End Get
  End Property

  Public Shared ReadOnly Property ToName() As String
    Get
      Dim result As String =
      WebConfigurationManager.AppSettings.Get("ToName")
      If Not String.IsNullOrEmpty(result) Then
        Return result
      End If
      Throw New Exception("AppSetting ToName not found in web.config file.")
    End Get
  End Property

  Public Shared ReadOnly Property SendMailOnError() As Boolean
    Get
      Dim result As String =
      WebConfigurationManager.AppSettings.Get("SendMailOnError")
      If Not String.IsNullOrEmpty(result) Then
        Return Convert.ToBoolean(result)
      End If
      Throw New Exception("AppSetting SendMailOnError not found in web.config file.")
    End Get
  End Property
End Class
The code in ContactForm.ascx.vb:
Code:
Imports System.IO
Imports System.Net.Mail
Partial Class Controls_ContactForm
    Inherits System.Web.UI.UserControl

  Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    

  End Sub

  Protected Sub CustomValidator1_ServerValidate(source As Object, args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
    If Not String.IsNullOrEmpty(PhoneHome.Text) Or
      Not String.IsNullOrEmpty(PhoneBusiness.Text) Then
      args.IsValid = True
    Else
      args.IsValid = False
    End If
  End Sub

  Protected Sub PhoneHome_TextChanged(sender As Object, e As System.EventArgs) Handles PhoneHome.TextChanged

  End Sub

  Protected Sub SendButton_Click(sender As Object, e As System.EventArgs) Handles SendButton.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##", Name.Text)
      mailbody = mailbody.Replace("##Email##", EmailAddress.Text)
      mailbody = mailbody.Replace("##HomePhone##", PhoneHome.Text)
      mailbody = mailbody.Replace("##BusinessPhone##", PhoneBusiness.Text)
      mailbody = mailbody.Replace("##Comments##", Comments.Text)

      Dim myMessage As MailMessage = New MailMessage()
      myMessage.Subject = "Response from web site"
      myMessage.Body = mailbody

      myMessage.From = New MailAddress("AppConfiguration.FromAddress, AppConfiguration.FromName")
      myMessage.To.Add(New MailAddress("AppConfiguration.ToAddress, AppConfiguration.ToName"))

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

      Message.Visible = True
      FormTable.Visible = True
      Name.Text = String.Empty
      EmailAddress.Text = String.Empty
      ConfirmEmailAddress.Text = String.Empty
      PhoneHome.Text = String.Empty
      PhoneBusiness.Text = String.Empty
      Comments.Text = String.Empty

      System.Threading.Thread.Sleep(5000)
    End If
  End Sub
End Class
Please help

Khalil
Reply With Quote
  #2 (permalink)  
Old February 3rd, 2012, 07:39 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

Take a look at this:

Quote:
myMessage.From = New MailAddress("AppConfiguration.FromAddress, AppConfiguration.FromName")
myMessage.To.Add(New MailAddress("AppConfiguration.ToAddress, AppConfiguration.ToName"))
You're passing a literal string to the MailAddress constructor. Remove the four double quotes and it should work.

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!
Reply With Quote
  #3 (permalink)  
Old February 3rd, 2012, 04:22 PM
Authorized User
Points: 400, Level: 6
Points: 400, Level: 6 Points: 400, Level: 6 Points: 400, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2009
Posts: 69
Thanks: 6
Thanked 0 Times in 0 Posts
Default ContactUs

Hi Imar,

It is working. Thank you very much. you are wonderful and very kind person.

Regards
Khalil
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off




All times are GMT -4. The time now is 08:52 AM.


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