记住端口和SMTP服务器地址(post:25,url:smtp.qq.com)
SendMail.cs
1 using System; 2 using System.Configuration; 3 using System.Net.Mail; 4 using System.Net; 5 6 namespace ParcelDistribution.BLL 7 { 8 public class SendMail 9 { 10 private readonly SmtpClient _smtp; 11 private MailMessage _objMailMessage; 12 private static readonly string UserMail = ConfigurationSettings.AppSettings["UserMail"]; 13 private static readonly string Password = ConfigurationSettings.AppSettings["PassWord"]; 14 public SendMail() 15 { 16 _smtp = new SmtpClient 17 { 18 EnableSsl = false, 19 Host = ConfigurationSettings.AppSettings["mailsmtp"], 20 Port = int.Parse(ConfigurationSettings.AppSettings["port"]), 21 UseDefaultCredentials = false, 22 Credentials = new NetworkCredential(UserMail, Password) 23 }; 24 } 25 /// <summary> 26 /// 发送邮件 27 /// </summary> 28 /// <param name="title">主题</param> 29 /// <param name="body">内容</param> 30 /// <param name="desmail">目标邮箱</param> 31 public bool SendMailtoDes(string title, string[] body, string[] desmail) 32 { 33 _smtp.UseDefaultCredentials = false; 34 _smtp.Credentials = new NetworkCredential(UserMail, Password); 35 36 for (int i = 0; i < desmail.Length; i++) 37 { 38 if (desmail[i] == null || desmail[i] == "") continue; 39 _objMailMessage = new MailMessage {Priority = MailPriority.Normal, From = new MailAddress(UserMail)}; 40 _objMailMessage.To.Add(new MailAddress(desmail[i])); 41 _objMailMessage.IsBodyHtml = true; 42 _objMailMessage.Subject = title; 43 _objMailMessage.Body = body[i]; 44 try 45 { 46 _smtp.Send(_objMailMessage); 47 } 48 catch (Exception) 49 { 50 return false; 51 } 52 } 53 return true; 54 55 } 56 57 } 58 }
web.config配置(像数据库那样配置)
<appSettings> <add key="UserMail" value="595806165@qq.com"/> <add key="PassWord" value="@zhangweiqq"/> <add key="mailsmtp" value="smtp.qq.com" /> <add key="port" value="25"/> </appSettings>
qq邮箱配置(运行过程中会提醒你配置的):
ok,运行下,发送成功!