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
            sMailuser = ""
            sPassword = "Account Password" 'Password of mail.
sMsg1 = " This is test mail from GMAIL account. "
            sFooter = "<BR><BR>This is footer."
            sTo = ""

            SmtpServer.Port = 587
            SmtpServer.Host = ""
            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
            MsgBox("Mail Sent.")
        Catch ex As Exception
        End Try

1 comment:

Anonymous said...

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