private void btnRegister_Click(object sender, EventArgs e) { Cursor.Current = Cursors.WaitCursor;//this is used to show the user that a process is occuring R_DOB = dtpDateOfBirth.Value.ToShortDateString(); R_NAME = txtName.Text; R_SNAME = txtSurname.Text; R_UNAME = txtUserName.Text; R_PWORD = txtPass.Text; R_EMAIL = txtEmail.Text; R_CURRDATE = CurrDate(R_CURRDATE); R_EMPPOS = cboEmpPosition.SelectedItem.ToString(); bool email = false; if (!R_NAME.Equals("")) { if (!R_SNAME.Equals("")) { if (!(cboEmpPosition.SelectedIndex < 0)) { if (!R_EMAIL.Equals("")) { try { var addr = new System.Net.Mail.MailAddress(R_EMAIL);// validates email address email = true; } catch { } if (email == true) { if (!R_UNAME.Equals("")) { if (!R_PWORD.Equals("")) { try { Check check = new Check(); bool executeSQL = check.CheckDB("Register", "R_UNAME", R_UNAME); if (executeSQL == false) { DBConnect objDBConnect = new DBConnect(); objDBConnect.OpenConnection(); objDBConnect.sqlCmd = new SqlCommand("INSERT INTO Register VALUES (@R_UNAME,@R_DOB, @R_NAME, @R_SNAME,@R_EMPPOS,@R_EMAIL, @R_PWORD)", objDBConnect.sqlConn); objDBConnect.sqlCmd.Parameters.AddWithValue("@R_UNAME", R_UNAME); objDBConnect.sqlCmd.Parameters.AddWithValue("@R_DOB", SqlDbType.Date).Value = dtpDateOfBirth.Value.Date; objDBConnect.sqlCmd.Parameters.AddWithValue("@R_NAME", R_NAME); objDBConnect.sqlCmd.Parameters.AddWithValue("@R_SNAME", R_SNAME); objDBConnect.sqlCmd.Parameters.AddWithValue("@R_EMPPOS", R_EMPPOS); objDBConnect.sqlCmd.Parameters.AddWithValue("@R_EMAIL", R_EMAIL); objDBConnect.sqlCmd.Parameters.AddWithValue("@R_PWORD", R_PWORD); objDBConnect.sqlDR = objDBConnect.sqlCmd.ExecuteReader(); objDBConnect.sqlDR.Close(); objDBConnect.sqlConn.Close(); string s = R_DOB + "," + R_NAME + "," + R_SNAME + "," + R_EMPPOS + "," + R_UNAME + "," + R_PWORD; MessageBox.Show("You have been successfully registered. Please be patient while an email is sent to you."); try { SmtpClient client = new SmtpClient("smtp.gmail.com"); client.Port = 587; client.EnableSsl = true; client.Timeout = 100000; client.DeliveryMethod = SmtpDeliveryMethod.Network; client.UseDefaultCredentials = false; client.Credentials = new NetworkCredential( R_MEMAIL, R_EPWORD);//logs into your email account //gets values from the textboxes MailMessage msg = new MailMessage(); msg.To.Add(txtEmail.Text); msg.From = new MailAddress(R_MEMAIL);//checks that email address exists msg.Subject = "Successful Registration - Fleet Tracking Application"; msg.Body = "Hello " + R_NAME.ToUpper() + " " + R_SNAME.ToUpper() + "\n\nThis is confirmation indicating that you have successfully registered to use the Fleet Tracking Application. \n\nDate: " + R_CURRDATE + "\nUser Name: " + R_UNAME + "\n(Use this to log into the application, along with your password)\n\nKind Regards,\nFleet Tracking Team\n(0312521212)"; client.Send(msg); MessageBox.Show("Registration Successful\nConfirmation Email Sent to: " + R_MEMAIL); Cursor.Current = Cursors.Default;// when processing is done default curser will appear } catch (Exception ex) { MessageBox.Show("Email Confirmation Not Sent:\n" + ex.Message); } } else { MessageBox.Show("That username already exists"); } } catch (SqlException ex) { MessageBox.Show(ex.Message); } catch (Exception ex) { MessageBox.Show("Error Cannot Be Registered: " + ex.Message); } } else { MessageBox.Show("Enter A Password"); } } else { MessageBox.Show("Please Enter a Username"); } } else { MessageBox.Show("Invalid Email Address: " + R_EMAIL); } } else { MessageBox.Show("Please Enter Your Email Address"); } } else { MessageBox.Show("Please Select The Employees Position"); } } else { MessageBox.Show("Please Enter Your Surname"); } } else { MessageBox.Show("Please Enter Your Name"); } }
private void btnPassReset_Click(object sender, EventArgs e) { //Code adapted from: http://www.codeproject.com/Tips/520998/Send-Email-from-Yahoo-Gmail-Hotmail-Csharp Check check = new Check(); GeneratePassword newPass = new GeneratePassword(); bool exit = false; string tempPass = newPass.GeneratePass(10); userName = txtUser.Text; exit = check.CheckEmpty(userName, "Username", exit); if (exit == false) { try { DBConnect objDBConnect = new DBConnect(); objDBConnect.OpenConnection(); objDBConnect.sqlCmd = new SqlCommand("UPDATE Register SET R_PWORD = @R_PWORD WHERE R_UNAME = @R_UNAME", objDBConnect.sqlConn); objDBConnect.sqlCmd.Parameters.AddWithValue("@R_PWORD", tempPass); objDBConnect.sqlCmd.Parameters.AddWithValue("@R_UNAME", userName); objDBConnect.sqlDR = objDBConnect.sqlCmd.ExecuteReader(); objDBConnect.sqlDR.Close(); MessageBox.Show("Successfully updated password"); objDBConnect.sqlCmd = new SqlCommand("SELECT R_EMAIL FROM Register WHERE R_UNAME = @R_UNAME", objDBConnect.sqlConn); objDBConnect.sqlCmd.Parameters.AddWithValue("@R_UNAME", userName); string email = ((string)objDBConnect.sqlCmd.ExecuteScalar()); objDBConnect.sqlConn.Close(); string smtpAddress = "smtp.gmail.com"; int portNumber = 587; bool enableSSL = true; string emailFrom = "*****@*****.**"; string password = "******"; string emailTo = email; string subject = "Password Reset"; string body = "You have requested a password reset, your new temporary password/Security Code is: " + tempPass + ". Please change your password as soon as possible."; using (MailMessage mail = new MailMessage()) { mail.From = new MailAddress(emailFrom); mail.To.Add(emailTo); mail.Subject = subject; mail.Body = body; mail.IsBodyHtml = false; using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber)) { smtp.Credentials = new NetworkCredential(emailFrom, password); smtp.EnableSsl = enableSSL; smtp.Send(mail); MessageBox.Show("An email has been sent. If you do not receive the email within 5 minutes check that the username you entered is correct"); } } this.Hide(); frmPasswordReset passReset = new frmPasswordReset(); passReset.Show(); } catch (SqlException ex) { MessageBox.Show(ex.Message); } catch (SmtpFailedRecipientException ex) { MessageBox.Show(ex.Message); } catch (SmtpException ex) { MessageBox.Show(ex.Message); } catch (Exception ex) { MessageBox.Show(ex.Message); } } }