In. net Framework 2.0 system. net. the new mailmessage and smtpclient classes in the mail namespace make it easy for us to send emails. The smtpclient class provides two synchronous and asynchronous methods for sending emails.
Because it is relatively simple to send emails synchronously, please referCode
Code
Mailmessage mm = New Mailmessage ();
// sender
MM. from = New mailaddress ( " aierong@126.com " , " aierong_126 " );
// recipients (multiple recipients)
MM. to. add ( New mailaddress ( " aierong@qq.com " ));
MM. to. add ( New mailaddress ( " aierong@163.com " ));
// CC)
MM. cc. add ( New mailaddress ( " aierong@qq.com " ));
MM. cc. add ( New mailaddress ( " aierong@qq.com " ));
// BCC)
MM. BCC. add ( New mailaddress ( " 114298@qq.com " ));
MM. BCC. add ( New mailaddress ( " 10249102@qq.com " ));
//Topic
Mm. Subject= "Subject 1 of the email";
//Email body
Mm. Body= "Body";
// attachments (multiple attachments can be added)
MM. attachments. add ( New attachment ( @" D: \ access.zip " ));
MM. attachments. add ( New attachment ( @" D: \ setupreport. msi " ));
smtpclient SC = New smtpclient ();
// set the SMTP host name or IP address
SC. host = " smtp.126.com " ;
string User = " " ;
string password = " " ;
// set creden for Sender authentication
SC. credentials = New system. net. networkcredential (user, password);
SC. Send (mm );
MessageBox. Show ("OK");
If the mail capacity is large, you can set the smtpclient. Timeout attribute (default value: 100 seconds) to avoid timeout.
Of course, we can also use the configuration file to specify the default host, port, credential value, and other attributes of the smtpclient object. See the following configuration
Code
< System.net >
< Mailsettings >
< SMTP >
< Network Password = "" Username = "" Host = "Smtp.126.com" />
</ SMTP >
</ Mailsettings >
</ System.net >
Send mail asynchronously:
The above Code is based on the synchronous sending method. The smtpclient. sendasync method can be used to conveniently send mails asynchronously.
The sendasync method is described as follows:
Public void sendasync (
Mailmessage message,
Object usertoken
)
Parameters
Message
The mailmessage that contains the message to be sent.
Usertoken
A user-defined object that is passed to the method called when asynchronous operations are completed.
Code
Code
Mailmessage mm = New Mailmessage ();
// sender
MM. from = New mailaddress ( " aierong@126.com " , " aierong_126 " );
//Recipients (can be multiple recipients)
Mm. to. Add (NewMailaddress ("Aierong@qq.com"));
//Topic
Mm. Subject= "Subject 1 of the email";
//Email body
Mm. Body= "Body";
//Attachment (can be multiple)
Mm. attachments. Add (NewAttachment (@"D: \ access.zip"));
smtpclient SC = New smtpclient ();
// smtpclient. sendcompleted event, called when the asynchronous email sending operation is complete
SC. sendcompleted += New sendcompletedeventhandler (SC _sendcompleted );
//Set the SMTP host name or IP address
SC. Host= "Smtp.126.com";
String User = " Aierong " ;
String Password = " Aie100le " ;
// Sets the creden used to authenticate the sender.
SC. Credentials = New System. net. networkcredential (user, password );
// Send mail Asynchronously
SC. sendasync (mm, " Test message " );
Void SC _sendcompleted ( Object Sender, asynccompletedeventargs E)
{
If (E. Error = Null )
{
MessageBox. Show ( " Sent successfully " );
// The User-Defined Object "Test message" is displayed"
MessageBox. Show (E. userstate. tostring ());
}
Else
{
MessageBox. Show (E. Error. Message );
}
}
Http://aierong.cnblogs.com
SQL server2005 Transact-SQL new weapon learning Summary-Summary
Backup and restoration of stored procedures in ms SQL database (enhanced version)
SQL Server's Distributed Query Essays (using sp_addmediaserver and sp_addmediasrvlogin)
Asp. summary of net2.0 internationalization/Localization Application Implementation (multi-language, multi-cultural page implementation)
WAP development data station (latest update)
Custom Format String essay (iformattable, iformatprovider, implementation of icustomformatter three interfaces)
asynchronous programming of mcad learning notes (asynccallback delegation, iasyncresult interface, begininvoke method, and endinvoke method usage summary)
mcad learning notes: class methods, attention, fields, and indexers called through reflection (2 methods)
serialization of mcad learning notes (binary and soap serialization)
delegate re-understanding of mcad learning notes (Delegate constructor, begininvoke, endinvoke, invoke4 methods)
winform development, summary of information about form display and form value passing
Microsoft Windows Service for mcad Study Notes
copy all the objects and files under a certain category to the target category (Object quota quota)
Asp. net status management (Summary)