最近用到了邮件发送功能,因为stmp设置、参数传递错误等一些问题走了弯路,虽然代码很简单,这里还是记录一下,方便下次查阅。
用个人163邮箱测试的邮件发送
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(); 6 client.Host = "smtp.163.com";//使用163的SMTP服务器发送邮件 7 client.UseDefaultCredentials = true; 8 client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; 9 //这里需要注意,填写的是用户名不带@后面内容的,密码不是邮箱的登录密码,是邮箱的授权码 10 client.Credentials = new System.Net.NetworkCredential("zhangwy", "wy123456"); 11 12 System.Net.Mail.MailMessage Message = new System.Net.Mail.MailMessage(); 13 Message.From = new System.Net.Mail.MailAddress("zhangwy@163.com"); 14 Message.To.Add("1111116738@qq.com"); 15 Message.Body = "测试邮件体"; 16 Message.SubjectEncoding = System.Text.Encoding.UTF8; 17 Message.BodyEncoding = System.Text.Encoding.UTF8; 18 Message.Priority = System.Net.Mail.MailPriority.High; 19 Message.IsBodyHtml = true; 20 client.Send(Message); 21 } 22 }