c#基于System.Net.Mail实现发送邮件功能
1.添加引用
using System.Net.Mail;
2.发送邮件实现代码
private void btnsend_Click(object sender, EventArgs e) { try { var senduser = ConfigurationManager.AppSettings["senduser"];// "test@qq.com"; var sendpwd = ConfigurationManager.AppSettings["sendpwd"];// "test"; var receiver = this.txtreceiver.Text; var sendmsg = this.txtsendmsg.Text; var message = new MailMessage(); var fromddress = new MailAddress(senduser); message.From = fromddress; message.To.Add(receiver);//接收方 message.CC.Add(receiver);//抄送方 message.Subject = "test";//主题 message.Body = sendmsg;//内容 message.Attachments.Add(new Attachment("test.txt"));//附件 SmtpClient client = new SmtpClient("smtp.qq.com", 25);//基于qq邮件发送 client.Credentials = new NetworkCredential(senduser, sendpwd); client.EnableSsl = true; client.Send(message); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
--qq邮件要求:
//验证发件人身份(发件人的邮箱,邮箱里的生成授权码);
client.Credentials = new NetworkCredential("123456@qq.com", "fnsedjxib");