SmtpClient Class in ASP.Net using VB.NET

In this article we will discuss SmtpClient Class.
  • 1792
 

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"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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("")
    End Sub
</script>
<
html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Sending E-Mails using SmtpClient</title>
</head>
<
body>
    <form id="form1" runat="server">
    <div>
    <asp:Button ID="Button1" runat="server" Text="Send Email" OnClick="Button1_Click" />
    <asp:Label ID="Label1" runat="server" Text="Email Sent."></asp:Label>
    </div>
    </form>
</body>
</
html>

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

Properties
 

  • 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.

Methods
 

  • 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.

Events
 

  • 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.

HAVE A GREAT CODING!

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.