Luckily, ASP.NET makes sending emails, using server side scripts as easy as talking walk in the park. Version 1.x of the .NET Framework incorporates many classes system like System.Web.Mail, which allows developers to send mails by using some lines of code. Although, these classes and APIs still exist in the Version 2.0 of the .NET framework, other, and obliviously better classes are used which are found in the System.Net.Mail library. The Version 1.x classes and namespaces have been deprecated.
In this tutorial, we will take a peek at what the new System.Net.Mail namespace has to offer, and how can we send emails using the new ASP.NET 2.0.
The System.Net.Mail package or namespace is made up of sixteen classes. These are present for the sole purpose of enabling developers to send, receive and track email(s) using SMTP.
The core classes in the System.Net.Mail package include the MailMessage class, and the SmtpClient class. The MailMessage class represents an email. It has properties like From, To, Subject, Body, etc. The SmtpClient class sends the mail object created by using the MailMessage class to a predefined SMTP server.
Sending an email using ASP.NET 2.0 requires two steps. The first one involves the creation of the MailMessage instance and setting the properties of it. The Second step involves the “SEND†method of the SmtpClient Class. The Send method takes the object of the MailMessage class as its argument.
Programmatically, it is not important to create an instance of the MailMessage class, since the ‘send’ method of the SmtpClient can take either, the MailMessage object, or four strings. The four strings represent from, to, subject, and body properties of the MailMessage class.
The other classes in the System.Net.Mail API provide, progressive email functionality, such as, some classes can be used to attach or embed objects in an email, others can handle SMTP based exceptions.
Whenever you use email clients, such as, the Outlook or free email services, like GMail, these programs or web applications set up a connection with a relay server, to relay the sent email message. The relay server accepts the message and then “relays†or transfers it to the recipients SMTP server.
Hence, to start sending messages using the new ASP.NET 2.0, we will have start by providing the SmtpClient class object with the information about the relay server. The information required by the SmtpClient instance, includes the relay hostname, the port, authentication credentials and whether the relay server uses SSL or not.
The server relay information can be provided programmatically or can be centrally specified in the Web.config file. This is the way Web.config would need to be edited.
Open the Web.config file in a suitable text editor, like notepad and append a <system.net> element within the <configuration> element. Within the <mailSettings> element, which is nested in the <system.net> element, we add a <smtp> element. Within this <smtp> element we would need to add the relay information.
<configuration> <system.net> <mailSettings> <smtp> <network host="RELAY_HOST_NAME" port="PORT_NUMBER" userName="USER_NAME" password="PASSWORD" /> </smtp> </mailSettings> </system.net> … … … </configuration>
The RELAY_HOST_NAME is to be changed to the host name of the relay server. If the port is anything other than the standard SMTP port 25, then you would have to provide the port number as value for the port attribute. If the relay host requires authentication, you will have change USER_NAME and PASSWORD to the authorized ones.
Here is an example of sending an email using a feedback form, using ASP.NET 2.0.
Web Form:
<table border="0"> <tr> <td><b>Email:</b></td> <td><asp:TextBox runat="server" ID="Email_Spec" Columns="30"></asp:TextBox></td> </tr> <tr> <td><b>Subject:</b></td> <td><asp:TextBox runat="server" ID="Subject_Spec" Columns="30"></asp:TextBox></td> </tr> <tr> <td colspan="2"> <b>Body:</b><br /> <asp:TextBox runat="server" ID="Email_Body" TextMode="MultiLine" Columns="55" Rows="10"></asp:TextBox> </td> </tr> <tr> <td colspan="2" align="center"> <asp:Button runat="server" ID="Send_Email" Text="Send Mail" /> </td> </tr> </table>
Protected Sub Send_Email_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SendEmail.Click Const Email_by As String = "source@ofyouremail.com" ‘Change source@ofyouremail.com to the email address of the sender. ‘creating the instance of the MailMessage class. Dim mailmsg As New MailMessage(Email_Spec.Text, Email_by) ‘Setting the instance properties of the MailMessage object mailmsg.Subject = Subject_Spec.Text mailmsg.Body = Email_Body.Text mailmsg.IsBodyHtml = False ’sending the message via the SmtpClient Class using the web.config file. Dim smtpsend As New SmtpClient smtpsend.Send(mailmsg) End Sub
That’s it. You are now ready to scoot emails across to your clients, prospects, friends and more, using the enhanced classes provided to you by ASP.NET 2.0.
Note: We haven’t set the SmtpClient properties because we are using the web.config file to send the mail