Reputation: 21
Aim: send mail with random number if user need to reset or forgot password
Till now what i have done is
View
<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>
Source code
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("[email protected]", 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 = "[email protected]";
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.";
}
}
An exception of type 'System.Net.Mail.SmtpException' occurred in System.dll but was not handled in user code Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
Please give me a fine solution for this error
Upvotes: 0
Views: 5354
Reputation: 1
Goto https://google.com/settings/security/lesssecureapps
And Turn On "Allow less secure apps: ON"
Upvotes: -1
Reputation: 5890
My code works, but I don't use default credentials.
Here's the complete code:
var addressFrom = new MailAddress("[email protected]");
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);
Upvotes: 2
Reputation: 18127
There is security setting which you should turn off in your gmail account(for the account to which you send the mail).
Check this LINK
Upvotes: 2