r/visualbasic • u/JohnDeere6930Premium VB.Net Advanced • Feb 07 '21
Need help with an e-mail client
Code
Try
Dim server As New SmtpClient
Dim mail As New MailMessage
server.UseDefaultCredentials = False
server.Credentials = New
Net.NetworkCredential("[email protected]", "")' my trash email adress
server.Port = 587
server.EnableSsl = True
server.Host = "smtp.gmail.com"
mail = New MailMessage()
mail.From = New MailAddress(from.Text)
mail.To.Add(to1.Text)
mail.Subject = subject.Text
mail.IsBodyHtml = False
mail.Body = message.Text
If CheckBox1.Checked = True Then
mail.CC.Add(cc.Text)
End If
server.Send(mail)
MsgBox("Mail sent")
Catch ex As Exception
MsgBox("Error while sending mail")
End TryWhen i try to send mail to another GMAIL account, its shows the error at the bottom of the code (Error while sending mail)
How do i fix it?
2
u/thalrok Feb 07 '21
To get a little more details on the error:
MsgBox(ex.message)
(within the catch block)
2
u/andrewsmd87 Web Specialist Feb 07 '21
Have you checked you Gmail to see if you got a notification about it. Usually when you use an app you specify different credentials in your Google account, basically authorizing the app to use your Gmail.
I'm not in a pc to test but use Gmail as a backup basically to notify me if our smtp server goes down and your code looks fine to me
Also put a breakpoint on your msgbox in your catch and inspect what ex is. It'll tell you the exact error
3
u/TCBW Feb 07 '21
I would check a) local firewall and b) make sure you run visual studio as admin.
When I did this I seem to remember needing to set up an application password. SMTP doesn't allow for 2fa, etc.