How to send email using VB.NET code

By: Issac  

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("[email protected]", "password")
            SmtpServer.Port = 587
            SmtpServer.Host = "smtp.gmail.com"
            mail = New MailMessage()
            mail.From = New MailAddress("[email protected]")
            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.




Archived Comments

1. Juust wish to sayy your article is as astounding.

The clarity in your post is just co

View Tutorial          By: escorts barcelona at 2017-05-27 15:59:09

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

3. Very nice tutorial. Hi Rintu for attachment u can visit
http://ram-a-singh.blogspot.in/2013/0

View Tutorial          By: ras at 2013-03-14 17:19:47

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

5. i cannot add attachment
its saying
"Exceed storage allocation. The sever response

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

6. mail sent but not shown in sent items
what shoud i do???

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

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

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

9. "I get an error returned saying the SMPT server requires a secure connection - how do I do this
View Tutorial          By: Dennis at 2011-06-24 10:18:10

10. Imports System.Web
Imports System.Net.Mail
Imports System.Text
Imports System.W

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

11. I get an error returned saying the SMPT server requires a secure connection - how do I do this?
View Tutorial          By: Christian Peut at 2011-04-04 04:35:51

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

13. nicely working please check it with following URL

http://programingvb.blogspot.com

View Tutorial          By: darshana at 2010-09-28 11:11:35


Most Viewed Articles (in VB.net )

Latest Articles (in VB.net)

Comment on this tutorial