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.

  1. <%@ Page Language="VB" %>  
  2. <%@ Import Namespace="System.Net.Mail" %>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
  4.   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5. <script runat="server">  
  6.     Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)  
  7.         Dim client As New SmtpClient()  
  8.         client.Host = "localhost"  
  9.         client.Port = 25  
  10.         client.Send("[email protected]""[email protected]""this is my subject""this is my body")  
  11.     End Sub  
  12. </script>  
  13. <html xmlns="http://www.w3.org/1999/xhtml" >  
  14. <head id="Head1" runat="server">  
  15.     <title>Sending E-Mails using SmtpClient</title>  
  16. </head>  
  17. <body>  
  18.     <form id="form1" runat="server">  
  19.     <div>  
  20.     <asp:Button ID="Button1" runat="server" Text="Send Email" OnClick="Button1_Click" />  
  21.     <asp:Label ID="Label1" runat="server" Text="Email Sent."></asp:Label>  
  22.     </div>  
  23.     </form>  
  24. </body>  
  25. </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!


Similar Articles