C#发送邮件
1 public class MailUtil 2 { 3 4 public SmtpClient smtpClient = null;// 设置smtp协议 5 public MailMessage mailMessage_mai = null; //设置邮件信息,要发送的内容 6 7 #region 发邮件 8 /// <summary> 9 /// 发邮件 10 /// </summary> 11 /// <param name="smtp">邮箱服务器名称</param> 12 /// <param name="affix">附件路径</param> 13 /// <param name="from">发件箱地址</param> 14 /// <param name="pwd">发件箱密码</param> 15 /// <param name="to">收件箱地址</param> 16 /// <param name="title">邮件标题</param> 17 /// <param name="body">邮件正文</param> 18 /// <returns></returns> 19 public bool SendMail(string smtp,string affix, 20 string from ,string pwd,string to,string title,string body) 21 { 22 smtpClient = new SmtpClient(); 23 smtpClient .Host = smtp; 24 smtpClient.UseDefaultCredentials = false; 25 26 //指定服务器认证 27 NetworkCredential network = new NetworkCredential(from,pwd); 28 29 //指定发件人信息,包括邮箱地址和密码 30 smtpClient.Credentials = new NetworkCredential(from,pwd); 31 //指定如何发送邮件 32 smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; 33 34 35 //创建mailMessage对象 36 mailMessage_mai = new MailMessage(from,to); 37 mailMessage_mai.Subject = title; 38 39 //设置正文默认格式为html 40 mailMessage_mai.Body = body; 41 mailMessage_mai.IsBodyHtml = true; 42 mailMessage_mai.BodyEncoding = Encoding.UTF8; 43 44 //添加附件 45 if (!string.IsNullOrEmpty(affix)) 46 { 47 Attachment data = new Attachment(affix,MediaTypeNames.Application.Octet); 48 mailMessage_mai.Attachments.Add(data); 49 } 50 51 try 52 { 53 //smtpClient.Send(mailMessage_mai);
54 //发送
55 smtpClient.SendAsync(mailMessage_mai,"000000000"); 56 57 return true;//返回true则发送成功 58 } 59 catch (Exception) 60 { 61 return false;//返回false则发送失败 62 } 63 } 64 #endregion 65 66 }