Results 1 to 5 of 5

Thread: Sending EMail - SMTP secure connection Error

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2016
    Posts
    69

    Sending EMail - SMTP secure connection Error

    Hi everyone.

    I followed an Email tutorial on this link:

    https://www.tutorialspoint.com/vb.ne...send_email.htm

    The purpose for sending email is when user wants to reset password. A new password is sent to their email.

    Code:
    Private Sub FgtPW_LinkLabel_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles FgtPW_LinkLabel.LinkClicked
            If MessageBox.Show("Are you sure you want to reset password?" & vbNewLine & vbNewLine &
                   "A new password will be sent to the following email:" & vbNewLine & vbNewLine &
                   vbTab & My.Settings.Email, "Forgot Password",
             MessageBoxButtons.YesNo, MessageBoxIcon.Question) _
             = DialogResult.Yes Then
    
                Try
                    Dim SmtpServer As New SmtpClient()
                    Dim mail As New MailMessage()
                    SmtpServer.UseDefaultCredentials = False
                    SmtpServer.Credentials = New Net.NetworkCredential(My.Settings.Email.ToString, "password")
                    SmtpServer.Port = 587
                    SmtpServer.EnableSsl = True
                    SmtpServer.Host = "smtp.gmail.com"
                    mail = New MailMessage()
                    mail.From = New MailAddress("[email protected]")
                    mail.To.Add(My.Settings.Email.ToString)
                    mail.Subject = "EVC Kiosk New Generated Password"
                    mail.Body = "This is for testing SMTP mail from GMAIL"
                    SmtpServer.Send(mail)
                    MsgBox("mail send")
                Catch ex As Exception
                    MsgBox(ex.ToString)
                End Try
    
            End If
        End Sub
    1st Question:

    I was flashed with the error upon testing. Something about SMTP server requires a secure connection or the client was not authenticated.

    Name:  emailError.jpg
Views: 564
Size:  45.4 KB

    Can anybody tell me what this means?



    2nd Question:

    Code:
    SmtpServer.Credentials = New Net.NetworkCredential(My.Settings.Email.ToString, "password")
    Do I really need an email password here? I mean there is no way I will know the email password the user will have. When we send email to others, we are not asked for their password right? Also, what if the user changes email with different password.
    I don't understand the reason behind this line of code. Can anyone enlighten me?

  2. #2
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    Re: Sending EMail - SMTP secure connection Error

    The network credentials are the username and password needed to authenticate with the SMTP mail server in order for it to accept the request to relay the email. With GMAIL, you will need to specify a valid gmail address as the username and its associated login password. Does My.Settings.Email.ToString represent a valid gmail email address and is its login password "password"?

  3. #3
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    Re: Sending EMail - SMTP secure connection Error

    You cannot rely on the user having a gmail address and like you say, if the user changes their gmail password, emails will stop sending. You will need to hard code a gmail address and password that you have created or provide a settings console where the user can enter their own SMTP credentials.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2016
    Posts
    69

    Re: Sending EMail - SMTP secure connection Error

    I did a bit of googling. The code works, but It seems that the error was because the Gmail Account Setting has blocked unsecure apps.

    One way I can think of is create a new dummy gmail account and turn its Secure Apps settings to OFF. Its only purpose is for authentication

    Code:
    SmtpServer.Credentials = New Net.NetworkCredential("dummy gmail", "dummy password")

    Then now you can send the message to whichever email (Need not be gmail) you want.

    Code:
    mail.To.Add(My.Settings.Email.ToString)
    Tested and it works.

  5. #5
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    Re: Sending EMail - SMTP secure connection Error

    That's what I'd suggest if you want a portable solution that's not going to require any user configuration. If you are going to be generating high volumes of emails, consider that Gmail's spam filter might start blocking them.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width