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

ud2008

Well-known member
Joined
Jul 5, 2010
Messages
148
Programming Experience
Beginner
I am using vb2010 and winforms.

I've been trying to send an email and got almost everything right, but getting an error:

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

I have the following code:
VB.NET:
Private Sub SendButton_Click(sender As Object, e As System.EventArgs) Handles SendButton.Click
        Dim ToString As String = ToTextBox.Text
        Dim smtpServer As New SmtpClient
        Dim mail As New MailMessage
        smtpServer.Credentials = New Net.NetworkCredential(My.Settings.EmailUser, My.Settings.EmailPW)
        smtpServer.Port = My.Settings.EmailPort
        smtpServer.Host = My.Settings.EmailHost
        'smtpServer.EnableSsl = True
        mail = New MailMessage
        mail.From = New MailAddress(My.Settings.EmailUser)
        [B][COLOR="#FF0000"]mail.To.Add(ToString)[/COLOR][/B]
        If CCTextBox.Text > "" Then
            mail.CC.Add(CCTextBox.Text)
        End If
        mail.Subject = SubjectTextBox.Text
        Dim msgBody As String
        msgBody = "<html><head></head>"
        msgBody = "<body style='background:#FFF;'>"
        msgBody = "<table width='100%' border='0'>"
        msgBody = "<tr><td><h3>Songlist for: " + SongServiceEdit1.SelectedText + ", " + DateTimeInput1.Text + "</h3></td></tr>"
        msgBody = "<tr><td> </td></tr>"
        msgBody = "<tr><td>Worshipservice: " + SongServiceEdit1.SelectedText + "</td></tr>"
        msgBody = "<tr><td>Worshipleader(s): " + LeadersEdit1.SelectedText + "</td></tr>"
        msgBody = "<tr><td></td></tr>"
        msgBody = "<tr><td>Songs: <br></td></tr>"
        For i = 0 To ListBox3.Items.Count - 1
            msgBody = "<tr><td>" + ListBox3.Items.Add(i) + "<br></td></tr>"
        Next
        msgBody = "<tr><td></td></tr>"
        msgBody = "<tr><td>Message:</td></tr>"
        msgBody = "<tr><td>" + RichEditControl1.HtmlText + "</td></tr>"
        msgBody = "</table><br><br>"
        msgBody = "Created with " + Application.Title + " version: " + Application.ProductVersion
        msgBody = "</body></html>"
        mail.Body = msgBody
        mail.HeadersEncoding = System.Text.Encoding.UTF8
        mail.BodyEncoding = System.Text.Encoding.UTF8
        mail.IsBodyHtml = True
        Try
            smtpServer.Send(mail)
            MessageBox.Show("Songlist Send!", "Application title", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

The error occurs with the line in bold/red. Here are the email addresses I want to send an email to, which can contain multiple email addresses separated by a semicolon.
I have set the encoding, but still the error occurs.

Any thought?
 
Back
Top