In this tutorial, we will learn How to send emails from the VB.NET Application or how to send emails to yourself by using Visual Basic Windows Application (VB.NET). What is SMTP? Sending mail through Gmail in Visual Basic. How to solve the error which you will be getting in Sending Email Application in VB.NET.
What is SMTP?
The full form of SMTP is the Simple Mail Transfer Protocol. This is a protocol for sending e-mail messages between servers. This is basically an Internet standard for electronic mail (e-mail) transmission. The default TCP port used by SMTP is 25 they can differ in different systems and the SMTP connections secured by SSL.
SMTP Authentication
It provides a set of protocols that simplify the communication of email messages between email servers. Most SMTP server names are written in the form “smtp.domain.com” or “mail.domain.com”: for example, a Gmail account will refer to smtp.gmail.com. Most email systems that send mail over the Internet use SMTP to send messages from one server to another, the messages can be retrieved with an email client using either POP or IMAP.
Smtp_Server.Credentials = New _ Net.NetworkCredential("username@gmail.com", "password")
You must provide your email address in the place of “username@gmail.com” and the real password of your given Gmail account in the place of “password” for credentials.
Sending Email through Gmail in VB.Net
How do I send mail using VB.Net?
VB.NET uses SMTP protocol for sending emails. SMTP stands for Simple Mail Transfer Protocol. VB.NET using System.Net.Mail namespace for sending mail. We can instantiate SmtpClient class and assign the Host and Port.
Using Gmail Server:
The Gmail SMTP server name is smtp.gmail.com and the port using send mail is 587. Here using Network Credential for password-based authentication.
Here we have to provide the information like our Gmail username and password, and then to address also.
The below example demonstrates how to send mail using the SmtpClient class. The following points to be noted in this are −
- We must specify the SMTP host server that you use to send e-mails. The Host and Port properties will be different for the different host servers. We will be using a Gmail server.
- We need to give the Credentials for authentication if required by the SMTP server.
- We should also provide the email address of the sender and the e-mail address or addresses of the recipients using the MailMessage.From and MailMessage.To properties, respectively.
- We should also specify the message content using the MailMessage.Body property.
Example:
In this example, we will create a simple application that would send an e-mail, here we will send an email to ourselves for testing the application. Use the following steps −
Step 1:
Open your Visual Studio > Go to File Menu > Click on New Project > Select Visual Basic Windows Application > Name the Project and then > Click on OK.
Step 2:
On Form1 Take three labels, three text boxes, and one-button control from the toolbox. Change the text properties of the labels to – ‘From:‘, ‘To:‘ and ‘Message:‘ respectively. Change the text property of the button control to ‘Send‘.
Step 3:
First of all add the following namespaces on top of the code window.
Imports System.Net.Mail Imports System.Net
Below the public class Form1 add the following code,
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Me.Text = "Email Sending App" End Sub
Double click on the Send button and add the following code there.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Try Dim Smtp_Server As New SmtpClient Dim e_mail As New MailMessage() Smtp_Server.UseDefaultCredentials = False Smtp_Server.Credentials = New Net.NetworkCredential("username@gmail.com", "password") Smtp_Server.Port = 587 Smtp_Server.EnableSsl = True Smtp_Server.Host = "smtp.gmail.com" Smtp_Server.DeliveryMethod = SmtpDeliveryMethod.Network e_mail = New MailMessage() e_mail.From = New MailAddress(Textbox1.Text) e_mail.To.Add(TextBox2.Text) e_mail.Subject = "Email Sending" e_mail.IsBodyHtml = False e_mail.Body = TextBox3.Text Smtp_Server.Send(e_mail) MsgBox("Mail Sent") Catch ex As Exception MsgBox(ex.ToString) End Try End Sub
You must provide your email address and real password for credentials.
Step 4:
Run the Application.
Output:
When we press F5 and run the application. First, we will see the Form1 just shown below image Fill your email address in From field and other person’s email address in the field of To whom you want to send mail. Write the message in message field and click on Send. The message will sent to that person and it will show the message box with the text “Mail Sent”. As shown in the below image.
Here in this example we have trying to send emails to ourselves that is why we fill our email address in both fields of TextBoxes in From: and To:
when you will enter your email address in both the textboxes then write any text or message in the field of Message. Then click on the Send button. If the message will have been sent to your email address successfully then the message box will show the message “Mail Sent” else if it didn’t then you will see an error window something like this.
Error while sending mail in Vb.net
In this window, it will show the error to correct that is some Authentication Required. Then we have to do the required authentication for that follow these steps-
- First go to your Google account > Setting.
- Then click on Security Settings.
- Find the option Less Secure App Access turn On this option.
Now go back on our application again and fill your email address in both field again write the text in message field and then click on send button. The message box will appear that shows “Mail Sent”.
Then check your Gmail box you will find the same message has sent to you. You can clearly see the below image that the message I have sent is delivered in my mailbox from my email address to myself, here I am a sender and also I am a recipient.
If you were also not able to send messages so by using this setting you will be able to send easily. Do try yourself.