来一波C#发送邮件

时间:2021-08-03 07:14:11

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Net.Mail; using System.Net.Mime; using System.Net; using System.IO; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //基于面向人工智能设计 private void Form1_Load(object sender, EventArgs e) { } private void btnOk_Click(object sender, EventArgs e) { if (yes.Text == "" || txtTestName.Text=="") { MessageBox.Show("你没有输入任何内容!"); }else try { //确定smtp服务器地址。实例化一个Smtp客户端 System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.qq.com", 25);//pop.exmail.qq.com //smtp.qq.com client.EnableSsl = true;//指定 SmtpClient 使用安全套接字层 (SSL) 加密连接 //生成一个发送地址 string strFrom = "5160665XX@qq.com"; //构造一个发件人地址对象 MailAddress from = new MailAddress(strFrom, "天意", Encoding.UTF8); //构造一个收件人地址对象 MailAddress to = new MailAddress(txtTestName.Text, "随缘", Encoding.UTF8); //构造一个Email的Message对象 MailMessage message = new MailMessage(from, to); //添加邮件主题和内容 message.Subject = "主题:Smina用户测试!" ; message.SubjectEncoding = Encoding.UTF8; message.Body = "内容:测试邮件" + yes.Text; //得到用户输入的文本 即发送的内容 message.BodyEncoding = Encoding.UTF8; //设置邮件的信息 client.DeliveryMethod = SmtpDeliveryMethod.Network; message.BodyEncoding = System.Text.Encoding.UTF8; message.IsBodyHtml = true; //设置用户名和密码。 client.UseDefaultCredentials = false; string username = "5160665XX@qq.com"; //你的邮箱账号 string passwd = "ipzuuXXXXXbjjb";//你的授权码,你的密码无法登陆,,授权码为如esscncukoftqe****的 一串序号 申请地址https://mail.qq.com/ //用户登陆信息 NetworkCredential myCredentials = new NetworkCredential(username, passwd); client.Credentials = myCredentials; //发送邮件 client.Send(message); MessageBox.Show("邮件发送成功,感谢你的支持!"); } catch { MessageBox.Show("邮件发送失败"); } } } }