From: http://www.alixixi.com/program/a/2009062656457.shtml
Today, I collected some information about sending emails using. net. I 'd like to share it with you! In.. net ,. net comes with the mail sending function, which has been encapsulated in vs2003.. NET Framework system. web. in the namespace of mail, in vs2005, more is the use of system. net. the mail namespace is now available.
Using the classes in this namespace, you can easily build a mail-sendingProgramAll you need is to build an SMTP server in windows.
System. Web. Mail namespace:
The naming control contains the following objects and three attributes:
Objects included:
Mailattachment: Object Class Related to email attachments
Mailmessage: Email Subject
Smtpmail: SMTP protocol used to send emails.
Attribute list:
Mailencoding: email encoding (base64, uuencode)
Mailformat: Mail format (HTML hypertext format, text plain text format)
Mailpriority: Mail priority (high, medium, low)
Construct a mailmessage object:
The mailmessage object is the subject of the mail. Generally, you can build the mailmessage object first, and then set its attributes to build the mail program. The following lists some common attributes:
Attachments: email attachment
BCC: Dark delivery address
Body: Email Subject
Bodyformat: email format (HTML, text)
Cc: CC address
From: sender address
Priority: Mail priority (high, medium, low)
Subject: Email Subject
To: recipient's address
Urlcontentbase: URL encoding method in HTML-format mail
Urlcontentlocation: Priority of mail Information (high, medium, low)
Use smtpmail to send emails
After creating the mailmessage object, you also need to use another object-smtpmail-to send emails. smtpmail has an important method: Send, which has two different usage methods, one of them can only send the entire mailmessage object:
Smtpmail. Send (myemailobject );
You can specify the sender, email address, email subject, and email subject respectively, and then send them out:
Smtpmail. Send (strfrom, strto, strsubject, strbody );
Example program:
Let's take a look at the complete example below. In this example, we use system. web. in the mail namespace, we first create a mailmessage object, then set some attributes, and finally use the smtpmail object to send it out:
Protected void page_load (Object sender, eventargs E)
{
// Create a mail message
System. Web. Mail. mailmessage myemail = new system. Web. Mail. mailmessage ();
// Set message Parameters
Myemail. From = "chenjun@webjx.com ";
Myemail. To = "admin@webjx.com ";
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 .";
}
<HTML>
<Body>
<Asp: Label id = "lblmailstatus" runat = "server"/>
</Body>
</Html>
System. net. Mail namespace:
The naming control contains the following main objects and attributes:
Objects included:
Mailaddress: the address of the sender or recipient of the email.
Attachment: the attachment of the email.
Mailaddresscollection: stores the email addresses associated with emails.
Mailmessage: an email that can be sent using the smtpclient class
Smtpclient: allows applications to send emails using Simple Mail Transfer Protocol (SMTP.
Attribute list:
Deliverynotificationoptions: Specifies the Email Delivery Notification option.
Mailpriority: Specifies the priority of mailmessage.
Smtpaccess: Specifies the allowed access level for Simple Mail Transfer Protocol (SMTP) servers
Smtpdeliverymethod: Specifies how to send an email
Smtpstatuscode: Specifies the result of sending an email using the smtpclient class
Construct a mailmessage object:
The mailmessage object is the subject of the mail. Generally, you can build the mailmessage object first, and then set its attributes to build the mail program. The following lists some common attributes:
Attachments: email attachment
BCC: Dark delivery address
Body: Email Subject
Cc: CC address
From: sender address
Subject: Email Subject
To: recipient's address
Use smtpclient to send emails
After creating the mailmessage object, you also need to use another object-smtpclient-to send emails. smtpclient has a very important method: Send, which can send the entire mailmessage object:
Smtpclient. Send (mailmessage );
In this example, use the system. net. Mail namespace
Static string strhost = string. empty;
Static string straccount = string. empty;
Static string strpwd = string. empty;
Static string strfrom = string. empty;
/// <Summary>
/// Send an email
/// </Summary>
/// <Param name = "to"> recipient's email address </param>
/// <Param name = "title"> email title </param>
/// <Param name = "content"> body content </param>
Public static bool Sendmail (string to, String title, string content)
{
Strhost = "smtp.webjx.com"; // stmp server address
Straccount = "abc@webjx.com"; // SMTP service account
Strpwd = "password"; // SMTP service Password
Strfrom = "chenjun@webjx.com"; // the sender's email address
smtpclient _ smtpclient = new smtpclient ();
_ smtpclient. deliverymethod = smtpdeliverymethod. network; // specify the email sending method
_ smtpclient. host = strhost; // specify the SMTP server
_ smtpclient. credentials = new system. net. networkcredential (straccount, strpwd); // user name and password
mailmessage _ mailmessage = new mailmessage (strfrom, to);
_ mailmessage. subject = title; // subject
_ mailmessage. body = content; // content
_ mailmessage. bodyencoding = system. text. encoding. utf8; // body encoding
_ mailmessage. isbodyhtml = true; // set to HTML format
_ mailmessage. priority = mailpriority. high; // priority
Try
{
_ Smtpclient. Send (_ mailmessage );
Return true;
}
Catch
{
Return false;
}
}