为什么我在发送电子邮件时出错

本文关键字:电子邮件 出错 为什么 | 更新日期: 2023-09-27 18:29:21

我正在c#中开发一个简单的登录页面。我正在实现忘记密码链接,同时点击链接,我需要向相应的用户发送一封带有随机数字的电子邮件。我已经添加了我在下面提到的代码

C#代码:

protected void Button1_Click(object sender, EventArgs e)
{      
    string username = string.Empty;
    string password = string.Empty;
    string constr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
        {
        using (SqlCommand cmd = new SqlCommand("SELECT Name, [Password] FROM tbl_Detailstbl WHERE Email_Id = @Email"))
        {
           cmd.Parameters.AddWithValue("@Email", txtEmail.Text.Trim());
            cmd.Connection = con;
            con.Open();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                if (sdr.Read())
                {
                    username= sdr["Name"].ToString();
                    password = sdr["Password"].ToString();
                }
            }
            con.Close();
        }
    }
    if (!string.IsNullOrEmpty(password))
    {
        MailMessage mm = new MailMessage("sender@gmail.com", txtEmail.Text.Trim());
        mm.Subject = "Password Recovery";
        mm.Body = string.Format("Hi {0},<br /><br />Your password is {1}.<br /><br />Thank You.", username, password);
        mm.IsBodyHtml = true;
       SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.EnableSsl = true;
        smtp.UseDefaultCredentials = false;
        smtp.Credentials = new NetworkCredential("sender@gmail.com","asfsdfg");
  
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = NetworkCred;
        smtp.Port = 587;
        smtp.Send(mm);
        lblMessage.ForeColor = Color.Green;
        lblMessage.Text = "Password has been sent to your email address.";
    }
    else
    {
        lblMessage.ForeColor = Color.Red;
        lblMessage.Text = "This email address does not match our records.";
    }
}

HTML代码:

<form id="form2" runat="server">
    <div>
    <fieldset style="width:380px;">
    <legend>Recover Password By Email Or User Name</legend>
    <table>
    <tr>
    <td>UserName : </td><td>
        <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
    <td colspan="2">&nbsp;&nbsp;OR</td>
    </tr>
    <tr>`enter code here`
    <td>Email Id : </td><td>
        <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox><br />
       <asp:RegularExpressionValidator ID="revEmailId" runat="server"
                                ErrorMessage="Please enter valid email address"
                                ControlToValidate="txtEmail" Display="Dynamic"
                                ForeColor="Red" SetFocusOnError="True"
                                ValidationExpression="'w+([-+.']'w+)*@'w+([-.]'w+)*'.'w+([-.]'w+)*"></asp:RegularExpressionValidator>
        </td>
    </tr>
    <tr>
    <td>&nbsp;</td><td>
        <asp:Button ID="Button1" runat="server" Text="Send"  onclick="Button1_Click"/></td>
    </tr>
    <tr>
    <td colspan="2">
        <asp:Label ID="lblMessage" runat="server" Text="" OnClick="Button1_Click"></asp:Label>
        </td>
    </tr>
    </table>
    </fieldset>
   
    </div>
    </form>

我的问题是,当我试图发送电子邮件时,我收到了一个错误:

"System.dll中发生类型为"System.Net.Mail.SmtpException"的异常,但未在用户代码中处理

附加信息:SMTP服务器需要安全连接,或者客户端未通过身份验证。服务器响应为:5.5.1需要身份验证"

为什么我在发送电子邮件时出错

检查以下内容。

  • 输入正确的登录密码。

  • 删除两步验证。

  • 你必须为你的谷歌启用从其他时区/ip登录账户要执行此操作,请单击此处并允许访问单击"继续"按钮。

  • 转到下面的安全设置,单击此处并启用不太安全的应用程序。这样你就可以从所有应用程序登录了。