I'm making a User Registration System (In the login section there is another part called (forgot password) So when user click on forgot password (he/she should provide a gmail account in which OTP will be send but I'm getting the error (Failure sending mail) ))
private void button2_Click(object sender, EventArgs e) { string from, pass, msgbody;
Random rand = new Random();
randomCode = (rand.Next(999999)).ToString();
MailMessage mm = new MailMessage();
to = (textBox1.Text).ToString();
from = "**************"; //here it is the email of the sender
pass = "**********";// password of the sender
msgbody = "Reset Code "+ randomCode;
mm.To.Add(to);
mm.From = new MailAddress(from);
mm.Body = msgbody;
mm.Subject = "Password reset code";
SmtpClient smtp = new SmtpClient("smtp.gmail.com");
smtp.EnableSsl = true;
smtp.Port = 587;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(from,pass);
try
{
smtp.Send(mm);
MessageBox.Show("Code sent successfully");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
private void button1_Click(object sender, EventArgs e)
{
if(randomCode == (textBox2.Text).ToString())
{
to = textBox1.Text;
Form9 f9 = new Form9();
f9.Show();
this.Hide();
}
else
{
MessageBox.Show("Wrong code","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}