Sending e-mail with attachments from an ASP.NET page
2005-02-03 21:02
651 查看
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 :
High-end commercial email component :
牋 i. AspEmail : Most feature-rich mailing component. Supports message queueing, embedded images, secure mail, authentication. Read more.
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 :
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);
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. |
牋 i. AspEmail : Most feature-rich mailing component. Supports message queueing, embedded images, secure mail, authentication. Read more.
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. |
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);
相关文章推荐
- Redirecting to custom 401 page when "Access denied" occures within an ASP.NET application with Windows authentication
- Embed client-side JavaScript in an ASP.NET 1.1 web page from server side code
- Exchange Server Mail server with asp.NET 1st step
- Navigating Away from a Page that has Changes, in ASP.NET
- Generate PDF documents from a HTML page using ASP.NET
- 讲座展示:Live From Redmond: Microsoft AJAX Patterns - Implementing Predictive Fetch with Microsoft ASP.NET 2.0 AJAX Extensions
- Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application
- ASP.NET: Dispaly data from an Excel Spreadsheet
- ASP.NET: Dispaly data from an Excel Spreadsheet
- [ZT]Submitting Web Form data from one ASP.NET page to another
- EF Core使用SQL调用返回其他类型的查询 ASP.NET Core 2.0 使用NLog实现日志记录 CSS 3D transforms cSharp:use Activator.CreateInstance with an Interface? SqlHelper DBHelper C# Thread.Abort方法真的让线程停止了吗? 注意!你的Thread.Abort方法真
- ASP.NET: System.BadImageFormatException: An attempt was made to load a program with an incorrect format. on Win7 64bit
- Security Measure To Prevent URL Parameter Forgery- An Example with ASP.Net MVC
- asp.net配置报错 “An error was encountered. Please return to the previous page and try again. ”
- ASP.NET MVC- VIEW Creating Page Layouts with View Master Pages Part 4
- Cross-Page WebPart Connections with ASP.NET 2.0
- ASP.NET: Dispaly data from an Excel Spreadsheet
- ASP.NET: Dispaly data from an Excel Spreadsheet
- ASP.NET MVC - The view must derive from WebViewPage, or WebViewPage<TModel>
- Login control in an ASP.NET AJAX toolkit PopupControlExtender with a close button--write by Laurent Kempé