Search code examples
mysqlasp.netmembershippassword-recovery

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


I'm trying to have a recovery password page in my website, but there's an error every time: "The specified string is not in the form required for an e-mail address."

I enter the username, clicking submit. It takes me to the secret question and answer page, I type the answer and clicking submit. Then, it shows me the error about the email. Is there a fix for it?

This is my page code:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPageTest.master" AutoEventWireup="true" CodeFile="forgotpass.aspx.cs" Inherits="forgotpass" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<br />
<center><b>
    <asp:PasswordRecovery ID="PasswordRecovery1" runat="server" 
        TextLayout="TextOnTop" 
        UserNameInstructionText="הכנס את שם המשתמש שלך כדי לקבל את סיסמתך" 
        UserNameLabelText="שם משתמש:" UserNameTitleText="שכחת את סיסמתך?">
        <UserNameTemplate>
            <table cellpadding="1" cellspacing="0" style="border-collapse:collapse;">
                <tr>
                    <td>
                        <table cellpadding="0">
                            <tr>
                                <td align="center">
                                    שכחת את סיסמתך?</td>
                            </tr>
                            <tr>
                                <td align="center">
                                    הכנס את שם המשתמש שלך כדי לקבל את סיסמתך</td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">שם משתמש:</asp:Label>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:TextBox ID="UserName" runat="server"></asp:TextBox>
                                    <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" 
                                        ControlToValidate="UserName" ErrorMessage="User Name is required." 
                                        ToolTip="User Name is required." ValidationGroup="PasswordRecovery1">*</asp:RequiredFieldValidator>
                                </td>
                            </tr>
                            <tr>
                                <td align="center" style="color:Red;">
                                    <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:Button ID="SubmitButton" runat="server" CommandName="Submit" Text="אפס סיסמא" 
                                        ValidationGroup="PasswordRecovery1" />
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
        </UserNameTemplate>
    </asp:PasswordRecovery>
</b></center>
</asp:Content>

Solution

  • You have to specify the correct From in the MailDefinition of your control. Example:

    <asp:PasswordRecovery ID="PasswordRecovery1" runat="server">
         <MailDefinition From="...@gmail.com" Subject="ResetPassword"
          Priority="High"/>
    </asp:PasswordRecovery>
    

    Either this way or from your code-behind.

    Plus you need to make sure that the EmailAddress returned by your Membership provider is a valid one.

    Also, make sure that your application is correctly configured to send email messages. Check the web.config file, section system.net -> mailSettings:

    <system.net>
        <mailSettings>
            <smtp>
                <network host="host.domain.com" userName="..." password="..." />
            </smtp>
        </mailSettings>
    </system.net>
    

    You can remove the userName and password if your SMTP server accepts unauthorized connections from your IP address.

    As a quick fix you can attach to the SendingMail event of the control (see http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.passwordrecovery.sendingmail(v=vs.110).aspx) and show a nice error message and cancel the message using:

    e.Cancel = true; // where e is the MailMessageEventHandler
    

    But if all validations are in place and all settings OK you should not have this scenario anyway.