The "system. net. Mail" provided in. NET Framework 2.0 can be easily implemented. This article lists three methods for sending:
1. Use localhost;
2. Use normal SMTP;
3. SMTP over SSL;
For example:
1. Use localhost
Public void sendmaillocalhost () |
{ |
System. net. Mail. mailmessage MSG = new system. net. Mail. mailmessage (); |
MSG. to. Add ("a@a.com "); |
MSG. to. Add ("B @ B .com "); |
/* |
* Msg. to. Add ("B @ B .com "); |
* Msg. to. Add ("B @ B .com "); |
* Msg. to. Add ("B @ B .com"); can be sent to multiple people |
*/ |
MSG. CC. Add ("c@c.com "); |
/* |
* Msg. CC. Add ("c@c.com "); |
* Msg. CC. Add ("c@c.com"); can be copied to multiple people |
*/ |
MSG. From = new mailaddress ("a@a.com", "alphawu", system. Text. encoding. utf8 ); |
/* The preceding three parameters are respectively the sender address (which can be written as needed), sender name, and encoding */ |
MSG. Subject = "this is a test email"; // mail title |
MSG. subjectencoding = system. Text. encoding. utf8; // email title Encoding |
MSG. Body = "Mail content"; // mail content |
MSG. bodyencoding = system. Text. encoding. utf8; // email Content Encoding |
MSG. isbodyhtml = false; // whether the email is an HTML email. |
MSG. Priority = mailpriority. High; // mail priority |
|
Smtpclient client = new smtpclient (); |
Client. Host = "localhost "; |
Object userstate = MSG; |
Try |
{ |
Client. sendasync (MSG, userstate ); |
// You can simply use client. Send (MSG ); |
MessageBox. Show ("sent successfully "); |
} |
Catch (system. net. Mail. smtpexception ex) |
{ |
MessageBox. Show (ex. message, "email sending error "); |
} |
} |
2. Use normal SMTP
Public void sendmailusezj () |
{ |
System. net. Mail. mailmessage MSG = new system. net. Mail. mailmessage (); |
MSG. to. Add ("a@a.com "); |
MSG. to. Add ("B @ B .com "); |
/* |
* Msg. to. Add ("B @ B .com "); |
* Msg. to. Add ("B @ B .com "); |
* Msg. to. Add ("B @ B .com"); can be sent to multiple people |
*/ |
MSG. CC. Add ("c@c.com "); |
/* |
* Msg. CC. Add ("c@c.com "); |
* Msg. CC. Add ("c@c.com"); can be copied to multiple people |
*/ |
MSG. From = new mailaddress ("a@a.com", "alphawu", system. Text. encoding. utf8 ); |
/* The preceding three parameters are respectively the sender address (which can be written as needed), sender name, and encoding */ |
MSG. Subject = "this is a test email"; // mail title |
MSG. subjectencoding = system. Text. encoding. utf8; // email title Encoding |
MSG. Body = "Mail content"; // mail content |
MSG. bodyencoding = system. Text. encoding. utf8; // email Content Encoding |
MSG. isbodyhtml = false; // whether the email is an HTML email. |
MSG. Priority = mailpriority. High; // mail priority |
|
Smtpclient client = new smtpclient (); |
Client. Credentials = new system. net. networkcredential ("username@zj.com", "userpass "); |
// The email address and password registered at zj.com |
Client. Host = "smtp.zj.com "; |
Object userstate = MSG; |
Try |
{ |
Client. sendasync (MSG, userstate ); |
// You can simply use client. Send (MSG ); |
MessageBox. Show ("sent successfully "); |
} |
Catch (system. net. Mail. smtpexception ex) |
{ |
MessageBox. Show (ex. message, "email sending error "); |
} |
} |
The above method is not applicable to all SMTP, and zj.com can be used after testing, but smtp.163.com cannot.
3. SMTP over SSL
Public void sendmailusegmail () |
{ |
System. net. Mail. mailmessage MSG = new system. net. Mail. mailmessage (); |
MSG. to. Add ("a@a.com "); |
MSG. to. Add ("B @ B .com "); |
/* |
* Msg. to. Add ("B @ B .com "); |
* Msg. to. Add ("B @ B .com "); |
* Msg. to. Add ("B @ B .com"); can be sent to multiple people |
*/ |
MSG. CC. Add ("c@c.com "); |
/* |
* Msg. CC. Add ("c@c.com "); |
* Msg. CC. Add ("c@c.com"); can be copied to multiple people |
*/ |
MSG. From = new mailaddress ("a@a.com", "alphawu", system. Text. encoding. utf8 ); |
/* The preceding three parameters are respectively the sender address (which can be written as needed), sender name, and encoding */ |
MSG. Subject = "this is a test email"; // mail title |
MSG. subjectencoding = system. Text. encoding. utf8; // email title Encoding |
MSG. Body = "Mail content"; // mail content |
MSG. bodyencoding = system. Text. encoding. utf8; // email Content Encoding |
MSG. isbodyhtml = false; // whether the email is an HTML email. |
MSG. Priority = mailpriority. High; // mail priority |
|
Smtpclient client = new smtpclient (); |
Client. Credentials = new system. net. networkcredential ("username@gmail.com", "password "); |
// Write your Gmail email address and password. |
Client. Port = 587; // The port used by Gmail |
Client. Host = "smtp.gmail.com "; |
Client. enablessl = true; // encrypted by SSL |
Object userstate = MSG; |
Try |
{ |
Client. sendasync (MSG, userstate ); |
// You can simply use client. Send (MSG ); |
MessageBox. Show ("sent successfully "); |
} |
Catch (system. net. Mail. smtpexception ex) |
{ |
MessageBox. Show (ex. message, "email sending error "); |
} |
} |
Using Gmail to send emails, the success rate is extremely high, and almost all emails can be sent. It is recommended.
Http://www.codeproject.com/useritems/SendMailUsingGmailAccount.asp