Search

Wednesday, November 16, 2011

VB.NET Send Mail Code

Following coding used to send mail from VB.NET using GMAIL Accounts.

  Imports System.Net.Mail

  Dim SmtpServer As New SmtpClient()
        Dim mail As New MailMessage()
        Dim sMsg1, sFooter As String
        Dim sTo, sMailuser, sPassword As String
       
        Try
            sMailuser = "rezbait@gmail.com"
            sPassword = "Account Password" 'Password of mail.
           
sMsg1 = " This is test mail from GMAIL account. "
            sFooter = "<BR><BR>This is footer."
            sTo = "rezbactg@yahoo.com"

            SmtpServer.Port = 587
            SmtpServer.Host = "smtp.gmail.com"
            SmtpServer.EnableSsl = True
           
SmtpServer.Credentials = New  _
            Net.NetworkCredential(sMailuser, sPassword)

            mail = New MailMessage()
            mail.From = New MailAddress(sMailuser)   'From Address
            mail.To.Add(sTo)  ' To Address
            mail.Subject = "Subject of Mail " 'Subject of Mail.
            mail.Body = sMsg1 + sFooter
            mail.IsBodyHtml = True
            SmtpServer.Send(mail)
            MsgBox("Mail Sent.")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

1 comment:

Anonymous said...

I already test this code. Its work fine. Thanks for the post.