Skip Navigation LinksHome : CBC Classes : ASP.Net : Sending Email

CS218 ASP.Net - Sending Email

When you want to get information from users, the easiest way is to build a database table and an input form, then add the information to the database. The problem with this method is that you have to look in the database to see the data. If you get a lot of data, or new data on a daily basis then this works great. But if you don't get much input, say something every couple of weeks, it can be a pain to constantly look for new data. And you may get lazy about looking, and stop checking every day, which may cause a delay when you actually do receive some input. For these occasions, it may be better to have the input emailed, since you're most likely checking your email on a regular basis.

In this section we look at 3 different methods for emailing data from an .aspx page.

  1. Video Tutorial - using the mailto: URL (Remember this will NOT work if the user has web based mail such as hotmail or gmail.)
  2. Video Tutorial - forwarding through gmail or hotmail
  3. The code for forwarding through gmail or hotmail
  4. Video Tutorial - using your web host's SMTP server
  5. The code for using your web host's SMTP server

The code for forwarding through gmail or hotmail

Here's the .aspx code to build the user interface

    <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>First Name <br />
    <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>Last Name <br />
    <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>Email 

     <br />

    <asp:TextBox ID="txtMessage" runat="server" TextMode="MultiLine" Height="138px" 
        Width="428px"></asp:TextBox>Message <br />

    <asp:Button ID="Button1" runat="server" Text="Send" />

    <asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>

And here's the VB code:

Imports System.Net.Mail 'make sure that this is the first line in your VB file.

'This is the hotmail version. The gmail version is exactly the same except for a couple lines of code

    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        Dim MyMailMessage As New MailMessage()
        ' For gmail use your gmail address in the following 2 lines
        MyMailMessage.From = New MailAddress("")  
        MyMailMessage.Subject = "Hotmail Test"
        MyMailMessage.Body = txtFirstName.Text + txtLastName.Text + txtEmail.Text + txtMessage.Text
        'MyMailMessage.Body = txtMessage.Text

        'For gmail use "" in the following line:
        Dim SMTPServer As New SmtpClient("")  
        SMTPServer.Port = 587
        'The default timeout is 100,000 ms or 100 secs. If this isn't long enough try making it longer
        'SMTPServer.Timeout = 200000
        ' For gmail use your gmail address in the following line
        SMTPServer.Credentials = New System.Net.NetworkCredential("", "yourPassword")
        SMTPServer.EnableSsl = True
            lblMessage.text = "Hotmail Sent"
        Catch ex As SmtpException
            lblMessage.text = ex.ToString
        End Try
    End Sub