SmtpClient Class in ASP.NET using VB.NET

SmtpClient Class

SmtpClient Class allows the developer to send emails (electronic mails). SmtpClient Class is available in System.Net.Mail Namesapce. As we know in each class in .NET Framework includes list of properties, methods, and events, in other word we say these all are the members of class. Look at the example given below.

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Net.Mail" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim client As New SmtpClient()
        client.Host = "localhost"
        client.Port = 25
        client.Send("[email protected]", "[email protected]", "this is my subject", "this is my body")
    End Sub
html xmlns="" >
<head id="Head1" runat="server">
    <title>Sending E-Mails using SmtpClient</title>
    <form id="form1" runat="server">
    <asp:Button ID="Button1" runat="server" Text="Send Email" OnClick="Button1_Click" />
    <asp:Label ID="Label1" runat="server" Text="Email Sent."></asp:Label>

In above coding I have used the following properties, methods and events:


  • Host: It is used to set the name or IP address of host for SMTP.
  • Port: The number of the port to use when sending an email message. By default we use 25 here.


  • Send: It sends the emails to SMTP server that we have used as host name above.
  • SendAsync: It just sends the emails to SMTP server asynchronously, in case we have multiple email addresses.


  • SendCompleted: This is a most useful event fires when asynchronous operation completes.

As I have given coding above, sends the email by using the local SMTP Server but if you don't have enabled your SMTP server, then you will receive the. To enable local SMTP Server open Internet Information Services (IIS), right-clicking Default SMTP Virtual Server, and selecting Start.


Up Next
    Ebook Download
    View all
    View all