Search
Close this search box.

Sending .NET EMail Via smtp.gmail.com

I just finished wrapping up an article on how to send email from within a Windows or Web application with the System.Net.Mail namespace. One of the things I uncovered is that you can send email through the gmail smtp server. Of course you need to have a GMail account first…

Imports System.Net.Mail

'Start by creating a mail message object
Dim MyMailMessage As New MailMessage()

'From requires an instance of the MailAddress type
MyMailMessage.From = New MailAddress("from_address_here@gmail.com")

'To is a collection of MailAddress types
MyMailMessage.To.Add("to_address_here@domain_name_hereyMailMessage.Subject = "GMail Test"
MyMailMessage.Body = "This is the test text for Gmail email"

'Create the SMTPClient object and specify the SMTP GMail server
Dim SMTPServer As New SmtpClient("smtp.gmail.com")
SMTPServer.Port = 587
SMTPServer.Credentials = New System.Net.NetworkCredential("account, " account pwd")
SMTPServer.EnableSsl = True

Try
   SMTPServer.Send(MyMailMessage)
   MessageBox.Show("Email")
Catch ex As SmtpException
   MessageBox.Show(ex.Message)
End Try

Just thought I’d pass that little tidbit on.

Have a day. 😐

This article is part of the GWB Archives. Original Author: Jim Duffy

Related Posts