When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
Author an Article
Technology Jobs
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.
Published: Wednesday, August 08, 2001

Sending Email from an ASP.NET 1.x Web Page

By Scott Mitchell


Sending Emails in ASP.NET Version 2.0
This article looks at sending email using the classes in the System.Web.Mail namespace, which was introduced in the .NET Framework 1.x. Version 2.0 of the .NET Framework has deprecated this namespace, updating it with the System.Net.Mail namespace. While the System.Web.Mail namespace remains in ASP.NET 2.0 for backwards compatability, if you are using ASP.NET 2.0, check out: Sending Email in ASP.NET 2.0.

Introduction
One of the most commonly asked questions at ASPMessageboard.com is along the lines of, "How do I send an email through an ASP page?" The wide array of email FAQs on ASPFAQs.com is also proof of this being a commonly asked question. In this article, we will look at how to send email from an ASP.NET Web page! (For more information about ASP.NET be sure to check out the ASP.NET Article Index.)

- continued -

'

In order to send an email from an ASP.NET Web page, you need to use the SmtpMail class, found in the System.Web.Mail namespace, which contains a static method Send. (More on this class later in the article!) In any case, the simplest way to send an email is to call this Send method passing in an instance of the MailMessage class. What's the MailMessage class, you ask? It's a class (also in the System.Web.Mail namespace) that represents an email message.

Creating a MailMessage
The MailMessage class contains properties very similar to those found in CDONTS (the often-used component to send emails from a classic ASP page). Such properties include: To, From, Cc, Bcc, BodyFormat, Subject, Priority, Body, etc. So, to send an email, we should create an instance of the MailMessage class and set its properties.

'Create an instance of the MailMessage class
Dim objMM as New MailMessage()

'Set the properties
objMM.To = "someone@someaddress.com"
objMM.From = "someoneelse@someotheraddress.com"

'If you want to CC this email to someone else...
objMM.Cc = "someone2@someaddress.com"

'If you want to BCC this email to someone else...
objMM.Bcc = "someoneElse@someaddress.com"

'Send the email in text format
objMM.BodyFormat = MailFormat.Text
'(to send HTML format, change MailFormat.Text to MailFormat.Html)

'Set the priority - options are High, Low, and Normal
objMM.Priority = MailPriority.Normal

'Set the subject
objMM.Subject = "Hello there!"

'Set the body - use VbCrLf to insert a carriage return
objMM.Body = "Hi!" & vbCrLf & vbCrLf & "How are you doing?"

Note that you will need to import the System.Web.Mail namespace in your ASP.NET Web page in order to be able to utilize the above code as-is: <% @Import Namespace="System.Web.Mail" %>.

Sending the MailMessage
Once we have a MailMessage class instance with the correct properties set, sending the actual email is a breeze. We simply need to call the static Send method of the SmtpMail class, passing in the MailMessage class instance:

'Now, to send the message, use the Send method of the SmtpMail class
SmtpMail.Send(objMM)

The SmtpMail class uses the SMTP Service built into Windows 2000 to do the work of actually sending the email message. By using the Send method above, the local SMTP server is used to send the email message. In order to specify a different SMTP mail server to use to send the message, set the SmtpServer property:

SmtpMail.SmtpServer = emailServerName

Be careful, though. Since this is a static property, changing this value in one ASP.NET Web page will affect all ASP.NET Web pages using sending emails via the SmtpMail object. That is, if in one ASP.NET Web page you set the SmtpServer property to myMailServer, and then, in another ASP.NET Web page, do not specify the SmtpServer property, intending to use the default SMTP server, myMailServer will be used instead. For that reason, you should probably always specify the SmtpServer property - if you want to use the default SMTP server, simply do:

SmtpMail.SmtpServer = ""

