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

时间:2016-01-07 05:03:25

标签: c# asp.net smtp

我正在用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需要身份验证。“

1 个答案:

答案 0 :(得分:0)

检查以下内容。

  • 输入正确的登录密码。

  • 删除两步验证。

  • 您必须为谷歌启用其他时区/ IP登录 要执行此操作,请按照click here并允许访问权限 单击“继续”按钮。

  • 转到以下click here的安全设置,启用安全性较低的应用。这样您就可以从所有应用程序登录。