In this article I will explain with an example, how to send emails using the System.Net class in VB.Net.
Mail Server Settings in App.Config file
The following Mail Server settings need to be saved in the App.Config file.
Note: It is necessary to use the sender’s email address credentials while defining the Gmail SMTP Server Credentials as Gmail the sender’s email address must be equal to Gmail Username specified in credentials.
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="sender@gmail.com">
<network
host="smtp.gmail.com"
port="587"
enableSsl="true"
userName="sender@gmail.com"
password="SenderGmailPassword"
defaultCredentials="true" />
</smtp>
</mailSettings>
</system.net>
Namespaces
You will need to import the following namespaces.
Imports System.IO
Imports System.Net
Imports System.Net.Mail
Imports System.Configuration
Sending SMTP email using System.Net Class in VB.Net
MailMessage class properties
Following are the required properties of the MailMessage class.
From – Sender’s email address.
To – Recipient(s) Email Address.
CC – Carbon Copies (if any).
BCC – Blind Carbon Copies (if any).
Subject – Subject of the Email.
Body – Body of the Email.
IsBodyHtml – Specify whether body contains text or HTML mark up.
Attachments – Attachments (if any).
ReplyTo – ReplyTo Email address.
SmptClient class properties
Following are the properties of the SmtpClient class.
Host – SMTP Server URL (Gmail: smtp.gmail.com).
EnableSsl – Specify whether your host accepts SSL Connections (Gmail: True).
UseDefaultCredentials – Set to True in order to allow authentication based on the Credentials of the Account used to send emails.
Credentials – Valid login credentials for the SMTP server (Gmail: email address and password).
Port – Port Number of the SMTP server (Gmail: 587)
Sending Email using System.Net Class
When the Program is executed, the Recipient email address (To), Subject and Body is read as input from the User, while the Sender email address (from) values are fetched from the SMTP section of the App.Config file.
Then, all these values are set into an object of the MailMessage class.
An object of SmtpClient class is created and the settings of the SMTP server such as Host, EnableSsl, Username, Password, DefaultCredentials and Port are fetched from the SMTP section of the App.Config file.
Finally, the email is sent using the Send function of the SmtpClient class object and success message is displayed.
Sub Main()
Console.WriteLine("Enter To Address:")
Dim [to] As String = Console.ReadLine().Trim()
Console.WriteLine("Enter Subject:")
Dim subject As String = Console.ReadLine().Trim()
Console.WriteLine("Enter Body:")
Dim body As String = Console.ReadLine().Trim()
Dim smtpSection As SmtpSection = CType(ConfigurationManager.GetSection("system.net/mailSettings/smtp"), SmtpSection)
Using mm As MailMessage = New MailMessage(smtpSection.From, [to])
mm.Subject = subject
mm.Body = body
mm.IsBodyHtml = False
Using smtp As SmtpClient = New SmtpClient()
smtp.Host = smtpSection.Network.Host
smtp.EnableSsl = smtpSection.Network.EnableSsl
Dim networkCred As NetworkCredential = New NetworkCredential(smtpSection.Network.UserName, smtpSection.Network.Password)
smtp.UseDefaultCredentials = smtpSection.Network.DefaultCredentials
smtp.Credentials = networkCred
smtp.Port = smtpSection.Network.Port
Console.WriteLine("Sending Email......")
smtp.Send(mm)
Console.WriteLine("Email Sent.")
System.Threading.Thread.Sleep(3000)
Environment.[Exit](0)
End Using
End Using
End Sub
Possible Errors
The following error occurs when you try to send email using Gmail credentials in your application.
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
Solution
Screenshot
Downloads