使用Gmail发送邮件 - 例外情况“SMTP服务器需要安全连接”

时间:2016-01-07 11:52:11

标签: c# asp.net .net authentication gmail

目标:如果用户需要重置或忘记密码,请使用随机数发送邮件

直到现在我所做的是

查看

    <form id="form1" runat="server">
Email Address:
<asp:TextBox ID="txtEmail" runat="server" Width="250" />
<br />
<asp:Label ID="lblMessage" runat="server" />
<br />
<asp:Button Text="Send" runat="server" OnClick="Button1_Click" />
</form>

源代码

    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("xxxxxxxxx@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;
                    NetworkCredential NetworkCred = new NetworkCredential();
                    NetworkCred.UserName = "xxxxxx@gmail.com";
                    NetworkCred.Password = "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.";
                }


   }
  

发生了'System.Net.Mail.SmtpException'类型的异常   System.dll但未在用户代码中处理附加信息:   SMTP服务器需要安全连接,否则客户端不需要   认证。服务器响应为:5.5.1需要身份验证。

请为此错误提供一个很好的解决方案

3 个答案:

答案 0 :(得分:2)

您应该在gmail帐户中关闭安全设置(对于您向其发送邮件的帐户)。

选中此LINK

答案 1 :(得分:2)

我的代码有效,但我不使用默认凭据。

这是完整的代码:

var addressFrom = new MailAddress("joe.doe@gmail.com");
var smtp = new SmtpClient
        {
            Host = "smtp.gmail.com",
            Port = 587,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials =
                new NetworkCredential(addressFrom.Address, "mypassword")
        };

var message = new MailMessage
        {
            Subject = _sSubject,
            Body = _sMessage,
            From = addressFrom
        };

var sTo = _sRecepients; // comma separated

message.To.Add(sTo);
smtp.Send(message);

答案 2 :(得分:-1)

转到 https://google.com/settings/security/lesssecureapps

启用&#34;允许安全性较低的应用:ON&#34;