1

在 ASP.Net 中 - 我正在使用内置的密码恢复向导。

是否有任何方法可以将 SMTP 身份验证添加到邮件身份验证向导 - 而无需将其添加到 web.config?或者可以在代码隐藏的某个地方注入它吗?

我的 aspx 代码如下 - 目前没有代码隐藏:

       <asp:PasswordRecovery ID="PasswordRecovery1" runat="server" borderStyle="None" 
        borderWidth="1px"  Font-Size="10pt" Font-Names="Verdana" onsendingmail="PasswordRecovery1_SendingMail">
    <TitleTextStyle BackColor="#6B696B" Font-Bold="True" ForeColor="#FFFFFF" />
    <MailDefinition From="info@mydomain.co.uk" Priority="High" 
        Subject="My Domain - new, temporary password" BodyFileName="forgotpassword.txt">
    </MailDefinition>
    <UserNameTemplate>
        <table border="0" cellpadding="1" cellspacing="0" 
            style="border-collapse:collapse;">
            <tr>
                <td>
                    <table border="0" cellpadding="0">

                        <tr>
                            <td align="center" colspan="2" style="font-family:Arial;font-size:Small;">
                                Enter your User Name to receive your password.</td>
                        </tr>
                        <tr>
                            <td align="right" style="font-family:Arial;font-size:Small;">
                                <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label>
                            </td>
                            <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" colspan="2" style="color:Red;">
                                <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
                            </td>
                        </tr>
                        <tr>
                            <td align="right" colspan="2">
                                <asp:Button ID="SubmitButton" runat="server" CommandName="Submit" Text="Submit" 
                                    ValidationGroup="PasswordRecovery1" />
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </UserNameTemplate>
</asp:PasswordRecovery>

目前我刚刚收到消息:

System.Net.Mail.SmtpFailedRecipientException 邮箱不可用。服务器响应是:必须经过身份验证

4

2 回答 2

1

该错误意味着您的 SMTP 服务器要求您的应用程序在通过它发送电子邮件之前对其进行身份验证。

您可能正在使用SmtpClient引擎盖下。.NET SMTP 客户端的默认值在您的应用程序的配置文件中定义,在本例中是您的 web.config。唯一的其他地方是如果您手动管理,SmtpClient因为您处理 PasswordRecovery 控件的事件。

PasswordRecovery不公开任何属性来控制底层 SMTP 客户端。

请注意,我强烈建议不要使用WebControls它们,因为使用它们意味着您放弃了对应用程序的大量控制:逻辑和演示。我还要注意,您提供的标记是无效的,因为它使用了表示属性,并且<table>强烈建议不要使用 for layout。

于 2013-03-30T11:05:50.933 回答
1
protected void Button1_Click(object sender, EventArgs e)
    {
            try
            {
            DataSet ds = new DataSet();
            using (SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\database\personal.mdf;Integrated Security=True;Connect Timeout=60;User Instance=True"))
            {
            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT UserName,Password FROM Logintable Where emailid= '" + txtEmail.Text.Trim() + "'", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            con.Close();
            }
            if(ds.Tables[0].Rows.Count>0)
            {



                            System.Net.Mail.MailMessage Msg = new System.Net.Mail.MailMessage();
                            // Sender e-mail address.
                            Msg.From = new MailAddress(txtEmail.Text);
                            // Recipient e-mail address.
                            Msg.To.Add(txtEmail.Text);
                            Msg.Subject = "Your Password Details";
                            Msg.Body = "Hi, <br/>Please check your Login Detailss<br/><br/>Your Username: " + ds.Tables[0].Rows[0]["UserName"] + "<br/><br/>Your Password: " + ds.Tables[0].Rows[0]["Password"] + "<br/><br/>";
                            Msg.IsBodyHtml = true;
                            // your remote SMTP server IP.
                            SmtpClient smtp = new SmtpClient();
                            smtp.Host = "smtp.gmail.com";
                            smtp.Port = 587;
                            smtp.Credentials = new System.Net.NetworkCredential("youremail@gmail.com", "N@@@@@@@@");
                            smtp.EnableSsl = true;
                            smtp.Send(Msg);

                            //Msg = null;
                            Label3.Text = "Your Password Details Sent to your mail";
                            // Clear the textbox valuess
                            txtEmail.Text = "";
                    }
                        else
                        {
                              lbltxt.Text = "The Email you entered not exists.";
                        }
                        }
                     catch (Exception ex)
                    {
                        Label2.Text = "Error Occured: " + ex.Message.ToString();

                    }
            }
       }
于 2015-01-07T05:49:58.303 回答