SMTP.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.Web.Util;
  12. using System.Net.Mail;
  13. using System.Net;
  14. /// <summary>
  15. /// 邮件发送类
  16. /// </summary>
  17. public class SMTP
  18. {
  19. string Sender = ConfigurationManager.AppSettings["Number"];//发送方邮箱
  20. string pwd=ConfigurationManager.AppSettings["Pwd"];//发送方密码
  21. string To;//接收方邮箱账号
  22. string SmtpServer = ConfigurationManager.AppSettings["Server"];//服务器
  23. /// <summary>
  24. /// 邮件发送初始化
  25. /// </summary>
  26. /// <param name="To"></param>
  27. public SMTP(string To)//第1个参数:接收方邮箱账号
  28. {
  29. this.To = To;
  30. }
  31. /// <summary>
  32. /// 发送邮件
  33. /// </summary>
  34. /// <param name="subject">标题</param>
  35. /// <param name="bodyinfo">内容</param>
  36. /// <returns></returns>
  37. public bool sendemail(string subject,string bodyinfo)
  38. {
  39. bool flag = false;
  40. string formto = Sender;
  41. string to = To;
  42. string content = subject;
  43. string body = bodyinfo;
  44. string name = Sender;
  45. string upass = pwd;
  46. string smtp = SmtpServer;
  47. SmtpClient _smtpClient = new SmtpClient();
  48. _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
  49. _smtpClient.Host = smtp; //指定SMTP服务器
  50. _smtpClient.Credentials = new System.Net.NetworkCredential(name, upass);//用户名和密码
  51. MailMessage _mailMessage = new MailMessage();
  52. //发件人,发件人名
  53. _mailMessage.From = new MailAddress(formto, "工艺品平台密码找回业务");
  54. //收件人
  55. _mailMessage.To.Add(to);
  56. _mailMessage.SubjectEncoding = System.Text.Encoding.GetEncoding("gb2312");
  57. _mailMessage.Subject = content;//主题
  58. _mailMessage.Body = body;//内容
  59. _mailMessage.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312");//正文编码
  60. _mailMessage.IsBodyHtml = true;//设置为HTML格式
  61. _mailMessage.Priority = MailPriority.High;//优先级
  62. _mailMessage.IsBodyHtml = true;
  63. try
  64. {
  65. _smtpClient.Send(_mailMessage);
  66. flag = true;
  67. }
  68. catch (Exception)
  69. {
  70. flag = false;
  71. }
  72. return flag;
  73. }
  74. /// <summary>
  75. /// 邮箱激活
  76. /// </summary>
  77. /// <param name="url"></param>
  78. /// <param name="ID"></param>
  79. /// <returns></returns>
  80. public bool Activation(string url,string ID)
  81. {
  82. bool flag = false;
  83. string formto = Sender;
  84. string to = To;
  85. string content = "工艺品平台:";
  86. string body = "尊敬的"+ID+"用户:请点击些链接激活:";
  87. body += "<a href=" + url +">" + url + "</a>";
  88. string name = Sender;
  89. string upass = pwd;
  90. string smtp = SmtpServer;// "smtp.qq.com";
  91. SmtpClient _smtpClient = new SmtpClient();
  92. _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
  93. _smtpClient.Host = smtp; //指定SMTP服务器
  94. _smtpClient.Credentials = new System.Net.NetworkCredential(name, upass);//用户名和密码
  95. MailMessage _mailMessage = new MailMessage();
  96. //发件人,发件人名
  97. _mailMessage.From = new MailAddress(formto, "工艺品网站平台激活验证");
  98. //收件人
  99. _mailMessage.To.Add(to);
  100. _mailMessage.SubjectEncoding = System.Text.Encoding.GetEncoding("gb2312");
  101. _mailMessage.Subject = content;//主题
  102. _mailMessage.Body = body;//内容
  103. _mailMessage.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312");//正文编码
  104. _mailMessage.IsBodyHtml = true;//设置为HTML格式
  105. _mailMessage.Priority = MailPriority.High;//优先级
  106. _mailMessage.IsBodyHtml = true;
  107. try
  108. {
  109. _smtpClient.Send(_mailMessage);
  110. flag = true;
  111. }
  112. catch (Exception)
  113. {
  114. flag = false;
  115. }
  116. return flag;
  117. }
  118. /// <summary>
  119. ///
  120. /// </summary>
  121. /// <param name="subject"></param>
  122. /// <param name="body"></param>
  123. public void SendMail(string subject,string body)//第一个参数邮箱主题,第二个参数邮箱内容
  124. {
  125. MailAddress from = new MailAddress(this.Sender);
  126. MailAddress to = new MailAddress(this.To);
  127. MailMessage oMail = new MailMessage(from, to);
  128. oMail.Subject = subject;//主题
  129. oMail.Body = body;//内容
  130. oMail.IsBodyHtml = true;
  131. oMail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
  132. oMail.Priority = MailPriority.High;
  133. SmtpClient client = new SmtpClient();
  134. client.Host = SmtpServer; //指定邮件服务器
  135. client.Credentials = new NetworkCredential(this.Sender,this.pwd);//指定服务器邮件,及密码
  136. //发送
  137. try
  138. {
  139. client.Send(oMail); //发送邮件
  140. }
  141. catch //(Exception cp)
  142. {
  143. //发送不成功!
  144. }
  145. oMail.Dispose(); //释放资源
  146. }
  147. }