What can cause "The specified string is not in the form required for the email address"?

What can cause the error "The specified line is not in the form required for the email address"?

Source line that causes the error:

msg.To.Add(new MailAddress("txtEmail.Text"));
+5
source share
7 answers
msg.To.Add(new MailAddress("txtEmail.Text"));

- a problem. txtEmail.Text is not an email address. If this is a text file containing a list of emails, you will need to open it and read it and transfer them one at a time.

If it refers to a text field, disable quotation marks. Like this:

msg.To.Add(new MailAddress(txtEmail.Text));
+13
source

(;) . (,), . , -.

+10

, , -

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

, , "txtEmail.Text" , , .

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

, , , , - . .

ASP.Net. . -, used ";" , . , .. ";"

, " , ".

, ";" ",", . .

: http://kopila.com.np

!

+3

, , , "txtEmail.Text" ? , , ...

+1

, . . user@domain.com

0

, , :

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

:

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

, :

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

It is also useful to use the regex 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 prefer checking email in C #, you can use this as S Fadhel Ali also points out:

public static bool IsValidEmail(String Email)
{
    if( Email != null && Email != "" )
        return Regex.IsMatch(Email, @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" );
    else
        return false;
}
0
source

It should be in the following format.

Dim myMail As New Net.Mail.MailMessage(New MailAddress(strFrom), New MailAddress(strTo))
0
source

All Articles