Wednesday, September 21

send email in asp.net


E-mail is often a standard part of Web-basedapplications. You may use it to send logon information to users oreven to send error messages to application administrators. ASP.NETmakes it easy to utilize e-mail in an application with theSystem.Web.Mail namespace. Let's take a closer look at putting thisnamespace to work in your applications.
System.Web.Mail namespace
The Microsoft documentation provides a goodoverview of the System.Web.Mail namespace. It's composed of classesthat allow you to create and send messages using the CollaborationData Objects for Windows 2000 (CDOSYS) message component. Theactual message may be delivered via the SMTP (Simple Mail TransferProtocol) mail service built into Microsoft Windows 2000 and beyondor through an arbitrary SMTP server. The classes in this namespacearen't restricted to an ASP.NET application.
The namespace includes three classes:
  • MailAttachment: Provides properties and methods for constructing ane-mail attachment.
  • MailMessage: Provides properties and methods for constructing ane-mail message.
  • SmtpMail:Provides properties and methods for sending messages using theCDOSYS message component.
MailAttachment classThe basic approach is the creation of a MailMessage objectfollowed by sending it on its way via a SmtpMail object. TheMailMessage class contains numerous methods and properties forworking with an e-mail message. Properties such as From, Subject,and Body provide everything you need to create an e-mail message,but a SmtpMail object is still necessary for sending it on itsway.
SmtpMailclassThe SmtpMail class includes the SmtpServer property that getsor sets the name of the SMTP relay mail server to use to sendmessages, and the Send method actually sends the message. The Sendmethod is overloaded. It allows a message to send using twoapproaches:
  • A MailMessage object is passed to theSmtpServer object.
  • Four string objects may be passed to theSmtpServer object with the first being the From field followed bythe Recipient, Subject, and the message's Body.
You'll use the MailAttachment and SmtpMailclasses together to create the necessary messages in yourapplication, but make sure the Web server is properly configured tosend a message via SMTP. Since IIS (Internet Information Services)is the most popular platform for ASP.NET applications, go ahead anduse both the IIS and SMTP services to send messages from yourapplication.
Using SMTP with IIS
You can set up both IIS and SMTP services viathe Windows control panel. The SMTP service's role is to accept anddeliver the messages using the server's configuration. It maydeliver the messages directly, or utilize a smart host to deliverthe message instead. When a smart host is enlisted, all messagesare forwarded to it for delivery.
A little more information is appropriate fordebugging. The SMTP service uses a directory structure to containmessages prior to delivery with the default directory beingC:\Inetpub\mailroot. It contains numerous subdirectories includingQueue, Drop, and Badmail. If you're unable to configure yourinstance of the SMTP Service for delivery, you can find the messagein an EML file in the Queue subdirectory. The Badmail directorycontains messages that that couldn't be delivered. Now, let's takea look at sending mail messages from your code.
Sending e-mail messages
To compose an e-mail message in your code, youneed to start by creating an instance of the MailMessage class, asshown in the following C# snippet:
MailMessage msg = new MailMessage();
Be sure to include the System.Web.Mailnamespace in your code:
using System.Web.Mail;
Once the object is instantiated, the variousproperties of the MailMessage class are used per your application.The following lines set the recipient, sender, and subject of themessage:
msg.To = "test@test.com";
msg.From = "me@test.com";
msg.Subject = "Test Message";
The next step is setting our mail server viathe SmtpServer object's SmtpServer property:
SmtpMail.SmtpServer = "smtp server name oraddress";
The final step is sending the message bypassing our MailMessage object to the SmtpMail object's Sendmethod:
SmtpMail.Send(msg);
The previous code used C#. Here's a morecomplete listing via a Web form's Page_Load event coded inVB.NET:
Private Sub Page_Load(ByVal sender AsSystem.Object, ByVal e As
System.EventArgs)
Handles MyBase.Load
Dim msg As MailMessage = New MailMessage()
msg.To = "test@test.com"
msg.From = "me@test.com"
msg.Subject = "Test message"
msg.Body = "Message body"
Try
SmtpMail.SmtpServer = " smtp server name or address "
SmtpMail.Send(msg)
Catch ex As HttpException
Response.Write("Error: " + ex.ToString())
Catch ex As Exception
Response.Write("Error: " + ex.ToString())
End Try
End Sub
Notice a try/catch block is used to catch anyexceptions raised during e-mail message composition. The equivalentC# code follows:
private void Page_Load(object sender,System.EventArgs e) {
MailMessage msg = new MailMessage();
msg.To = "test@test.com";
msg.From = "me@test.com";
msg.Subject = "Test message";
msg.Body = "Body of the message";
try {
SmtpMail.SmtpServer = "smtp server name or address";
SmtpMail.Send(msg);
} catch (HttpException ex) {
Response.Write("HTTP Error: " + ex.ToString());
} catch (Exception ex) {
Response.Write("Error: " + ex.ToString());
} }
A straightforward approach to e-mail
The .NET platform makes the task of sendinge-mail messages easier. The System.Web.Mail namespace includeseverything necessary to send these messages--that is, except theactual SMTP server.
You must set up an SMTP server on theapplication's host box, or you must redirect it to the appropriateaddress. IIS provides one approach with its SMTP add-on service,allowing an SMTP server to be set up to handle the messages or toutilize a smarthost indicating what server will handle theprocessing.

No comments:

Post a Comment

Please don't spam, spam comments is not allowed here.

ShibashishMnty
shibashish mohanty