Recently, due to the needs of an R & I project, users require that the system send an email reminder or send an email every Monday to automatically collect data, therefore, I also found relevant materials and wrote a Demo to share with you, so that everyone can learn together.
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:
[Html]
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 ");
}
}
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 (master@boys90.com, "dulei", 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. The common smtp c # code is as follows:
[Html]
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 ("master@boys90.com", "dulei", 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 ("dulei@71info.com", "userpass ");
// The email address and password registered at 71info.com
Client. Host = "smtp.71info.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 ");
}
}
3. SMTP over SSL
[Html]
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 ("boys90.com", "dulei", 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 ("boys90com@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 to use the above methods. I think it is enough for our development. As for the Demo I have prepared, I will share it with you.
My independent blog, 90 boys, enjoy the Internet. Welcome to join us to learn more, share with us, and live a happy life !!
From Share Network-shi's blog