Send Email Using Visual Basic.Net

In this tutorial, I’m going to show you how create an application on how to send email messages using Visual Basic.Net. To start with this application, create a new project called “sendEmail”. Then design the form that looks like as shown below. e2 Next, double click the form and add the following code above the public class Form1.
  1. Imports System.Net.Mail
The added code above is a namespace that contains classes used to send electronic mail to a Simple Mail Transfer Protocol(SMTP) server for delivery. After this, double click the button and add the following code.
  1. Dim mail As New MailMessage
  2. 'this area for our Sender
  3. mail.From = New MailAddress(txtfrom.Text)
  4. 'this area for our Recipient
  5. mail.To.Add(txtto.Text)
  6. 'this would the email subject
  7. mail.Subject = txtsubj.Text
  8. 'this for the body of our email
  9. mail.Body = txtbody.Text
The code above has a MailMessage class, this class represents the content of a mail message. Under these codes, we will be dealing with another class called SmtpClient that will used to transmit email to SMTP host that you designate for email delivery. And here’s the following codes.
  1. Dim smtp As New SmtpClient("smtp.gmail.com")
  2. 'set the EnableSsl to true
  3. smtp.EnableSsl = True
  4. 'this area is for your login username and password
  5. smtp.Credentials = New System.Net.NetworkCredential("yourUsername", "yourPassword")
  6. 'Gets the port used for SMTP Transactions
  7. smtp.Port = "587"
  8. 'finally it sends the message to SMTP server for delivery
  9. smtp.Send(mail)
At this time, we can test our application by pressing the “F5” or Start button. Then it should look like as shown below. e1 Then after you fill up the following input fields. You can then click the “Send Email” button. Then if you check your Google mail, this will look like as shown below. e3 And here's all the code used in this application:
  1. Imports System.Net.Mail
  2.  
  3. Public Class Form1
  4.  
  5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  6.         Dim mail As New MailMessage
  7.         'this area for our Sender
  8.         mail.From = New MailAddress(txtfrom.Text)
  9.         'this area for our Recipient
  10.         mail.To.Add(txtto.Text)
  11.         'this would the email subject
  12.         mail.Subject = txtsubj.Text
  13.         'this for the body of our email
  14.         mail.Body = txtbody.Text
  15.  
  16.         Dim smtp As New SmtpClient("smtp.gmail.com")
  17.         'set the EnableSsl to true
  18.         smtp.EnableSsl = True
  19.         'this area is for your login username and password
  20.         smtp.Credentials = New System.Net.NetworkCredential("yourUsername", "yourPassword")
  21.         'Gets the port used for SMTP Transactions
  22.         smtp.Port = "587"
  23.         'finally it sends the message to SMTP server for delivery
  24.         smtp.Send(mail)
  25.  
  26.     End Sub
  27.  
  28. End Class

Comments

Add new comment