1 using System.Net; 2 using System.Net.Mail; 3 namespace ClassLibrary 4 { 5 /// <summary> 6 /// 发送邮件 7 /// </summary> 8 public static class Eail 9 { 10 11 #region 发送邮件 12 /// <summary> 13 /// 发送邮件 14 /// </summary> 15 /// <param name="toMail">接受邮箱地址</param> 16 /// <param name="toName">接受人</param> 17 /// <param name="Subject">邮件主题</param> 18 /// <param name="Body">邮件内容</param> 19 /// <param name="IsBodyHtml">是否是HTMLtrue/false</param> 20 /// <param name="Priority">邮件优先级</param> 21 public static void SendEail(string toMail, string toName, string Subject, string Body, bool IsBodyHtml = true, MailPriority Priority = MailPriority.Normal) 22 { 23 SmtpClient client = new SmtpClient(); 24 client.Host = "smtp.163.com";//邮件服务器 25 client.Port = 25;//端口号 26 client.DeliveryMethod = SmtpDeliveryMethod.Network;//邮件发送方式:通过网络发送到SMTP服务器 27 client.Credentials = new NetworkCredential("", "");//凭证,发件人登录邮箱的用户名和密码 28 29 //电子邮件信息类 30 MailAddress fromAddress = new MailAddress("@163.com", "");//发件人Email,在邮箱是这样显示的 31 MailAddress toAddress = new MailAddress(toMail, toName);//收件人Email,在邮箱是这样显示的 32 MailMessage mailMessage = new MailMessage(fromAddress, toAddress);//创建一个电子邮件类 33 mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;//邮件主题编码 34 mailMessage.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");//邮件内容编码 35 mailMessage.Subject = Subject;//主题 36 mailMessage.Body = Body;//可为html格式文本 37 mailMessage.IsBodyHtml = IsBodyHtml;//邮件内容是否为html格式 38 mailMessage.Priority = MailPriority.Normal;//邮件的优先级,有三个值:高(在邮件主题前有一个红色感叹号,表示紧急),低(在邮件主题前有一个蓝色向下箭头,表示缓慢),正常(无显示). 39 client.Send(mailMessage); 40 } 41 #endregion 42 43 } 44 45 } 46 47 代码