.NET Framework Developer Center > .NET Development Forums > .NET Framework Networking and Communication > The specified string is not in the form required for an e-mail address
Ask a questionAsk a question
 

QuestionThe specified string is not in the form required for an e-mail address

  • Friday, September 07, 2007 4:44 PMJohnStockton Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I have searched and searched and searched for an answer to this problem...

    I have been using the System.Net.Mail namespace for quite a while to send emails from my websites. A problem occurs though on certain (VALID) email addresses. For example, when the name field of the email address has a dot (.) in it (example: my.name@email.com), an exception is raised when this address is added to the TO property of the MailAdddress object. It seems the dot between my and name causes the exception.

    Please note that these are valid email addresses, but the MailAddress object considers them to be invalid.

    Any suggestions/workarounds?

    Error Message: The specified string is not in the form required for an e-mail address.
    Stack Trace: at System.Net.Mime.MailBnfHelper.ReadMailAddress(String data, Int32& offset, String& displayName)
    at System.Net.Mail.MailAddress.ParseValue(String address)
    at System.Net.Mail.MailAddress..ctor(String address, String displayName, Encoding displayNameEncoding)
    at System.Net.Mail.MailAddress..ctor(String address, String displayName)

All Replies

  • Saturday, September 08, 2007 10:24 AMAlan J. McFarlane Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    The problem is not the dot:

    Code Snippet

    Console.WriteLine(new MailAddress("foo.bar@example.com", "Fred"));

    gives:

       "Fred" <foo.bar@example.com>

     

    But

    Code Snippet

    Console.WriteLine(new MailAddress("foo@bar@example.com", "Fred"));

    gives:

    Unhandled Exception: System.FormatException: The specified string is not in the form required for an e-mail address.
       at System.Net.Mime.MailBnfHelper.ReadMailAddress(String data, Int32& offset, String& displayName)
       at System.Net.Mail.MailAddress.ParseValue(String address)
       at System.Net.Mail.MailAddress..ctor(String address, String displayName, Encoding displayNameEncoding)
       at System.Net.Mail.MailAddress..ctor(String address, String displayName)
       at MailAddressDotted.Main(String[] args) in c:\Temp\2007-08\MailAddressDotted.cs:line 16

     

    So there must be something other than the dot wrong with your addresses...
  • Saturday, September 08, 2007 11:44 PMAbdulrahiman Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    John & Alan,

     

    Even am facing same problem since yesterday and finding a solution to fix this weird problem.

     

    I get the same messege as John's..which is as below. The weird thing to notice is similar code works fine in one of my other page where I use textboxes to pass values instead of variables as in here, and it works absolutely fine in there and also I receive email to the same email id used below, but it ain't work here while placing the values in variables.

     

    Some one please helppppppppppppppppppppp.................ASAP

     

    Also appreciate if someone kindly drop me an email to armohamm@gmail.com with the solution for this problem. Thanks

    --------------------------------------------------------------------------------------------------------------------- 

    The specified string is not in the form required for an e-mail address.

    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: The specified string is not in the form required for an e-mail address.

    Source Error:

    Line 22:         smtpName = "localhost"
    Line 23: 
    Line 24:         Dim emailMsg As New MailMessage(MsgFrom, MsgTo, msgSub, msgBody)
    Line 25:         emailMsg.IsBodyHtml = True
    Line 26:         ''emailMsg.Priority = System.Net.Mail.MailPriority.High

    Source File: C:\Documents and Settings\Abdul Rahiman\My Documents\Visual Studio 2005\WebSites\sendsms\SendEmail.aspx.vb    Line: 24

    Stack Trace:

    [FormatException: The specified string is not in the form required for an e-mail address.]
       System.Net.Mime.MailBnfHelper.ReadMailAddress(String data, Int32& offset, String& displayName) +898147
       System.Net.Mail.MailAddress.ParseValue(String address) +245
       System.Net.Mail.MailAddress..ctor(String address, String displayName, Encoding displayNameEncoding) +87
       System.Net.Mail.Message..ctor(String from, String to) +127
       System.Net.Mail.MailMessage..ctor(String from, String to) +117
       System.Net.Mail.MailMessage..ctor(String from, String to, String subject, String body) +13
       SendEmail.Button1_Click(Object sender, EventArgs e) in C:\Documents and Settings\Abdul Rahiman\My Documents\Visual Studio 2005\WebSites\sendsms\SendEmail.aspx.vb:24
       System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
       System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
       System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102
    

     

    My code goes as below..

     

    Imports System

    Imports System.Data

    Imports System.Data.SqlClient

    Imports System.Net.Mail

     

    Partial Class SendEmail

    Inherits System.Web.UI.Page

    Dim MsgFrom As String

    Dim MsgTo As String

    Dim msgSub As String

    Dim msgBody As String

    Dim smtpName As String

     

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

     

    MsgFrom = "Abdulrahiman"

    MsgTo = "armohamm@yahoo.co.in"

    msgSub = "My Subject Test"

    msgBody = "Testing Email Msg"

    smtpName = "localhost"

    Dim emailMsg As New MailMessage(MsgFrom, MsgTo, msgSub, msgBody)

    emailMsg.IsBodyHtml = True

    ''emailMsg.Priority = System.Net.Mail.MailPriority.High

    '' emailMsg.SubjectEncoding = System.Text.Encoding.GetEncoding(1256)

    ''emailMsg.BodyEncoding = System.Text.Encoding.GetEncoding(1256)

    Dim mySmtp As New Net.Mail.SmtpClient("" & smtpName & "")

    Try

    mySmtp.Send(emailMsg)

    lblResult.Text = "Email sent successfully"

    Catch ex As Exception

    lblResult.Text = "Send Failed !!: " + ex.Message

    End Try

    End Sub

    End Class

  • Sunday, September 09, 2007 12:35 AMtimvw Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
     Abdulrahiman wrote:
     

    Also appreciate if someone kindly drop me an email to armohamm@gmail.com with the solution for this problem.



    We're not your personal helpdesk. If you ask a question here, you'll get the answer here (this way other people with the same problem can find and answer)

     Abdulrahiman wrote:

    MsgFrom = "Abdulrahiman"


    This is the e-mail address that could not be parsed...



    MsgFrom = "armohamm@yahoo.co.in" would work...

  • Monday, September 17, 2007 7:22 PMCraig Barber Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I just ran into a similiar issue where a dot, ".", preceding the at-sign, "@", would cause an exception to be thrown.


    Examples:


    Console.WriteLine(new MailAddress("foo.bar@example.com", "Fred")); // OK

    Console.WriteLine(new MailAddress("f.o.o.b.a.r@example.com", "Fred")); // Still OK

    Console.WriteLine(new MailAddress("foobar.@example.com", "Fred")); // BOOM!


    Apparently, the code is doing the "right" thing. RFC 2822 defines the local part as being atext or "." surrounded by atext characters. From the spec:


    "The locally interpreted string is either a quoted-string or a dot-atom. If the string can be represented as a dot-atom (that is, it contains no characters other than atext characters or "." surrounded by atext characters),"


    The issue arrises because some email providers (I tested gmail, earthlink, and comcast) allow users to create and deliver to addresses in the aformentioned format. The users will only be confused or frustrated if their pseudo-valid email address is denied on the form, so I'm adding my own hack to remove /^.|.(?=@)/ from the email address.


    Hope this post helps someone out running into the same issue.



    Sorry about the lack of format. I made earnest attempts in both Firefox and Opera and could not find a reference to the forum's syntax. MSDN forums fail at usability Stick out tongue

  • Tuesday, September 09, 2008 2:49 PMRohan Reddy Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    I also came across this issue in my app and found the solution in another forum post. Here is the text from the post: 

    It's in your <system.net><mailSettings><smtp> element in your web.config. Make sure the "from" attribute of the <smtp> element contains a valid email address
    -----------------
    Link: http://forums.asp.net/t/1183968.aspx

    Thanks,
    Rohan Reddy
  • Friday, July 17, 2009 3:08 AMAlanZX Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Help! Just ran into the similar situation and I've absolutely no idea what's causing that:

    Mail To    Address: user2@domain.com.au  (valid)
          CC   Address : test@subdomain.domain.com.au (valid)
          From Address: user1@domain.com.au (valid)

    system.web.mail throw an error of "The server rejected one or more recipient addresses. The server response was: 501 5.5.4 Invalid Address "
    while system.net.mail gives "The specified string is not in the form required for an e-mail address. "
    but everything works after removing the test@subdomain.domain.com.au from list. or works fine when sending through outlook manualy 

    Could anyone shed some light on this issue?

  • Saturday, November 28, 2009 11:00 AMGeorge I. Birbilis Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    You can use the "Alert me" button at the top (with the bell ring) to sign up for notifications of updates to this content
    Microsoft MVP J# 2004-2008, Borland Spirit of Delphi 2001
  • Monday, December 28, 2009 11:59 PMMAbshir Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Well, I'm pretty sure everybody is now got their problem solved but i thought i spelled out the solution for future comers.
    First, i got this error because i was sending email from the CreateUserWizard  controls send email feature.

    My problem was that I made the UserName to be the Email and Email be username.
    Eventhough i changed the naming in the .aspx file it broke the "TO" field in the email object.

    So, to remedy that, i catch one of the routines of the "createUserWizard" prior to sending the email and swaped the values.

    Basically, if you're getting this error from "SMTP.send()" ensure that the "TO" field has a valid email address.