日期:2009-06-29  浏览次数:20387 次

SmtpMail and MailMessage : Send mails in .NET
Tools UsedVisual C# .NET
NamespaceSystem.Web.Util
AssemblySystem.Web.dll

The SmtpMail class can be used to send from your C# application. Mail is by default queued on the system, ensuring that the calling program does not block network traffic. The SmtpMail class is defined in the namespace System.Web.Util. You need to call
using System.Web.Util

before you use SmtpMail. This class has only one member function Send. Send sends a mail message. The Send method is overloaded. Either a MailMessage class or four arguments can be passed to the Send message. You can call the Send method in two manners:
SmtpMail.Send(txtFrom.Text, txtTo.Text, txtSubject.Text, txtMessage.Text);

Or, if you don't want to call System.Web.Util.
System.Web.Util.Smptmail.Send(txtFrom.Text, txtTo.Text, txtSubject.Text, txtMessage.Text);

You can call the Send method in two ways.
1. By passing MailMessage as a parameter
public static void Send(MailMessage);

Here MailMessage is a class.
            MailMessage mailMsg = new MailMessage();
            mailMsg .From = "from@fromServer.com";
            mailMsg .To = "to@toServer.com";
            mailMsg .Cc = "cc@ccServer.com"";
            mailMsg .Bcc = "bcc@bccServer.com";
            mailMsg .Subject = "SubjectOfTheMailString";
            mailMsg .Body = "BodyOfTheMailString";
            SmtpMail.Send(mailMsg );

2. Direct method.
public static void Send(string from , string to, string subject, string messageText);
rametersDescription
fromEmail address of sender
toEmail address of recipient
subjectEmail subject line.
messageTextBody of Email message

Example:
SmtpMail.Send("mcb@mindcracker.com", "webmaster@mindcracker.com", "Subject", "Message body");

The MailMessage Class
The MailMessage class can be used to send mails with cc, bcc or attachments. The MailMessage constructor initializes a new instance of the MailMessage class. It doesn't take any parameters.
MailMessage mail = new MailMessage();