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 “smtp.gmail.com” as host and “587” as Port to send email. Following function can send email.

CODE:
Private Sub SendEmail()
Try
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 "smtp.gmail.com" and port would be 587
Dim client As New Net.Mail.SmtpClient("smtp.gmail.com", 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
client.Send(message)

Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

No comments:

Post a Comment