In this article I will explain with an example, how to send emails using GMAIL Account Credentials and also make use of threading to improve performance in ASP.Net using VB.Net.
 
 
HTML Markup
The HTML Markup consists of:
TextBox – For capturing the values of Recipient Email address, Subject, Body, Gmail account email address, Gmail account password.
FileUpload – For attaching file to the email.
Button – For sending email.
The Button has been assigned with an OnClick event handler.
<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td style="width: 80px">To:
        </td>
        <td><asp:TextBox ID="txtTo" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td>Subject:
        </td>
        <td><asp:TextBox ID="txtSubject" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td valign="top">Body:</td>
        <td><asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Height="150" Width="200"></asp:TextBox></td>
    </tr>
    <tr>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td>File Attachment:</td>
        <td><asp:FileUpload ID="fuAttachment" runat="server" /></td>
    </tr>
    <tr>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td>Gmail Email:</td>
        <td><asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td>Gmail Password:</td>
        <td><asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox></td>
    </tr>
    <tr>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td></td>
        <td><asp:Button ID="btnSend" Text="Send" OnClick="SendAsyncEmail" runat="server" /></td>
    </tr>
</table>
 
 
Mail Server Settings in Web.Config file
The following Mail Server settings need to be saved in the Web.Config file.
<system.net>
    <mailSettings>
        <smtp deliveryMethod="Network">
            <network
                host="smtp.gmail.com"
                port="587"
                enableSsl="true"
                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.Threading
Imports System.Configuration
Imports System.Net.Configuration
 
 
Sending email asynchronously in background using Thread
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.
 
SMTP Class Properties
Following are the properties of the SMTP 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)
 
When the SendAsyncEmail Button is clicked, the Recipient email address (to), the Sender email address (from), Subject and Body values are fetched from their respective fields and are passed as parameter to SendEmail function.
For sending email asynchronously in background, a new Thread object is defined and inside which the SendEmail function is called using delegate in C# and Sub in VB.Net.
Inside the SendEmail function, all these parameters are set into an object of the MailMessage class.
If the ASP.Net Multiple FileUpload control has attachment then, the attachment is added to the Attachments List of the MailMessage Object.
Then, object of the SmtpClient class is created and the settings of the Mail Server such has Host, Port, DefaultCredentials and EnableSsl are fetched from the mailSettings section of the Web.Config file and are set in respective properties of the SmtpClient class object.
And Username and Password values are passed as parameter to NetworkCredential class from their respective fields.
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.
 
Finally, the email is being sent and success the message is displayed in JavaScript Alert Message Box using RegisterStartupScript method.
Protected Sub SendAsyncEmail(sender As Object, e As EventArgs) Handles btnSend.Click
    Dim [to] As String = txtTo.Text
    Dim from As String = txtEmail.Text
    Dim password As String = txtPassword.Text
    Dim subject As String = txtSubject.Text
    Dim body As String = txtBody.Text
    Dim postedFile As HttpPostedFile = fuAttachment.PostedFile
    Dim email As New Thread(Sub() SendEmail([to], from, password, subject, body, postedFile))
    email.IsBackground = True
    email.Start()
    ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Email sent.');", True)
End Sub
 
Private Sub SendEmail([to] As String, from As String, password As String, subject As String, body As String, postedFile As HttpPostedFile)
    Dim smtpSection As SmtpSection = CType(ConfigurationManager.GetSection("system.net/mailSettings/smtp"), SmtpSection)
    Using mm As New MailMessage(from, [to])
        mm.Subject = subject
        mm.Body = body
        If postedFile.ContentLength > 0 Then
            Dim fileName As String = Path.GetFileName(postedFile.FileName)
            mm.Attachments.Add(New Attachment(postedFile.InputStream, fileName))
        End If
        mm.IsBodyHtml = False
        Dim smtp As New SmtpClient()
        smtp.Host = smtpSection.Network.Port
        smtp.EnableSsl = smtpSection.Network.EnableSsl
        Dim networkCred As New NetworkCredential(from, password)
        smtp.UseDefaultCredentials = smtpSection.Network.DefaultCredentials
        smtp.Credentials = networkCred
        smtp.Port = smtpSection.Network.Host
        smtp.Send(mm)
    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
 
 
Email Form
Send Email using GMAIL in ASP.Net using VB.Net
 
Received Email
Send Email using GMAIL in ASP.Net using VB.Net
 
 
Downloads