1. Technology

Learn ASP.NET 2.0 - VWD in more depth

Sending email from a web page


Part 3 of the tutorial wouldn't be complete without learning some new code. In this part, we're going to start a new website to automate sending web based emails. The code this time will be both elementary and advanced. It will be elementary because I'm not including any validation code and the web design is as simple as it can be. But it will be advanced because Microsoft's latest namespace for sending SMTP (Simple Mail Transport Protocol) will be used and this will be the first introduction to the critically important web.config file.

Initially, the purpose of our website will be simply to allow a user to enter the basic parts of an email message into a web based form and send it to a fixed address. Getting started is the same as before, create a new website. I named mine AVBAsk. You can set the page title in the <title> tag, source view, in the .aspx source code. Add text and TextBox components for the email subject, message, and a return email address. Add a Button component to send the email. My form looks like this:

Click Here to display the illustration
Click the Back button on your browser to return

Now comes the fun part ... adding the code. The complete code is shown at the bottom of this article.

Microsoft's latest namespace for sending email using an SMTP mail server is System.Net.Mail. But to send it from the server web page, the server has to be able to connect to an SMTP mail server and you need to know all the parameters for the account you will be using to send.

Add an Imports statement at the top of your code for the namespace. Declare a New MailMessage object. This is where the parameters for the specific message are coded. Assign values to the properties as shown below. Finally, create a client and send the message. Here's the code:

Imports System.Net.Mail
Partial Class _Default
   Inherits System.Web.UI.Page
   Protected Sub AVBSend_Click( _
      ByVal sender As Object, _
      ByVal e As System.EventArgs) _
      Handles AVBSend.Click
      Dim AVBMsg As New MailMessage()
      AVBMsg.From = New MailAddress(AVBFrom.Text)
      AVBMsg.To.Add(New MailAddress("seigfried@msn.com"))
      AVBMsg.Subject = AVBSubject.Text
      AVBMsg.Body = AVBMessage.Text
      AVBMsg.IsBodyHtml = False
      AVBMsg.Priority = MailPriority.Normal
      Dim mySmtpClient As New SmtpClient()
   End Sub
End Class

To communicate with the server, however, you'll need to modify the web.config file. The traditional way to do this (which still works) is to edit the XML source directly. But VWD has a better way. Select ASP.NET Configuration under the Website menu. Microsoft has coded this particular utility as a webpage, the Web Site Administration Tool. Select the Application tab and click Configure SMTP e-mail settings. Here's what it should look like.

Click Here to display the illustration
Click the Back button on your browser to return

...   many   other   lines   of   XML   ...
         <smtp   from="yourid@yourserver.com">
            <network   host="mail.yourserver.com"   password="yourpassword"   userName="yourid@yourserver.com"   />

In the next lesson, ASP.NET 2.0 Server Controls, we do more with this starter system by learning about "server controls" and adding a few to the system.

  1. About.com
  2. Technology
  3. Visual Basic
  4. Learn VB.NET
  5. Learn ASP.NET
  6. Learn ASP.NET 2.0 - VWD in more depth

©2014 About.com. All rights reserved.