using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Net.Mail; using System.Text; namespace Test { public static class SmtpTest { /// <summary> /// 如果企业QQ邮箱设置了微信授权登陆的,则发送失败 /// </summary> public static void SendEmail() { string sender = "121212121212@qq.com"; string pwd = "***********"; var message = new MailMessage(); message.From = new MailAddress(sender, "寄件人显示名称"); message.To.Add(new MailAddress("111111@qq.com","收件人显示名称")); message.Subject = "测试"; message.Body = "测试"; message.IsBodyHtml = false; using (var smtpClient = new SmtpClient()) { //之前遇到过一个坑,公司运维为了安全设置了企业QQ邮箱微信授权登陆的 //后来偶然发现自动发送邮件的服务总是发送不成功 //查询了发久没有找到原因,偶然把这个设置关闭了,才发送成功 smtpClient.UseDefaultCredentials = false; smtpClient.Host = "smtp.exmail.qq.com"; smtpClient.Port = 25;//生产环境推荐使用:465 smtpClient.EnableSsl = false;//生产环境推荐使用:true smtpClient.Credentials = new NetworkCredential(sender, pwd); smtpClient.Send(message); } } } }