[Solved] Email not sending using VB.Net in ASP.Net

phonghue
 
on Oct 26, 2021 10:57 PM
492 Views

hello,

last time you sent me the step by step how to grap the username from the server and send it to the email.

i had made some change to fit in with my department and the server, but then when i click the button, then it's said send sucessfully, but i don't recieve anything.

i will show you two version

this is your version from last time.

Protected Sub SendEmail(ByVal sender As Object, ByVal e As EventArgs)
    Dim username As String = String.Empty
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(constr)
        Using cmd As SqlCommand = New SqlCommand("SELECT Username FROM Users WHERE Email = @Email")
            cmd.Parameters.AddWithValue("@Email", txtEmail.Text.Trim())
            cmd.Connection = con
            con.Open()
            Using sdr As SqlDataReader = cmd.ExecuteReader()
                If sdr.Read() Then
                    username = sdr("Username").ToString()
                End If
            End Using
            con.Close()
        End Using
    End Using
 
    If Not String.IsNullOrEmpty(username) Then
        Dim mm As MailMessage = New MailMessage("sender@gmail.com", txtEmail.Text.Trim())
        mm.Subject = "Username Recovery"
        mm.Body = String.Format("Your Username is {0}.<br /><br />Thank You.", username)
        mm.IsBodyHtml = True
        Dim smtp As SmtpClient = New SmtpClient()
        smtp.Host = "smtp.gmail.com"
        smtp.EnableSsl = True
        Dim NetworkCred As NetworkCredential = New NetworkCredential()
        NetworkCred.UserName = "sender@gmail.com"
        NetworkCred.Password = "<Password>"
        smtp.UseDefaultCredentials = True
        smtp.Credentials = NetworkCred
        smtp.Port = 587
        lblMessage.ForeColor = Color.Green
        lblMessage.Text = "Username has been sent to your email address."
    Else
        lblMessage.ForeColor = Color.Red
        lblMessage.Text = "This email address does not match our records."
    End If
End Sub

this is my version:  please help me to rewrite it if you see any error because i did not recieve any email after i click the button.

Protected Sub SendEmail(ByVal sender As Object, ByVal e As EventArgs)
    Dim username As String = String.Empty
    Dim constr As String = ConfigurationManager.ConnectionStrings("DBHUSRConnectionString").ConnectionString
    Using con As SqlConnection = New SqlConnection(constr)
        Using cmd As SqlCommand = New SqlCommand("SELECT aspnet_Users.UserName, aspnet_Membership.Email FROM aspnet_Users INNER JOIN aspnet_Membership ON aspnet_Users.UserId = aspnet_Membership.UserId WHERE aspnet_Membership.Email = @Email")
 
            cmd.Parameters.AddWithValue("@Email", txtEmail.Text.Trim())
            cmd.Connection = con
            con.Open()
            Using sdr As SqlDataReader = cmd.ExecuteReader()
                If sdr.Read() Then
                    username = sdr("Username").ToString()
                End If
            End Using
            con.Close()
        End Using
    End Using
 
    If Not String.IsNullOrEmpty(username) Then
        Dim mm As MailMessage = New MailMessage("sender@fullerton.edu", txtEmail.Text.Trim())
        Dim toAddress As String = txtEmail.Text
 
        mm.Subject = "Username Recovery"
        mm.Body = String.Format("Your Username is {0}.<br /><br />Thank You.", username)
        mm.IsBodyHtml = True
 
        Dim smtp As SmtpClient = New SmtpClient()
        smtp.Host = "smtp.fullerton.edu"
        smtp.EnableSsl = True
        Dim NetworkCred As NetworkCredential = New NetworkCredential()
        NetworkCred.UserName = "sender@fullerton.edu"
        NetworkCred.Password = "<Password>"
        smtp.UseDefaultCredentials = True
        smtp.Credentials = NetworkCred
        smtp.Port = 587
        lblMessage.ForeColor = Color.Green
        lblMessage.Text = "Username has been sent to your email address."
    Else
        lblMessage.ForeColor = Color.Red
        lblMessage.Text = "This email address does not match our records."
    End If
End Sub

thanks

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Oct 27, 2021 02:36 AM

Hi phonghue,

You are not calling the smtp.Send method.

Refer the modified code.

Protected Sub SendEmail(ByVal sender As Object, ByVal e As EventArgs)
    Dim username As String = String.Empty
    Dim constr As String = ConfigurationManager.ConnectionStrings("DBHUSRConnectionString").ConnectionString
    Using con As SqlConnection = New SqlConnection(constr)
        Using cmd As SqlCommand = New SqlCommand("SELECT aspnet_Users.UserName, aspnet_Membership.Email FROM aspnet_Users INNER JOIN aspnet_Membership ON aspnet_Users.UserId = aspnet_Membership.UserId WHERE aspnet_Membership.Email = @Email")
            cmd.Parameters.AddWithValue("@Email", txtEmail.Text.Trim())
            cmd.Connection = con
            con.Open()
            Using sdr As SqlDataReader = cmd.ExecuteReader()
                If sdr.Read() Then
                    username = sdr("Username").ToString()
                End If
            End Using
            con.Close()
        End Using
    End Using

    If Not String.IsNullOrEmpty(username) Then
        Dim mm As MailMessage = New MailMessage("sender@fullerton.edu", txtEmail.Text.Trim())
        Dim toAddress As String = txtEmail.Text
        mm.Subject = "Username Recovery"
        mm.Body = String.Format("Your Username is {0}.<br /><br />Thank You.", username)
        mm.IsBodyHtml = True
        Dim smtp As SmtpClient = New SmtpClient()
        smtp.Host = "smtp.fullerton.edu"
        smtp.EnableSsl = True
        Dim NetworkCred As NetworkCredential = New NetworkCredential()
        NetworkCred.UserName = "sender@fullerton.edu"
        NetworkCred.Password = "<Password>"
        smtp.UseDefaultCredentials = True
        smtp.Credentials = NetworkCred
        smtp.Port = 587
        smtp.Send(mm)
        lblMessage.ForeColor = Color.Green
        lblMessage.Text = "Username has been sent to your email address."
    Else
        lblMessage.ForeColor = Color.Red
        lblMessage.Text = "This email address does not match our records."
    End If
End Sub