Sending HTML Emails Using Gmail Account in Asp.Net - VB Code

Web Design Web Hosting

Sending HTML Emails Using Gmail Account in Asp.Net - VB Code

Published: 10/03/2009 by Vinoth Kannan S

This is the sample asp.net code to send html emails via asp.net using gmail accounts.

Dim client As New SmtpClient
client.DeliveryMethod = SmtpDeliveryMethod.Network
client.EnableSsl = True
client.Host = "smtp.gmail.com"
client.Port = 587

' setup Smtp authentication
Dim credentials As System.Net.NetworkCredential = New System.Net.NetworkCredential("example@gmail.com", "PASSWORD")
client.UseDefaultCredentials = False
client.Credentials = credentials

Dim msg As New MailMessage
msg.From = New MailAddress("example@gmail.com")
msg.To.Add(New MailAddress(TextBoxToAddress.Text))

msg.Subject = TextBoxSubject.Text
msg.IsBodyHtml = True
msg.Body = String.Format(TextEditor.Content)

Try
client.Send(msg)
lblmsg.Text = "Your message has been successfully sent."
Catch ex As Exception
lblmsg.ForeColor = Drawing.Color.Red
lblmsg.Text = "Error occured while sending your message." + ex.Message
End Try

Photo Gallery