1 //简单邮件传输协议类 2 System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(); 3 client.Host = "smtp.163.com";//邮件服务器 4 client.Port = 25;//smtp主机上的端口号,默认是25. 5 client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;//邮件发送方式:通过网络发送到SMTP服务器 6 client.Credentials = new System.Net.NetworkCredential("panthervic", "123456");//凭证,发件人登录邮箱的用户名和密码 7 8 //电子邮件信息类 9 System.Net.Mail.MailAddress fromAddress = new System.Net.Mail.MailAddress("panthervic@163.com", "小明");//发件人Email,在邮箱是这样显示的,[发件人:小明<panthervic@163.com>;] 10 System.Net.Mail.MailAddress toAddress = new System.Net.Mail.MailAddress("43327681@163.com", "小红");//收件人Email,在邮箱是这样显示的, [收件人:小红<43327681@163.com>;] 11 System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage(fromAddress, toAddress);//创建一个电子邮件类 12 mailMessage.Subject = "邮件的主题"; 13 string filePath = Server.MapPath("/index.html");//邮件的内容可以是一个html文本. 14 System.IO.StreamReader read = new System.IO.StreamReader(filePath, System.Text.Encoding.GetEncoding("GB2312")); 15 string mailBody = read.ReadToEnd(); 16 read.Close(); 17 mailMessage.Body = mailBody;//可为html格式文本 18 //mailMessage.Body = "邮件的内容";//可为html格式文本 19 mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;//邮件主题编码 20 mailMessage.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");//邮件内容编码 21 mailMessage.IsBodyHtml = true;//邮件内容是否为html格式 22 mailMessage.Priority = System.Net.Mail.MailPriority.High;//邮件的优先级,有三个值:高(在邮件主题前有一个红色感叹号,表示紧急),低(在邮件主题前有一个蓝色向下箭头,表示缓慢),正常(无显示). 23 try 24 { 25 client.Send(mailMessage);//发送邮件 26 //client.SendAsync(mailMessage, "ojb");异步方法发送邮件,不会阻塞线程. 27 } 28 catch (Exception) 29 { 30 }