Question An invalid character was found in the mail header: ';'.

BJhillC

New member
Joined
Mar 30, 2011
Messages
1
Programming Experience
Beginner
Hi all,

I am new to the forum as well as asp.net in general. I am trying to create a simple contact us form but get the following error:
Below I have also attached the code behind file that throws the error. Any help would be greatly appreciated.

Thanks in advance.
An invalid character was found in the mail header: ';'.

[FONT=Arial, Helvetica, Geneva, SunSans-Regular, sans-serif]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

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

[/FONT]
 
The parameterless constructor:
From is set to the value in the network element for mailSettings<smtp> Element (Network Settings), if it exists.
So it retrieves this from Web.config file (or Machine.config), which in your case has invalid data. You could use the constructor that takes From/To elements directly.
 
Back
Top