Sending emails through Gmail SMTP in VB.NET desktop app

I’m having trouble sending email in my VB.NET desktop application using Visual Studio 2010. When I try to use Gmail’s SMTP server, I get an error saying ‘SMTP host not found’. Here’s a simplified version of what I’m trying to do:

Dim emailSender As New SmtpClient()
emailSender.Credentials = New Net.NetworkCredential('[email protected]', 'mypass')
emailSender.Port = 587
emailSender.Host = 'smtp.gmail.com'
emailSender.EnableSsl = True

Dim message As New MailMessage()
message.From = New MailAddress('[email protected]', 'Sender Name')
message.To.Add('[email protected]')
message.Subject = 'Test Email'
message.Body = 'This is a test email sent from VB.NET'

emailSender.Send(message)

I’ve double-checked my Gmail credentials and made sure to enable less secure app access in my Google account settings. What could be causing this error? Any help would be appreciated!

I’ve dealt with similar SMTP issues in VB.NET before. One often overlooked cause is antivirus software or firewalls blocking outgoing connections. Try temporarily disabling your antivirus and see if that resolves the issue. If it does, you’ll need to add an exception for your application.

Another potential fix is to use Gmail’s app-specific passwords instead of your regular account password. Go to your Google Account settings, find ‘App passwords’ under the Security tab, and generate a new one for your VB.NET app. Replace ‘mypass’ in your code with this new password.

Lastly, make sure you’re using the latest .NET Framework version compatible with VS2010. Older versions might have trouble with modern SSL/TLS requirements. If all else fails, consider using a third-party SMTP library like MailKit, which often handles these edge cases better than the built-in classes.

I encountered a similar issue when integrating Gmail SMTP into a legacy VB.NET application. The problem might be related to your network settings or firewall. Try explicitly specifying the IP address for smtp.gmail.com instead of the domain name. You can find the current IP by running ‘nslookup smtp.gmail.com’ in Command Prompt. Also, ensure your application has outbound access on port 587. If issues persist, consider using a different email service provider or exploring OAuth 2.0 for authentication, as Google is phasing out support for less secure apps. Good luck troubleshooting!

hey there, try adding this line before sending:

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

i had a similar issue and this fixed it for me. gmail’s been updating their security stuff lately, so older apps might need this tweak. lemme know if it works!