Monday, July 14, 2008

Send Email from your Gmail Account

You can use your gmail account to send emails from VB.NET using SMTP. You need to set SSL credentials. You have to use “” as host and “587” as Port to send email. Following function can send email.

Private Sub SendEmail()
Dim fromAddress As String = "fromAddress@domain"
Dim toAddress As String = "toAddress@domain"
Dim message As New Net.Mail.MailMessage(fromAddress, toAddress)

message.Subject = "Subject of the email"
message.Body = "<b>Hello World</b>"  'Will show Hello World in bold letters

'Set True if you your body contains Html
message.IsBodyHtml = True

'Set Priority of the Email
message.Priority = Net.Mail.MailPriority.High

'For gmail host would be "" and port would be 587
Dim client As New Net.Mail.SmtpClient("", 587)

'Set EnableSsl = True as gmail requires SSL
client.EnableSsl = True

'Credentials for the sender
client.Credentials = New Net.NetworkCredential(fromAddress, "Sender gmail password")

'Send the Email

Catch ex As Exception
End Try
End Sub

No comments:

Post a Comment