Signup · Login
Stardeveloper.com  
Home · Articles · Forums · Advertise · Contact
Search this Website
Stay Informed
Enter your email address below to be informed every time a new article is posted at Stardeveloper.com:
Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (29)
  ADO (16)
  ADO.NET (10)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Product Reviews (1)
  Applets (1)
  Patterns (1)
Latest Forum Activity
Re: Preventing/Investigating the Source of SQ..
by Faisal Khan on 25 Nov 2008 Go To Post

Re: Column multiplication..
by Faisal Khan on 24 Nov 2008 Go To Post

Re: Unable to insert data in an Access databa..
by Faisal Khan on 12 Nov 2008 Go To Post

Re: HOW TO MOVE FROM APACHE TO MS IIS
by Faisal Khan on 12 Nov 2008 Go To Post

Re: Preventing/Investigating the Source of SQ..
by peeru999 on 8 Oct 2008 Go To Post

Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : .NET : ASP.NET : Sending e-mail with attachments from an ASP.NET page
 
Sending e-mail with attachments from an ASP.NET page
by Sriram Vaideeswaran.

Overview :
The task of sending e-mails has become much easier in ASP.NET. In previous versions of ASP we either used a third party component or the limited functionality of CDONTS component for sending emails. The .NET framework provides simpler but powerful class libraries for sending emails. These classes are in the System.Web.Mail Namespace. Following table lists the classes and their use :

Class Purpose
MailAttachment To construct an email attachment.
MailMessage To construct an email message.
SmtpMail To send an email attachment using the SMTP mail service.

In order to access these classes we have to import the System.Web.Mail namespace within our ASP.NET page using the import directive as shown below :

<% @ Import NameSpace = "System.Web.Mail" %>

Then we construct an email message by creating an Instance of the MailMessage class as shown below :

MailMessage MyMail = new MailMessage();

The MailMessage class has various properties which need to be set before sending emails. Following table shows some of the important properties of the MailMessage class :

Property Description
Attachments List of attachments that is sent with the email.
Body Body of the email message.
From Email address of the sender.
Subject Subject line of the email message.
To Email address of the recipient.

To send an attachment with email we need to create an instance of the MailAttachment class. We pass the full path of the file to be attached as an argument to the constructor of MailAttachment class as shown below :

MailAttachment MyAttachment = new MailAttachment("C:\\My Folder\\MyFile.txt");

Note : The file name passed should be valid file which exists on the server.

This MailAttachment instance is then attached to the mail by using the Attachment property of the Mail class as shown below :

MyMail.Attachments.Add(MyAttachment);

Finally we send the email using the Send() method of the SmtpMail Class as shown below :

SmtpMail.Send(MyMail);

 ( No Further Pages )

Download Associated Files
0032.zip

Related Articles
  1. File Uploading in ASP.NET Using C#
  2. Your first ASP.NET page
  3. Browser Capabilities Component in ASP.NET
  4. Sending E-Mails using ASP.NET
  5. A Preview of Active Server Pages+
  6. Exposing Web Services
  7. Get Detailed Information About Your Site Visitors In Real Time using ASP.NET
  8. ASP.NET Website Programming: Problem - Design - Solution : Deploying the Site
  9. How to determine what server is given web site running on using ASP.NET?
  10. Sending Mass E-Mails using ASP.NET

Comments/Questions ( Threads: 3, Comments: 7 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. Sending mails with embedded images ( 1 Reply )
  2. Could not access 'CDO.Message' object ( 1 Reply )
  3. Same functionality in VB.NET ( 2 Replies )

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed.

 
© 1999 - 2008 Stardeveloper.com, All Rights Reserved.