, , :
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;
}
source
share