邮件发送是Web开发中常用的功能之一,在asp.net中有多种方法实现该功能,本文分享如何通过SmtpClient进行邮件发送:
首先我们对SmtpClient进行简单封装:
1 public class MailService 2 { 3 private SmtpClient client; 4 5 /// <summary> 6 /// 邮件发送类 7 /// </summary> 8 public MailService() 9 : this(null, 25, false) 10 { 11 } 12 13 /// <summary> 14 /// 邮件发送类 15 /// </summary> 16 /// <param name="host">SMTP服务器地址</param> 17 /// <param name="port">发送端口(一般为25)</param> 18 public MailService(string host, int port) 19 : this(host, port, false) 20 { 21 } 22 23 /// <summary> 24 /// 邮件发送类 25 /// </summary> 26 /// <param name="host">SMTP服务器地址</param> 27 /// <param name="port">发送端口(一般为25)</param> 28 /// <param name="enableSsl">是否启用SSL</param> 29 public MailService(string host, int port, bool enableSsl) 30 { 31 client = new SmtpClient(); 32 client.DeliveryMethod = SmtpDeliveryMethod.Network; 33 if (!string.IsNullOrEmpty(host)) 34 { 35 client.Host = host; 36 } 37 client.Port = port; 38 client.EnableSsl = enableSsl; 39 40 } 41 42 private string _user; 43 /// <summary> 44 /// 登录用户名 45 /// </summary> 46 public string User 47 { 48 get { return _user; } 49 set { _user = value; } 50 } 51 private string _password; 52 /// <summary> 53 /// 密码 54 /// </summary> 55 public string Password 56 { 57 get { return _password; } 58 set { _password = value; } 59 } 60 private string _sendmail; 61 /// <summary> 62 /// 发件人 63 /// </summary> 64 public string SendMail 65 { 66 get { return _sendmail; } 67 set { _sendmail = value; } 68 } 69 private string _errormessage; 70 /// <summary> 71 /// 错误信息 72 /// </summary> 73 public string ErrorMessage 74 { 75 get { return _errormessage; } 76 private set { _errormessage = value; } 77 } 78 /// <summary> 79 /// 发送邮件 80 /// </summary> 81 /// <param name="message">邮件内容</param> 82 /// <returns></returns> 83 public bool Send(MailMessage message) 84 { 85 try 86 { 87 NetworkCredential credential = new NetworkCredential(User, Password); 88 client.Credentials = credential; 89 client.Send(message); 90 return true; 91 } 92 catch (Exception e) 93 { 94 ErrorMessage = e.Message; 95 return false; 96 } 97 } 98 99 /// <summary> 100 /// 发送邮件 101 /// </summary> 102 /// <param name="mail">收件人</param> 103 /// <param name="title">标题</param> 104 /// <param name="body">邮件内容</param> 105 /// <returns></returns> 106 public bool Send(string mail, string title, string body) 107 { 108 109 MailAddress addrFrom = new MailAddress(SendMail, User, Encoding.UTF8); 110 MailAddress addrTo = new MailAddress(mail, mail, Encoding.UTF8); 111 112 using (MailMessage message = new MailMessage(addrFrom, addrTo)) 113 { 114 115 message.Subject = title; 116 message.SubjectEncoding = Encoding.UTF8; 117 118 message.Body = body; 119 message.BodyEncoding = Encoding.UTF8; 120 121 message.IsBodyHtml = true; 122 message.Priority = MailPriority.Normal; 123 124 return Send(message); 125 126 } 127 } 128 }
使用方法:
1 MailService ms = new MailService("smtp.jiniannet.com", 25, false); 2 ms.SendMail = "test@jiniannet.com"; 3 ms.User = "test"; 4 ms.Password = "123456"; 5 ms.Send("admin@jinianet.com", "测试邮件", "极念网(www.jiniannet.com)邮件发送测试!");