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 "); |
} |
} |