.Net, Programming, Tips & Tricks

Send Mail Using .Net with Gmail or Ymail Account

Here is a small code for sending an email from ASP.NET 2.0 page, using the namespace System.net.mail and a Gmail account.

Imports System.Net.Mail

Imports System.Net.Mail.MailMessage

Imports System.Net.NetworkCredential

Dim mail As New MailMessage()

Dim msgBody As String

Dim smtp As New SmtpClient

mail.From = New MailAddress(“ur - gmail - account@ gmail.com“,
"display name")

mail.To.Add(“ur - email@ host.com“)
mail.Subject = “Subject“

mail.Body = “msgBody“

mail.IsBodyHtml = True ' This is to enable HTML in your email body

mail.ReplyTo = New MailAddress(“reply - to - email - address“)
' This is optional, it allows you to add Reply To email address.

smtp.Host = "smtp.gmail.com"

smtp.Port = 25

smtp.EnableSsl = True

smtp.Credentials = New System.Net.NetworkCredential(“ur - gmail -
account@ gmail.com“, "gmail-password")

smtp.Send(mail)

lblFlag.Text = "Your Message has beent sent."

Note: We can build this email form AJAX based, by putting the content in an UpdatePanel control, and add the button “send” as a trigger.

Leave a Reply

Your email address will not be published. Required fields are marked *