Friday, December 21, 2012

How to send email from VB.NET

Leave a Comment

VB.NET using SMTP protocol for sending email . SMTP stands for Simple Mail Transfer Protocol . VB.NET using System.Net.Mail namespace for send mail . We can instantiate SmtpClient class and assign the Host and Port . The default port using SMTP is 25 , but it may vary different Mail Servers .In the following example shows how to send an email from a Gmail address.

Imports System.Net.Mail

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles Button1.Click

        Try

            Dim SmtpServer As New SmtpClient()

            Dim mail As New MailMessage()

            SmtpServer.Credentials = New Net.NetworkCredential("username@gmail.com", "password")

            SmtpServer.Port = 587

            SmtpServer.Host = "smtp.gmail.com"

            mail = New MailMessage()

            mail.From = New MailAddress("YOURusername@gmail.com")

            mail.To.Add("TOADDRESS")

            mail.Subject = "Test Mail"

            mail.Body = "This is for testing SMTP mail from GMAIL"

            SmtpServer.Send(mail)

            MsgBox("mail send")

        Catch ex As Exception

            MsgBox(ex.ToString)

        End Try

    End Sub

End Class

0 comments: