做这个发送邮件前,先去qq邮箱设置开启smtp服务,如图:记住第一个服务pop3/smtp的授权码。
private void simpleButton3_Click(object sender, EventArgs e) { //发送 SmtpClient client = new SmtpClient("smtp.qq.com"); //设置邮件协议 client.EnableSsl = true; client.UseDefaultCredentials = false;//这一句得写前面 client.DeliveryMethod = SmtpDeliveryMethod.Network; //通过网络发送到Smtp服务器 client.Credentials = new NetworkCredential("000000000@qq.com", "ywufrjewunxfbuuu"); //qq邮箱 和 授权码 MailMessage mmsg = new MailMessage(new MailAddress("000000000@qq.com"), new MailAddress("111111111@qq.com")); //发件人和收件人的邮箱地址 mmsg.Subject = "测试"; //邮件主题 mmsg.SubjectEncoding = Encoding.UTF8; //主题编码 mmsg.Body = "测试"; //邮件正文 mmsg.BodyEncoding = Encoding.UTF8; //正文编码 mmsg.IsBodyHtml = true; //设置为HTML格式 mmsg.Priority = MailPriority.High; //优先级 try { client.Send(mmsg); MessageBox.Show("邮件已发成功"); } catch (Exception ex) { MessageBox.Show(ex.Message); } }