Receiving Feedback from Web Visitors
A common use of sending email from a Web page is allowing site visitors to fill in comments via an HTML form and send them in to the site's administrator. 4Guys contains such a feedback page, as do many other sites. The code at the end of this article shows how to create such a feedback form as an ASP.NET Web page; this below code illustrates how to send an email message using the MailMessage and SmtpMail classes, as well as how to use the asp:panel Web control to display certain "chunks" of pages at certain times. (For more information on the asp:panel Web control see this article.)

Conclusion
Hopefully this article has shown how easy it is to send an email via an ASP.NET Web page. While the syntax differs slightly from sending an email via a classic ASP page, the overall approaches are similar. Note that when creating any .NET application (be it an ASP.NET Web page, a Web service, or a stand-alone Windows application), you will use the techniques shown here to send an email.

Happy Programming!

  • By Scott Mitchell


    Related Links

  • Visit the ASP.NET Article Index
  • Visit the Email FAQs Category
  • Visit www.SystemWebMail.com (tons of FAQs and samples for sending email in ASP.NET 1.x)


    Source Code for Creating an ASP.NET Visitor Feedback Page
    <% @Import Namespace="System.Web.Mail" %>
    <script language="vb" runat="server">
    
    Sub btnSendFeedback_Click(sender as Object, e as EventArgs)
    
      'Create an instance of the MailMessage class
      Dim objMM as New MailMessage()
    
      'Set the properties - send the email to the person who filled out the
      'feedback form.
      objMM.To = "webmaster@yoursite.com"
      objMM.From = txtEmail.Text
    
      'If you want to CC this email to someone else, uncomment the line below
      'objMM.Cc = "someone@someaddress.com"
    
      'If you want to BCC this email to someone else, uncomment the line below
      'objMM.Bcc = "someone@someaddress.com"
    
      'Send the email in text format
      objMM.BodyFormat = MailFormat.Text
      '(to send HTML format, change MailFormat.Text to MailFormat.Html)
    
      'Set the priority - options are High, Low, and Normal
      objMM.Priority = MailPriority.Normal
    
      'Set the subject
      objMM.Subject = "4GuysFromRolla.com - Feedback"
    
      'Set the body
      objMM.Body = "At " + DateTime.Now + " feedback was sent from an ASP.NET " & _
                   "Web page.  Below you will find the feedback message " & _
                   "send by " & txtName.Text & "." & vbCrLf & vbCrLf & _
                   "---------------------------------------" & vbCrLf & vbCrLf & _
                   txtMessage.Text & vbCrLf
    
      
      'Specify to use the default Smtp Server
      SmtpMail.SmtpServer = ""
      
      'Now, to send the message, use the Send method of the SmtpMail class
      SmtpMail.Send(objMM)
    
    
      panelSendEmail.Visible = false
      panelMailSent.Visible = true
    End Sub
    
    </script>
    
    <html>
    <body>
      <asp:panel id="panelSendEmail" runat="server">
        <form runat="server">
          <h2>We are interested in your feedback!  Please enter the following
          requested information below to send us your comments.</h2>
    
          <b>Your Name:</b>
          <asp:textbox id="txtName" runat="server" />
          <br>
    
          <b>Your Email Address:</b>
          <asp:textbox id="txtEmail" runat="server" />
          <p>
    
          <b>Your Message:</b><br>
          <asp:textbox id="txtMessage" TextMode="MultiLine"
                          Columns="40" Rows="10" runat="server" />
          <p>
    
          <asp:button runat="server" id="btnSendFeedback" Text="Send Feedback!"
                      OnClick="btnSendFeedback_Click" />
        </form>
      </asp:panel>
    
    
      <asp:panel id="panelMailSent" runat="server" Visible="False">
        An email has been sent to the email address you specified.  Thanks!
      </asp:panel>
    </body>
    </html>
    

  • Software Developer / Programmer - Distributed Systems (NYC)
    Next Step Systems
    US-NY-New York

    Justtechjobs.com Post A Job | Post A Resume


    ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article