1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| 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.CC.Add("c@c.com");
msg.From = new MailAddress("a@a.com", "AlphaWu", System.Text.Encoding.UTF8); msg.Subject = "这是测试邮件"; msg.SubjectEncoding = System.Text.Encoding.UTF8; msg.Body = "邮件内容"; msg.BodyEncoding = System.Text.Encoding.UTF8; msg.IsBodyHtml = false; msg.Priority = MailPriority.High; SmtpClient client = new SmtpClient(); client.Credentials = new System.Net.NetworkCredential("username@gmail.com", "password"); client.Port = 587; client.Host = "smtp.gmail.com"; client.EnableSsl = true; object userState = msg; try { client.SendAsync(msg, userState); MessageBox.Show("发送成功"); } catch (System.Net.Mail.SmtpException ex) { MessageBox.Show(ex.Message, "发送邮件出错"); } }
|