Hi,
I am trying to create a password recovery link to be sent to user email. I created a table that will store requests with 3 columns, which are:
Id (uniqueidentifier)
Email (nvarchar (50))
RequestDateTime (datetime)
I then created a web form where the link will be sent from using the user email address
When I tested it, I got an error on the browser as seen below
The specified string is not in the form required for an e-mail address.
using System;
using System.Collections.Generic;
using System.Data;
using System.Web.Security;
using System.Xml.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
using System.Net.Mail;
using System.Net;
public partial class PasswordRecovery : System.Web.UI.Page
{
SqlCommand cmd = new SqlCommand();
SqlDataAdapter sda = new SqlDataAdapter();
DataSet ds = new DataSet();
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnPassRec_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("SELECT * from Signup where email='"+tbEmailId.Text+"'", con);
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count != 0)
{
String myGUID = Guid.NewGuid().ToString();
var email = Convert.ToString(dt.Rows[0][0]);
SqlCommand cmd1 = new SqlCommand("insert into ForgotPassRequests values ('"+myGUID+"','"+email+"', getdate())", con);
cmd1.ExecuteNonQuery();
string ToEmailAddress = dt.Rows[0][0].ToString();
string Username= dt.Rows[0][1].ToString();
String EmailBody = "HI " + email + " <br/><br/> Click the link below to reset your password <br/><br/> http://localhost:55752/ResetPassword.aspx?email="+myGUID;
MailMessage PassRecMail = new MailMessage("youremail@gmail.com", ToEmailAddress);
PassRecMail.Body = EmailBody;
PassRecMail.IsBodyHtml = true;
PassRecMail.Subject = "Reset Password";
SmtpClient SMTP = new SmtpClient("smtp.gmail.com", 587);
SMTP.Credentials = new NetworkCredential()
{
UserName = "yourusername@gmail.com",
Password = "youGmailPassword"
};
SMTP.EnableSsl = true;
SMTP.Send(PassRecMail);
LblPassRec.Text = "Password Reset Link has been sent to Email";
LblPassRec.ForeColor = Color.Green;
}
else
{
LblPassRec.Text = "User Email DOES NOT exist !";
LblPassRec.ForeColor = Color.Red;
}
}
}