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="[email protected]" />
<add key="FromName" value="Elegance Accounting" />
<add key="ToAddress" value="[email protected]" />
<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 <[email protected]>">
<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