locked
An invalid character was found in the mail header: ','.

    Question

  • I have a web page that sends emails.It loops over emails dataset and call send function.
     Dim mail As New SmtpClient  
    02. Dim r As New Regex("((([\w\W])*(\(?(\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)\)?))(([\w\W])*((\;\,\؛)\(?(\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)\)?))*)")  
    03. 
    04. 'Dim Attachment As New Attachment  
    05. Dim Mailmsg As New MailMessage  
    06. 
    07.Mailmsg.From = New MailAddress(FromAddress, FromName, Encoding.GetEncoding("Windows-1256"))  
    08.     Dim m As Match = r.Match(Toemail)  
    09. 
    10.     If (m.Success) Then 
    11.       Mailmsg.To.Add(New MailAddress(Toemail, ToName))  
    12. 
    13.       If (BCCAddress <> "") Then 
    14.         Mailmsg.Bcc.Add(New MailAddress(BCCAddress, BCCName))  
    15.       End If 
    16. 
    17.       Mailmsg.Body = Left(BodyText, 24533)  
    18.       Mailmsg.BodyEncoding = Encoding.GetEncoding("Windows-1256")  
    19.       Mailmsg.Subject = subject  
    20.       Mailmsg.SubjectEncoding = Encoding.GetEncoding("Windows-1256")  
    21.   If ishtml = True Then 
    22.         Mailmsg.IsBodyHtml = True 
    23.       Else 
    24.         Mailmsg.IsBodyHtml = False 
    25.       End If 
    26. 
    27.     Try 
    28.         mail.Send(Mailmsg)  
    29.         Mailmsg.Dispose()  
    30.         mail.Dispose()  
    31.         sendmail = True 
    32.       Catch ex As Exception  
    33.         sendmail = False 
    34.         strerror = Err.Description  
    35.       End Try 
    36.     Else 
    37.       'do nothing  
    38.       sendmail = False 
    39.     End If 
    
    

    I noticed that sometimes the web page works and sends all emails and in other cases it gives the error

    [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

     

    Am i missing something?Do i have to use another encoding?

    • Moved by Kee Poppy Monday, December 06, 2010 1:15 PM (From:Visual Basic Language)
    Tuesday, November 23, 2010 2:45 PM

All replies


  • Dump with the error message the email addresses that fail.  Perhaps you will see a pattern, specifically a comma. 

    --
    Mike
    Wednesday, November 24, 2010 12:23 AM
  • Hi maha,

    Have you solved your issues?

    If have not, you could try posting this issue in System.Net forum for better support.

     

    Best Regards & Thanks

    Kee


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, November 30, 2010 5:48 AM