12

What can cause the error "The specified string is not in the form required for an e-mail address"?

Source code line that causes the error:

msg.To.Add(new MailAddress("txtEmail.Text"));
Jon Schneider
  • 25,758
  • 23
  • 142
  • 170
parveen
  • 129
  • 1
  • 1
  • 4

7 Answers7

16
msg.To.Add(new MailAddress("txtEmail.Text"));

is the problem. txtEmail.Text is not an e-mail address. If that's a text file that's a list of e-mails, you're going to need to open it and read it and pass them in one by one.

If it's referring to a textbox, take the quotes around it off. Like this:

msg.To.Add(new MailAddress(txtEmail.Text));
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
ajma
  • 12,106
  • 12
  • 71
  • 90
13

For me, the problem was using semi-colon(;) to seperate multiple emails. Once I change it to comma(,) it works. Hope this helps someone.

chodae
  • 141
  • 1
  • 2
5

The issue on the code above might have occured due to

msg.To.Add(new MailAddress("txtEmail.Text"));

You might be clear that here "txtEmail.Text" appears as a string but not the mailing address to whom the mail is to be send. So code should be replaced with

msg.To.Add(new MailAddress(txtEmail.Text));

and sometimes the error like "The specified string is not in the form required for an e-mail address" might also occur due to use of improper string.As even I faced it.

Basically I was working in email sending task using the ASP.Net. The main issue for me was sending mail to multiple users. Firstly, I retrieved the email address from database and used " ; " so as to separate the multiple email addresses. Because while sending the email to multiple users, in regular basis we use semicolon i.e. " ;"

Everything seemed ok but after compilation I got the error "The specified string is not in the form required for an e-mail address".

After a bit analysis, I came to know that instead of using the " ; " we should use " , " so as to separate multiple email address while sending mails . This is the formatted string for separating the emails.

For Details visit: http://kopila.com.np

Thank you!

user1780336
  • 121
  • 1
  • 4
3

both the sender and recipient address need to be a valid email address format. eg. user@domain.com

WraithNath
  • 17,658
  • 10
  • 55
  • 82
1

Guess what the issue was for us?

Trailing spaces. For some reason, when you use a multi-line text box, spaces are added to the front of the string.

When we used Trim on the string, it worked fine.

Deltatech
  • 11
  • 2
0

I had a problem where I was creating the message with the recipient and the sender already in it, but it returned the following error:

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

The problematic code was as follows:

MailMessage objMsg = new MailMessage(regEmail.Text.ToString(), "me@mysite.com");

I fixed the issue by replacing that code with this:

MailMessage objMsg = new MailMessage();
objMsg.From = new MailAddress(regEmail.Text.ToString());
objMsg.To.Add(new MailAddress("me@mysite.com"));

It is also helpful to use a regular expression validator in your user control to make sure the address is valid, you can use the following code for asp:

<asp:RegularExpressionValidator ID="regex1" ControlToValidate="regEmail" ErrorMessage="Please enter a valid email address" ValidationExpression="^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$" ValidationGroup="regGroup" runat="server" Display="None" SetFocusOnError="True"></asp:RegularExpressionValidator>

Or if you'd prefer to validate the email in C# then you can use this as also stated by S Fadhel Ali:

public static bool IsValidEmail(String Email)
{
    if( Email != null && Email != "" )
        return Regex.IsMatch(Email, @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" );
    else
        return false;
}
Luke Alderton
  • 3,198
  • 1
  • 23
  • 34
0

It should be in the following format.

Dim myMail As New Net.Mail.MailMessage(New MailAddress(strFrom), New MailAddress(strTo))
Mukund Thakkar
  • 1,225
  • 14
  • 19