Imports System.Net
Imports System.Net.Mail
Imports System.Drawing
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Data

Partial Class login_ForgotPW
    Inherits System.Web.UI.Page

    Protected Sub btn_Back_Click(sender As Object, e As EventArgs)
        Response.Redirect("login/login_FLEX.aspx")
    End Sub

    Protected Sub SendEmail(sender As Object, e As EventArgs)
        Dim username As String = txt_Username.Text
        Dim password As String = String.Empty
        Dim email As String = txtEmail.Text

        ' cDBFunctions.GetNewOpenConnection()
        Dim ConnectionString = ""
        If HttpContext.Current.Request.ServerVariables("SERVER_NAME") = "localhost" Then
            'ConnectionString = "Server=DEVELOPER\DEVSQL;Database=VERAG_HOMEPAGE;Uid=sa;Pwd=BmWr501956"
            ConnectionString = "Server=DEVELOPER\DEVSQL;Database=VERAG_HOMEPAGE;Uid=AppUser;Pwd=yp/THDd?xM+pZ$;"
        Else
            ConnectionString = "Server=DEVELOPER.verag.ost.dmn\DEVSQL;Database=VERAG_HOMEPAGE;Uid=AppUser;Pwd=yp/THDd?xM+pZ$;"
            'ConnectionString = "Server=db593295684.db.1and1.com;Database=db593295684;Uid=dbo593295684;Pwd=atilla#2;"
        End If

        ' Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As New SqlConnection(ConnectionString)
            ' Using cmd As New SqlCommand("Validate_User")
            Using cmd As New SqlCommand("SELECT COUNT(*) FROM [VERAG_HOMEPAGE].[dbo].[Users] WHERE Username=@Username")
                ' cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.AddWithValue("@Username", username)
                cmd.Connection = con
                con.Open()
                'userId = Convert.ToInt32(cmd.ExecuteScalar())
                Dim dr As SqlDataReader = cmd.ExecuteReader()
                If dr.Read() Then
                    username = dr("@Username").ToString()
                End If
            End Using
            con.Close()
        End Using

        If Not String.IsNullOrEmpty(username) Then
            password = RandomString(New Random, 10)
            'SendEmail(username, password, email)
            MsgBox("Mail would be sent successfully!" + Environment.NewLine + "new PW: \t" + password)
            lblMessage.ForeColor = Color.Green
            lblMessage.Text = "Passwort wurde erfolgreich an die angegebene E-Mail Adresse gesendet."
        Else
            MsgBox("Mail would not be sent successfully!")
            lblMessage.ForeColor = Color.Red
            lblMessage.Text = "Diese E-Mail ist nicht in unserer Datenbank vorhanden."
        End If
    End Sub

    Function RandomString(r As Random, max As Integer) As String
        Dim s As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!§$%&/?="
        Dim sb As New StringBuilder
        Dim cnt As Integer = r.Next(4, max)
        For i As Integer = 1 To cnt
            Dim idx As Integer = r.Next(0, s.Length)
            sb.Append(s.Substring(idx, 1))
        Next
        Return sb.ToString()
    End Function

    Function SendEmail(username As String, password As String, email As String) As Boolean
        Dim Msg As New MailMessage
        Dim myCredentials As New System.Net.NetworkCredential
        myCredentials.UserName = "al@verag.ag"
        myCredentials.Password = "Luxandreas#2"
        Msg.IsBodyHtml = False
        Dim mySmtpsvr As New SmtpClient()
        mySmtpsvr.Host = "smtp.1und1.de" 'bei web.de
        mySmtpsvr.Port = 587 '25
        mySmtpsvr.UseDefaultCredentials = False
        mySmtpsvr.Credentials = myCredentials
        Try
            Msg.From = New MailAddress("al@verag.ag")
            Msg.To.Add(email)
            Msg.Subject = "TEST"
            Msg.Body = String.Format("Hi {0},<br /><br />Your password is {1}.<br /><br />Thank You.", username, password)

            '     Dim attachment As Attachment = New Attachment(File.OpenRead(excel), "Kundenliste.xlsx")
            '     Msg.Attachments.Add(attachment)
            '     mySmtpsvr.Send(Msg)
            MsgBox("SENT")
            Return True
        Catch ex As Exception
            MsgBox(Err.Number & ex.Message & ex.StackTrace.ToString) 'Falls ein Fehler auftritt wird eine MsgBox angezeigt
        End Try
        Return False
    End Function


End Class
