Sending Email in .NET

Sending Email in .NET

Author: James Foster Date Posted: 09/05/2001 .NET Version: 1.0.3705
If you've had to send email with regular ASP, you've probably used CDONTS. In .NET, this functionality has been rolled into the Framework, and is part of the System.Web.Mail namespace. Using the classes in this namespace, you can easily construct and send email, with attachments if you wish, using the SMTP service built into Windows.
System.Web.Mail Namespace
The System.Web.Mail namespace is composed of three classes and three enumerations.

Classes:

MailAttachment Represents the attachments of an email.
MailMessage Represents the email itself.
SmtpMail Responsible for sending MailMessages via SMTP.

Enumerations:

MailEncoding Specifies the Encoding of the message: Base64 or UUEncode
MailFormat Specifies the Format of the message: Html or Text
MailPriority Specifies the Priority of the message: High, Medium, or Low.
Constructing the MailMessage
The MailMessage object represents an email. You start by instantiating a MailMessage object, and then set the properties with the details of the email. The following properties are the ones you are most likely to use in your application:

Attachments the list of attachments (represented by MailAttachment objects) that are transmitted with the email
Bcc a semicolon-delimited list of email addresses that receive a Blind Carbon Copy (BCC) copy of the email message
Body the body of the email
BodyFormat specifies the MailFormat of the email
Cc a semicolon-delimited list of email addresses that receive a Carbon Copy (CC) of the email message
From the email address of the sender
Priority specifies the MailPriority of the email
Subject the subject of the email
To the email address of the recipient
UrlContentBase the URL base of all relative URL's used in an Html Body
UrlContentLocation Specifies the Priority of the message: High, Medium, or Low.
Sending with SmtpMail
Once you have constructed your MailMessage object, you use another object, SmtpMail, to actually send it. The most important method on the SmtpMail object is a static method, Send. This method has two variants. One allows you to send a MailMessage object:
SmtpMail.Send(myEmailObject);
Another variant allows you to specify the sender, recipient, subject and the body of an email, and have it sent right away.
SmtpMail.Send(strFrom, strTo, strSubject, strBody);
Example
Now let's look at an example that pulls this all together. We'll start by creating a MailMessage object, setting a few properties, and finally sending the email with the SmtpMail object.

< %@ Page Language = "C#" %>

< script language = "C#" runat = "server">

void Page_Load ( )
{
     // CREATE A MAIL MESSAGE
    System .Web .Mail .MailMessage myEmail = new System .Web .Mail .MailMessage ( ) ;

     // SET MESSAGE PARAMETERS
     myEmail .From = "[email protected]" ;
     myEmail .To = "[email protected]" ;
     myEmail .Subject = "Product Availability Notice" ;
     myEmail .BodyFormat = System .Web .Mail .MailFormat .Html ;
     myEmail .Body = "The sunglasses you expressed interest in are now in stock." ;

     //SEND THE MESSAGE
    System .Web .Mail .SmtpMail .Send ( myEmail ) ;

     //UPDATE STATUS
     lblMailStatus .Text = "Mail successfully sent." ;
}

< / script>

< html>
< body>

< asp :Label id = "lblMailStatus" runat = "server" />

< / body>
< / html>
< %@ Page Language = "VB" %>

< script language = "VB" runat = "server">

Sub Page_Load ( )
     'CREATE A MAIL MESSAGE
     Dim myEmail as System . Web . Mail . MailMessage = new System . Web . Mail . MailMessage ( )

     'SET MESSAGE PARAMETERS
     myEmail . From = "[email protected]"
     myEmail . To = "[email protected]"
     myEmail . Subject = "Product Availability Notice"
     myEmail . BodyFormat = System . Web . Mail . MailFormat . Html
     myEmail . Body = "The sunglasses you expressed interest in are now in stock."

     'SEND THE MESSAGE
     System . Web . Mail . SmtpMail . Send ( myEmail )

     'UPDATE STATUS
     lblMailStatus . Text = "Mail successfully sent."
End Sub

< / script>

< html>
< body>
    < asp : Label id = "lblMailStatus" runat = "server" />
< / body>
< / html>


你可能感兴趣的:(Sending Email in .NET)