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 ()
{< br> 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 Code
MSG. body = "email content"; // email 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 ()
{< br> 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 Code
MSG. body = "email content"; // email 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.
C # send mail http://www.legalsoft.com.cn/docs/docs/17/577.html requiring SMTP authentication. Net C # send mail content embedded picture http://hi.baidu.com/lovevc2008/blog/item/831f036cf8c5c8f34216945e.html http://space.itpub.net/12639172/viewspace-490574 http://blog.csdn.net/donny_zhang/archive/2009/01/13/3769454.aspx http://www.codesoso.com/code/smtp-email-sender-net.aspx instance source code http://hi.baidu.com/huqing7002/blog/item/1d6aa2d6c8f6ce2806088b17.html