Welcome to the. NET Nuts & Bolts column. In this column, we'll explore how to send e-mail in an application. This will use the class in the System.Web.Mail namespace.
Collaboration Data Objects
The Windows 2000 Collaboration Data Object (CDOSYS) is a message component that Microsoft uses to create and send standards-based e-mail messages. It is a substitute for Collaboration Data Objects (CDONTS) with Windows NT. Although CDONTS is already included in Windows 2000 for backward compatibility reasons, Windows XP, Windows Server 2003, and later versions do not include or support CDONTS components. So any application that uses CDONTS to send messages must migrate to use CDOSYS. It provides the same functionality and is easy to use.
In addition to being an alternative, CDOSYS also introduces some features not in the CDONTS, such as:
The ability to send messages to newsgroups
Control of MIME body structure of message
Receive and forward mechanisms
Transfer event accept pool to respond to events
The System.Web.Mail namespace contains classes that interact with CDOSYS components to create and send information.
Using Internet Information Services (IIS) and the SMTP service
In order to be able to send e-mail messages from the application using CDOSYS, you must confirm that the SMTP service is already installed in the list of IIS services. In Windows 2000/XP, you can set up-> Add/Remove Programs-> Add/Remove Windows component options from the Control Panel. The task of the STMP service is to receive and send messages based on configuration. This service can either post messages directly or use a proxy server to send messages. When the proxy server is configured, all messages are forwarded to it for delivery. You must ensure that the IIS and SMTP services are properly installed and configured.
Before posting, the SMTP service uses a directory structure to hold messages. The default directory is C:\Inetpub\mailroot. This folder contains subdirectories, such as Queue, Drop, and Badmail. If you cannot configure the SMTP service instance to send, you will be able to C:\Inetpub\mailroot\Queue the * in the directory. The message was found in the EML file. This technique is useful when creating messages offline.
Send a message
As mentioned earlier, sending e-mail will be a relatively simple thing to do. Class-System.Web.Mail.MailMessage class represents the message that will be sent. An E-mail message is created by an instance of the class. This class contains attributes such as recipients, senders, and topics that let you control which messages you want to send. You can also use instances of class System.Web.Mail.MailAttachment to create attachments and then add them to the MailMessage Attachments (Attachment) collection. The message is then sent out by the class System.Web.Mail.SmtpMail.
Send mail Sample code
The following C # sample code will contain a Windows console program that demonstrates how to send a simple e-mail message. When the SmtpMail SmtpServer property is not set, the local machine defaults to its configuration. You must ensure that references to System.Web.dll are added because it is a console application and not a asp.net application.
Using System;
Using System.Web.Mail;
Namespace Codeguru.sendmail
{
<summary>
Test Console application to demonstrate sending e-mail.
</summary>
Class Testmail
{
<summary>
The main entry point is for the application.
</summary>
[STAThread]
static void Main (string[] args)
{
Testmail.send ("testuser@codeguru.com",
"Mstrawmyer@crowechizek.com",
"Test message Using CDOSYS",
"Hello world! This is a simple message sent
Using CDOSYS. ");
}
<summary>
Send a message using the. NET Wrapper for collaborative Data
Objects (CDO). This method should is used when sending to a
Single recipient only; Otherwise, the list of recipients
would be known.
</summary>
<param name= "Messagefrom" >message originator</param>
<param name= "Messageto" >message receipent</param>
<param name= "MessageSubject" >message subject</param>
<param name= "MessageBody" >message body</param>
public static void Send (String messagefrom,
String Messageto,
String MessageSubject,
String messagebody)
{
MailMessage message = new MailMessage ();
Message. from = Messagefrom;
Message. to = Messageto;
Message. Subject = MessageSubject;
Message. BodyFormat = Mailformat.text;
Message. BODY = MessageBody;
Send a message sample code with attachments
The following C # sample code will contain a Windows console program that demonstrates how to send an e-mail message with an attachment. This is done by creating an instance of the class messageattachment and then adding it to the Attachments collection.
Using System;
Using System.Web.Mail;
Namespace Codeguru.sendmail
{
<summary>
Console application to demonstrate sending e-mail with an
Attachment.
</summary>
Class Testmail
{
<summary>
The main entry point is for the application.
</summary>
[STAThread]
static void Main (string[] args)
{
Testmail.sendattachment ("testuser@codeguru.com",
"Mstrawmyer@crowechizek.com",
"Test message Using CDOSYS",
"Hello world! This is a simple
Message sent using CDOSYS. ",
"C:\\myattachment.txt");
}
<summary>
Send a message using the. NET Wrapper for collaborative Data
Objects (CDO). This method should is used when sending to
A single recipient only; Otherwise, the list of recipients
would be known.
</summary>
<param name= "Messagefrom" >message originator</param>
<param name= "Messageto" >message receipent</param>
<param name= "MessageSubject" >message subject</param>
<param name= "MessageBody" >message body</param>
<param name= "Messageattachmentpath" >path to Attachment
</param>
public static void Sendattachment (String messagefrom,
String Messageto,
String MessageSubject,
String MessageBody,
String messageattachmentpath)
{
Create and setup The message
MailMessage message = new MailMessage ();
Message. from = Messagefrom;
Message. to = Messageto;
Message. Subject = MessageSubject;
Message. BodyFormat = Mailformat.text;
Message. BODY = MessageBody;
Create and add the attachment
MailAttachment attachment = new
MailAttachment (Messageattachmentpath);
Message. Attachments.Add (attachment);
Possible improvements
We've shown how to send e-mail in different ways. Now it's your turn to figure out how to apply this functionality in your application. Here are some ideas to share with you:
E-mail warning-when a fatal or unrecoverable application error occurs, your application can send e-mail to a specified address to make it known quickly.
Create a web-based Contact Message form-You can enable users to send user feedback by filling out a Web form, and then write a program to send the message to the appropriate contact person.
Subscription service-When sending e-mail through the CDOSYS component to support subscription-type services, you will need to send multiple messages instead of a single message to all recipients. When a message has a large number of recipients, handling all the recipients will dramatically reduce the processing speed. This is a good time to split the list of recipients into multiple lists and send messages several times.
Send messages using Bcc-when you send e-mail through the CDOSYS component to support subscription-type services, you may want to use the BCC instead of to to fill in the recipient's address. This will make it impossible for all recipients to know the list of recipients.
Send HTML message-message body format can be HTML. It enables the message body to be sent in HTML format instead of plain text.
-----------------------------------------------------------
Original:
Sending e-mail with System.Web.Mail
Mark Strawmyer (View profile)
February 9, 2004
Rating:not yet rated
Welcome to the next installment of the. NET Nuts & Bolts column. In this column, we ll explore sending e-mail from within applications. This would involve utilizing classes contained in the System.Web.Mail namespace.
Collaboration Data Objects
Collaboration Data Objects for Windows Watts (CDOSYS) is a Microsoft messaging component This allows for standards-based E Mail messages to is constructed and sent. It is a replacement of the ' collaboration Data Objects for NTS (CDONTS), which, as and can probably guess by the name, For Windows NT. CDONTS are included with Windows? Backwards compatibility, but Windows XP, Windows Server 2003, and Beyond do not include or support the use of CDONTS. Thus, any applications that send messages using CDONTS must is migrated to use CDOSYS. IT provides the same functionality and is just as easy to use.
In addition to serving as a replacement, CDOSYS introduces some new functionality this is not previously available in CDO NTS. Some of the functionality includes:
Ability to post messages to newsgroups
Control ' MIME body structure for messages
Reply and forward functionality
Transport event sink to allow responding to events
The System.Web.Mail namespace contains classes that interact with CDOSYS to construct and send the message (s).
Using IIS and SMTP Service
In order for CDOSYS to send e-mail or other messages from your application, your need to enlist the services of IIS with th E SMTP Service installed. Both are available in Windows 2000/xp through the control Panel-> add/remove Programs-> add/remove windows Compone NTS option. The job of the STMP Service is to accept and deliver the messages, based on the configuration. The service can attempt to deliver the messages directly, or it can utilize a smart host to deliver the message instead. When a smart host are enlisted, all messages are forwarded to it for delivery. You are need to have IIS and the SMTP service installed and configured.
The SMTP Service uses a directory structure to contain messages prior to delivery. The default directory is C:\Inetpub\mailroot. This folder contains a number of subdirectories such as Queue, Drop, and Badmail. If you are are unable to configure your instance of the SMTP Service for delivery, you can find the message in a *. EML file in the C:\Inetpub\mailroot\Queue directory. This technique can is useful when creating messages offline.
Sending a message
As previously mentioned, sending an e-mail is a relatively simple thing todo. The System.Web.Mail.MailMessage class represents is sent. e-mail messages are constructed by using instances of this class. This class is contains properties such as to, from, and Subject that allow the being. Attachments can be created through instances of the System.Web.Mail.MailAttachment and then added to the MailMessage GH the Attachments collection of the MailMessage. The message is then delivered through the System.Web.Mail.SmtpMail class.
Sending a message Sample Code
The following sample code contains a c#-based Windows Console application that shows I-Send an e-mail message. By isn't specifying that's the Smtpmail.smtpserver property isn't set, the localhost is used by default. You are need to make sure to add a reference to the System.Web.dll because this is a console application and not a asp.net AP Plication.
Using System;
Using System.Web.Mail;
Namespace Codeguru.sendmail
{
<summary>
Test Console application to demonstrate sending e-mail.
</summary>
Class Testmail
{
<summary>
The main entry point is for the application.
</summary>
[STAThread]
static void Main (string[] args)
{
Testmail.send ("testuser@codeguru.com",
"Mstrawmyer@crowechizek.com",
"Test message Using CDOSYS",
"Hello world! This is a simple message sent
Using CDOSYS. ");
}
<summary>
Send a message using the. NET Wrapper for collaborative Data
Objects (CDO). This method should is used when sending to a
Single recipient only; Otherwise, the list of recipients
would be known.
</summary>
<param name= "Messagefrom" >message originator</param>
<param name= "Messageto" >message receipent</param>
<param name= "MessageSubject" >message subject</param>
<param name= "MessageBody" >message body</param>
public static void Send (String messagefrom,
String Messageto,
String MessageSubject,
String messagebody)
{
MailMessage message = new MailMessage ();
Message. from = Messagefrom;
Message. to = Messageto;
Message. Subject = MessageSubject;
Message. BodyFormat = Mailformat.text;
Message. BODY = MessageBody;
Sending a attachment Sample Code
The following sample code contains a c#-based Windows Console application that shows how to send an e-mail message, in Cludes an attachment. This is doing by creating instances of the Messageattachment class and then adding them to the message through the Attachme NTS collection.
Using System;
Using System.Web.Mail;
Namespace Codeguru.sendmail
{
<summary>
Console application to demonstrate sending e-mail with an
Attachment.
</summary>
Class Testmail
{
<summary>
The main entry point is for the application.
</summary>
[STAThread]
static void Main (string[] args)
{
Testmail.sendattachment ("testuser@codeguru.com",
"Mstrawmyer@crowechizek.com",
"Test message Using CDOSYS",
"Hello world! This is a simple
Message sent using CDOSYS. ",
"C:\\myattachment.txt");
}
<summary>
Send a message using the. NET Wrapper for collaborative Data
Objects (CDO). This method should is used when sending to
A single recipient only; Otherwise, the list of recipients
would be known.
</summary>
<param name= "Messagefrom" >message originator</param>
<param name= "Messageto" >message receipent</param>
<param name= "MessageSubject" >message subject</param>
<param name= "MessageBody" >message body</param>
<param name= "Messageattachmentpath" >path to Attachment
</param>
public static void Sendattachment (String messagefrom,
String Messageto,
String MessageSubject,
String MessageBody,
String messageattachmentpath)
{
Create and setup The message
MailMessage message = new MailMessage ();
Message. from = Messagefrom;
Message. to = Messageto;
Message. Subject = MessageSubject;
Message. BodyFormat = Mailformat.text;
Message. BODY = MessageBody;
Create and add the attachment
MailAttachment attachment = new
MailAttachment (Messageattachmentpath);
Message. Attachments.Add (attachment);
Possible Enhancements
We have demonstrated how to send e-mail messages in a couple of ways. It is now up to I/ways in which can utilize this functionality within your. Here are some ideas to consider on your own:
E-mail Alerts-when a fatal or unrecoverable application error occurs, your application could e-mail information to a desig nated location So, it is immediately known.
Build a web-based contacts Form-you can allow users to send the customer feedback by filling out a Web form and then Programmat Ically e-mailing it to the appropriate contacts (s).
Subscription service-when sending mail by using CDOSYS for a subscription-type service, your may want to send multiple mess Ages instead of a single and all of the recipients. When a message has too many recipients, it can drastically slow as all of the processing recipients. It is often better to break the list of recipients into multiple lists and send multiple messages.
Send messages using bcc-when sending mail using by CDOSYS for a subscription-type service s using the BCC instead of to. This would keep the list of recipients unknown to all of those that receive it.
Send html-formatted mail-the Message body format can is set to HTML. This would allow the body of the ' message to be sent in HTML format rather than plain text.
Translation experience:
This article describes how to. NET program, including ideas on how to configure IIS and SMTP services, how to send simple messages, and how to use them in your applications. For developers, it's a good article about sending e-mail. In the development of the. NET Bookstore, we can also use email to send feedback to our customers about the book, to provide customers with order services and so on.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.