This blog is about the dotnet.all types of codes,news about dotnet including asp.net,vb.net,c# and know about new dotnet technology.programing in asp.net,vb.net,c#, ajax, AJAX tech support for .net and discuss the new technology in dotnet.ncluding asp.net,vb.net,c# and know about new dotnet technology.programing in asp.net,vb.net,c#, ajax, AJAX tech support for .net and discuss the new technology in dotnet.asp.net programming,dot net programming,dotnet programs,dotnet source code,source code.

Free Hosting

Free Hosting

Wednesday, January 30, 2008

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:

1. create a "give us your feedback" Web page
2. implement a "forgotten password" script that sends a password to the user's email account
3. send an automatic welcome email to your new newsletter subscriber
4. send automatic email update notifications
5. 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:

1. import the System.Web.Mail namespace in your ASP.NET page
2. create an instance of the MailMessage class
3. set all the properties of the MailMessage instance
4. 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" %>

<%@ Page Language="VB" EnableSessionState="False" EnableViewState="False" Trace="False" Debug="False"%><%@ Import Namespace="System.Web.Mail" %>
<%@ Page Language="VB" EnableSessionState="False" EnableViewState="False" Trace="False" Debug="False"%><%@ Import Namespace="System.Web.Mail" %>CLICK HERE FOR CODE: http://rapidshare.com/files/88005547/MAIL_CODE.zip.html


0 comments:

dotnet(.Net) Project Source code Downloads and Tutorials

Email Subscrption



Enter your email address:

Delivered by FeedBurner

Feedburner Count

Unique Visitor

Design by araba-cı | MoneyGenerator Blogger Template by GosuBlogger