Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"));
like image 888
parveen Avatar asked Jan 21 '11 08:01

parveen


5 Answers

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.

like image 111
Deltatech Avatar answered Sep 30 '22 13:09

Deltatech


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));
like image 22
ajma Avatar answered Nov 17 '22 17:11

ajma


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

like image 20
chodae Avatar answered Nov 17 '22 16:11

chodae


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!

like image 5
user1780336 Avatar answered Nov 17 '22 17:11

user1780336


both the sender and recipient address need to be a valid email address format. eg. [email protected]

like image 3
WraithNath Avatar answered Nov 17 '22 17:11

WraithNath