Send E-Mail from Your Web Applications

Page Design

By using the code attached to this article, with just a bit of configuration, you can send e-mails from your Web pages.

The e-mail application page that you create should be designed to contain the necessary e-mail fields:

  1. A "label from" control with text property initialized to your e-mail address or any other address to be used to indicate where the e-mail came from.
  2. A note of caution: You can write any e-mail ID in this control. The e-mail will apeear to be sent from that ID, so you send mail to anyone from anyone else's e-mail ID. This is discouraged, and this is why it is best to not make your 'from' field an open entry field. Otherwise, people may use your form to send spam.
  3. Three textboxes one each for "textTo", "textSubject", and "textComments" controls.
  4. Two buttons, one for Sending mail and other for clearing the textboxes.

For my form, the page design looks like the page below:

Send E-Mail from Your Web Applications

The VB Code

The e-mail form uses VB that has to be included. The two buttons on the page should be programmed to fulfill the desired tasks. A click handler can be used for the buttons (viz. buttonSend).

The code goes like this:

Sub buttonSend_Click(sender As Object, e As EventArgs)

   Dim emailMessage as MailMessage
   emailMessage=New MailMessage
   emailMessage.From=labelFrom.Text
   emailMessage.To=textTo.Text
   emailMessage.Subject=textSubject.Text
   emailMessage.Body=textMessage.Text
   SmtpMail.SmtpServer="localhost"
   Try
      SmtpMail.Send(emailMessage)
      labelStatus.Text="Message sent!"
      buttonSend.Enabled=False
   Catch ex as Exception
      labelStatus.Text="Unable to send the e-mail message"
   End Try
End Sub

The other button (viz. buttonNewMessage) should also deal with the click event and the code:

Sub buttonNewMessage_Click(sender As Object, e As EventArgs)

   textTo.Text=""
   textSubject.Text=""
   textMessage.Text=""
   buttonSend.Enabled=True
   labelStatus.Text=""
End Sub

Because this is an ASP page, the above code should be enclosed within the following tags:

<%@ Page Language="VB" %>
<%@ import Namespace="System.Web.Mail" %>
<script runat="server">
              .
              .
              .
              .
</script>

Send E-Mail from Your Web Applications

Validating The Controls

The textTo control should be validated for e-mail formats using regular expressions, whereas the textMessage and textTo controls should be coded to be compulsory fields. The required validation code goes like this:

<asp:RequiredFieldValidator
   id="RequiredFieldValidator1"
   runat="server"
   ControlToValidate="textTo" 
   ErrorMessage="You must enter a
   recipient!">
</asp:RequiredFieldValidator>

<asp:RegularExpressionValidator 
      id="RegularExpressionValidator1"
      runat="server"
      ControlToValidate="textTo"
      ErrorMessage="Invalid e-mail address!"
      ValidationExpression="\w+([-+.]\w+)*@\w+
         ([-.]\w+)*\.\w+([-.]\w+)*">
      </asp:RegularExpressionValidator>

Because the controls are coded to be validated, you have your page ready for the application.

Note: Because this page is an Active Server Page, do not forget to include the code runat="server" in the body of the page designed for sending e-mail.

The only thing you have left to do is to configure your SMTP to send e-mails according to the file system (FAT or NTFS) you have on your computer. Add this configuration and you will have your application ready.

Now it's on you to use the form to send e-mails. You should, however, avoid mis-using this form!



About the Author

Abhijit Mehta

I am a CSE student at NIT Dgp,INDIA.My real name is Abhijit Mehta. I do web designing and wish to make commercial use of it. Keen to learm a lot of things and m always too curious I am also MICROSOFT STUDENTS CHAMP .

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: May 7, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT This eSeminar will explore three popular games engines and how they empower developers to create exciting, graphically rich, and high-performance games for Android® on Intel® Architecture. Join us for a deep dive as experts describe the features, tools, and common challenges using Marmalade, App Game Kit, and Havok game engines, as well as a discussion of the pros and cons of each engine and how they fit into your development …

  • Instead of only managing projects organizations do need to manage value! "Doing the right things" and "doing things right" are the essential ingredients for successful software and systems delivery. Unfortunately, with distributed delivery spanning multiple disciplines, geographies and time zones, many organizations struggle with teams working in silos, broken lines of communication, lack of collaboration, inadequate traceability, and poor project visibility. This often results in organizations "doing the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds