1.添加引用this
using System.Net.Mail;spa
2.發送郵件實現代碼code
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");blog