Click here to Skip to main content
14,981,841 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
As part of an ordering system I am making, I will have a forgot password option which allows the user to choose a new password. The program emails them a randomly generated 6 digit code which the user then has to input. However, the email is not sending. I have used code which is more or less identical to code I used in an earlier program which worked correctly.

What I have tried:

This is the code so far:
Try
                    Dim array(6) As String
                    Dim rn As New Random
                    Dim digitCode As Integer
                    Randomize()
                    For count = 1 To 6
                        'array(count) = "1"
                        array(count) = rn.Next(0, 9)
                    Next
                    digitCode = array(1) + array(2) + array(3) + array(4) + array(5) + array(6)
                    sixDigitCode = digitCode

                    Dim email As New MailMessage
                    Dim SMTP As New SmtpClient("smtp.gmail.com")
                    email.Subject = "Forgot Password"
                    email.From = New MailAddress("Ordering system prototype")
                    SMTP.Credentials = New System.Net.NetworkCredential("myemailaddress@gmail.com", "mypassword")
                    email.To.Add(EmailAddress)
                    email.Body = SixDigitCode
                    SMTP.EnableSsl = True
                    SMTP.Port = "587"
                    SMTP.Send(email)
                    TabControlForgotPassword.SelectedIndex = 2
                    MsgBox("A six digit code has been sent to" + EmailAddress)

                Catch ex As Exception
                    MsgBox("Could not send email. Contact your Adminiatrator.")
                End Try




I thought it might be due to Google updating their SMTP number? But I have checked and I can confirm that 587 is the correct number still. Is there anything I have done wrong?
Posted
Updated 28-May-18 3:08am

1 solution

Add the exception error message string to the message box output to know what went wrong:
VB
MsgBox("Could not send email." + ex.Message)

Note also that you must allow less secure applications sending mail for the used Goggle mail account (see Allow or disallow less secure apps to access accounts - G Suite Administrator Help[^]) as noted at Step 2 - Use the Gmail SMTP Server at Send email from a printer, scanner, or app - G Suite Administrator Help[^].

Other possible error sources are firewalls blocking the request.

[EDIT]
And, as known meanwhile by getting the error message, the 'From' field does not contain a valid email address:
VB
email.From = New MailAddress("Ordering system prototype")

[/EDIT]
   
v2
Comments
Member 13779854 29-May-18 18:59pm
   
Thanks for replying. It says "The specified string is not in the form required for an e-mail address".
Jochen Arndt 30-May-18 2:39am
   
The From and To must be valid email addresses. So check your 'EmailAddress' variable and change the 'From' field because your actual string is not a valid address.
Member 13779854 30-May-18 3:38am
   
I can confirm that the email address for the receiver is in the correct format. I also changed the credentials from my actual info to nobody here steals the account. The sender email address is also correctly formatted.
Jochen Arndt 30-May-18 3:40am
   
The 'From' is not a valid email address:
email.From = New MailAddress("Ordering system prototype")
Member 13779854 30-May-18 3:40am
   
Sorry, I think I misunderstood your comment. I thought you meant the SMTP credentials for some reason. I changed it and it works fine now. Thanks for the reply.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900