Thursday, April 29, 2010

Visual Basic Send Email - Smtp Tutorial - {VB.net}

Hello and welcome to another quick VB.net tutorial, today in this tutorial i will be showing u all how to make a VB.net program that sends an email using SMTP client.



Codes:

Imports System.Net.Mail
____________________________________________________________________
Public Class Form1

Private Sub btn_submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_submit.Click
If TB_Name.Text = "" Or TB_Subject.Text = "" Or TB_Cmt.Text = "" Then
MsgBox("Name, Subject and Comment are required fields", vbCritical, "Error") 'This code here test if there is no text in the textbox and if there is no text, it will give an error msgbox and wont progress through the rest of the steps until the user inputs some data in all textboxes.

Else

Try
Dim Mail As New MailMessage 'This code declares a new MailMessage
Mail.From = New MailAddress("mrvbdude2@gmail") 'This is u, who is sending the message to someone else on gmail.
Mail.To.Add("mrvbdude2@gmail.com") 'This is the person u are sending the message to, u should use gmail address here..!
Mail.Subject = TB_Subject.Text & " - " & TB_Name.Text 'Subject of the MailMessage
Mail.Body = TB_Cmt.Text 'Body of the MailMessage
Dim smtp As New SmtpClient("smtp.gmail.com") 'Declares new SMTPCLIENT
smtp.Port = 587 ' SMTP port no.
smtp.EnableSsl = True
smtp.Credentials = New System.Net.NetworkCredential("YOUR GMAIL USERNAME ID HERE", "YOUR GMAIL PASSWORD HERE") 'ur gmail login information
smtp.Send(Mail) 'sends the message u created
MsgBox("Sent Successfully", vbInformation, "Thank you") 'confirmation - message has been send successfully
Catch ex As Exception 'Just incase program runs into error
MsgBox("There was an error, the program will close now!!", vbCritical, "Fatal error")
End Try
End If
End Sub

End Class
_________________________________________________________________________
Here is the video tutorial:

No comments:

Post a Comment