Sending Web eMail in ASP.NET
One of the most common functionalities used in Web development is sending email from a Web page. Before you ask, I'll give you a couple of reasons why you might want to send email from your Web application:
# create a "give us your feedback" Web page
# implement a "forgotten password" script that sends a password to the user's email account
# send an automatic welcome email to your new newsletter subscriber
# send automatic email update notifications
# send automatic email notifications whenever an error occurs in your Web application
Of course there are many more situations where it's appropriate to send email from your Web applications, and it's up to you to determine exactly how and where you're going to implement this functionality.
Enter: .NET
The .NET framework makes the task of sending email from a Web page unbelievably easy and I'll show you how to do it in a matter of minutes. This article presumes that you have a basic understanding of how .NET and ASP.NET works.
1. Import the namespace, and create instances of required classes
We are going to use the SmtpMail and MailMessage classes, which belong to the System.Web.Mail namespace. In order to use them, we will need to import the System.Web.Mail namespace like this:
<%@ Import Namespace="System.Web.Mail" %>
The MailMessage class provides properties and methods for constructing an email message. To build our email message we need to create an instance of this class:
Dim objMail As New MailMessage()
2. Set the properties of the object
Next, we have to set all the properties of the objMail object:
' The email address of the sender
objMail.From = "yourname@yourdomain.com"
' The email address of the recipient
objMail.To = "recipientname@somedomain.com"
' The email address of the Cc recipient
objMail.Cc = "name1@anotherdomain.com"
' The email address of the Bcc recipient
objMail.Bcc = "name2@anotherdomain.com"
' The format of the message - it can be MailFormat.Text
or MailFormat.Html
objMail.BodyFormat = MailFormat.Text
' The priority of the message - it can be MailPriority.High,
MailPriority,Normal or MailPriority.Low
objMail.Priority = MailPriority.High
'The subject of the message
objMail.Subject = "My first ASP.NET email"
'The message text
objMail.Body = "This is my first email sent via
ASP.NET. It was easier than I thought :)"
After all the properties (some of them are optional) of our new email message are set properly, the only thing that's left to do is send the message.
3. Send the Message
If you have experience sending email from classic ASP script with CDONTS you might think that the MailMessage class has method Send or something similar. Well, the way ASP.NET sends email messages is a little different from ASP.
We need to use the static method Send of the SmtpMail class, passing in our objMail instance. You might be asking "what's a 'static method'?" A static method is one that's not associated with an instance of a type. An example of an instance of a type is our objMail object. It is illegal to reference a static member of a class through an instance. The proper way to do it is:
SmtpMail.Send(objMail)
If you want to specify an SMTP server that's different than the default, you'll need to set the SmtpServer property of the SmtpMail class:
SmtpMail.SmtpServer = "smtp.your-server.com"
Summary
In summary, to send an email from your ASP.NET page, you need to:
# import the System.Web.Mail namespace in your ASP.NET page
# create an instance of the MailMessage class
# set all the properties of the MailMessage instance
# send the message with SmtpMail.Send method
And finally, to get you started, here's the complete, functional code for an ASP.NET page that sends the user's feedback via email to the Webmaster:
<%@ Page Language="VB" EnableSessionState="False"
EnableViewState="False" Trace="False" Debug="False"%>
<%@ Import Namespace="System.Web.Mail" %>
<script language="VB" runat=server>
Sub Page_Load(Sender as Object, E as EventArgs)
If Page.IsPostBack Then
lblResponse.Text = "Your email has been sent."
End If
End Sub
Sub btn_Click(sender as Object, e as System.EventArgs)
If Request.Form("Email") <> "" Then
Dim objMail As New MailMessage()
objMail.From = "your_name@yourdomain.com"
objMail.To = Request.Form("Email")
objMail.Subject = Request.Form("Subject")
objMail.Body = Request.Form("Message")
objMail.BodyFormat = MailFormat.Text
SmtpMail.SmtpServer = " smtp.your-server.com"
SmtpMail.Send(objMail)
Else
lblResponse.Text = "Please enter an email address."
End If
End Sub
</script>
<html>
<head>
<style>
.main {font-family:Verdana; font-size:12px;}
.title {font-family:Verdana; font-size:18px; font-weight:bold;}
</style>
</head>
<body>
<span class="title" align="center">Send email from
an ASP.NET page</span>
<br><br><asp:Label class="main" id="lblResponse"
runat="server"/>
<form method="POST" name="MainForm" runat="server">
<table>
<tr>
<td class="main" align="right">Email:</td>
<td class="main"><input type="text"
class="main" name="Email" value=""></td>
</tr>
<tr>
<td class="main" align="right">
Subject:</td>
<td class="main"><input type="text"
class="main" name="Subject" value=""></td>
</tr>
<tr>
<td class="main" align="right"
valign="top">Message:</td>
<td class="main"><textarea name="Message"
cols="50" rows="8"></textarea></td>
</tr>
<tr>
<td class="main"> </td>
<td class="main"><input type="Submit"
id="btnSubmit" OnServerClick="btn_Click" value="Send"
runat="server" /></td>
</tr>
</table>
</form>
</body>
</html>
No comments:
Post a Comment