How to send email using VB.NET code

By: Issac Viewed: 153455 times  Printer Friendly Format    


Surprised, we can use SMTP protocol and .net source code for sending an email, SMTP stands for Simple Mail Transfer Protocol, in VB.NET we can use 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. The following example shows how to send emails from a Gmail address.

The Gmail SMTP server name is smtp.gmail.com and the port using send mail is 587. Here using NetworkCredential for password based authentication.

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

You have to provide the informations like your gmail username and password , and the to address also.



Most Viewed Articles (in VB.net )

Latest Articles (in VB.net)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. nicely working please check it with following URL<
View Tutorial          By: darshana at 2010-09-28 11:11:35

2. It won't work for me ;S
View Tutorial          By: jordi at 2011-01-21 08:22:45

3. I get an error returned saying the SMPT server req
View Tutorial          By: Christian Peut at 2011-04-04 04:35:51

4. Imports System.Web
Imports System.Net.Mail<

View Tutorial          By: Naresh at 2011-05-24 07:27:12

5. "I get an error returned saying the SMPT serv
View Tutorial          By: Dennis at 2011-06-24 10:18:10

6. Thanks a lot
View Tutorial          By: praveen at 2011-10-01 09:38:02

7. how about if you attach microsoft word documents?p
View Tutorial          By: gian at 2012-01-06 03:11:44

8. mail sent but not shown in sent items
what

View Tutorial          By: Hafiz Hussaim at 2012-04-14 06:55:03

9. i cannot add attachment
its saying
&

View Tutorial          By: Rintu Das at 2012-05-04 18:07:48

10. set the property of SMTP.EnableSsl = True
View Tutorial          By: Kowsalya at 2012-06-03 10:47:58

11. Very nice tutorial. Hi Rintu for attachment u can
View Tutorial          By: ras at 2013-03-14 17:19:47

12. Keep writing. Very helpful information provided. T
View Tutorial          By: send email vb.net at 2014-08-27 09:32:03

13. Juust wish to sayy your article is as astounding.<
View Tutorial          By: escorts barcelona at 2017-05-27 15:59:09