Monday, May 4, 2009

Send Email From ASP.NET

This tutorial will show you how to send a simple email message using ASP.NET 2.0 and VB.NET 

Sending a email using ASP.NET 2.0 and VB.NET is actually very simple. 

First, you will need to import the System.Net.Mail namespace.

The System.Net.Mail namespace contains the SmtpClient and MailMessage Classes that we need in order to send the email.

Imports System.Net.Mail

We use the btnSubmit_Click event to do the work.

We then call the emailClient.Send to send the message using the variables from our ASP.NET coded page.

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Try
Dim message As New MailMessage(txtTo.Text, txtFrom.Text, txtSubject.Text, txtBody.Text)
Dim emailClient As New SmtpClient(txtSMTPServer.Text)
emailClient.Send(message)
litStatus.Text = "Message Sent"
Catch ex As Exception
litStatus.Text = ex.ToString()
End Try
End Sub




No comments:

Post a Comment