MailSender.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.Text;
  3. using System.Xml;
  4. using System.Configuration;
  5. using System.Web;
  6. using System.IO;
  7. using System.Net;
  8. using System.Net.Mail;
  9. namespace Ant.Service.Utilities
  10. {
  11. public class MailSender
  12. {
  13. public static void Send(string server, string sender, string recipient, string subject,
  14. string body, bool isBodyHtml, Encoding encoding, bool isAuthentication, params string[] files)
  15. {
  16. System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(server);
  17. System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(sender, recipient);
  18. message.IsBodyHtml = isBodyHtml;
  19. message.SubjectEncoding = encoding;
  20. message.BodyEncoding = encoding;
  21. message.Subject = subject;
  22. message.Body = body;
  23. message.Attachments.Clear();
  24. if (files != null && files.Length != 0)
  25. {
  26. for (int i = 0; i < files.Length; ++i)
  27. {
  28. Attachment attach = new Attachment(files[i]);
  29. message.Attachments.Add(attach);
  30. }
  31. }
  32. if (isAuthentication == true)
  33. {
  34. smtpClient.Credentials = new NetworkCredential(SmtpConfig.Create().SmtpSetting.User,
  35. SmtpConfig.Create().SmtpSetting.Password);
  36. }
  37. smtpClient.Send(message);
  38. }
  39. public static void Send(string recipient, string subject, string body)
  40. {
  41. Send(SmtpConfig.Create().SmtpSetting.Server, SmtpConfig.Create().SmtpSetting.Sender, recipient, subject, body, true, Encoding.Default, true, null);
  42. }
  43. public static void Send(string Recipient, string Sender, string Subject, string Body)
  44. {
  45. Send(SmtpConfig.Create().SmtpSetting.Server, Sender, Recipient, Subject, Body, true, Encoding.UTF8, true, null);
  46. }
  47. static readonly string smtpServer = System.Configuration.ConfigurationManager.AppSettings["SmtpServer"];
  48. static readonly string userName = System.Configuration.ConfigurationManager.AppSettings["UserName"];
  49. static readonly string pwd = System.Configuration.ConfigurationManager.AppSettings["Pwd"];
  50. static readonly int smtpPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SmtpPort"]);
  51. static readonly string authorName = System.Configuration.ConfigurationManager.AppSettings["AuthorName"];
  52. static readonly string to = System.Configuration.ConfigurationManager.AppSettings["To"];
  53. public void Send(string subject, string body)
  54. {
  55. //List<string> toList = StringPlus.GetSubStringList(StringPlus.ToDBC(to), ',');
  56. //OpenSmtp.Mail.Smtp smtp = new OpenSmtp.Mail.Smtp(smtpServer, userName, pwd, smtpPort);
  57. //foreach (string s in toList)
  58. //{
  59. // OpenSmtp.Mail.MailMessage msg = new OpenSmtp.Mail.MailMessage();
  60. // msg.From = new OpenSmtp.Mail.EmailAddress(userName, authorName);
  61. // msg.AddRecipient(s, OpenSmtp.Mail.AddressType.To);
  62. // //设置邮件正文,并指定格式为 html 格式
  63. // msg.HtmlBody = body;
  64. // //设置邮件标题
  65. // msg.Subject = subject;
  66. // //指定邮件正文的编码
  67. // msg.Charset = "gb2312";
  68. // //发送邮件
  69. // smtp.SendMail(msg);
  70. //}
  71. }
  72. }
  73. public class SmtpSetting
  74. {
  75. private string _server;
  76. public string Server
  77. {
  78. get { return _server; }
  79. set { _server = value; }
  80. }
  81. private bool _authentication;
  82. public bool Authentication
  83. {
  84. get { return _authentication; }
  85. set { _authentication = value; }
  86. }
  87. private string _user;
  88. public string User
  89. {
  90. get { return _user; }
  91. set { _user = value; }
  92. }
  93. private string _sender;
  94. public string Sender
  95. {
  96. get { return _sender; }
  97. set { _sender = value; }
  98. }
  99. private string _password;
  100. public string Password
  101. {
  102. get { return _password; }
  103. set { _password = value; }
  104. }
  105. }
  106. public class SmtpConfig
  107. {
  108. private static SmtpConfig _smtpConfig;
  109. private string ConfigFile
  110. {
  111. get
  112. {
  113. string configPath = ConfigurationManager.AppSettings["SmtpConfigPath"];
  114. if (string.IsNullOrEmpty(configPath) || configPath.Trim().Length == 0)
  115. {
  116. configPath = HttpContext.Current.Request.MapPath("/Config/SmtpSetting.config");
  117. }
  118. else
  119. {
  120. if (!Path.IsPathRooted(configPath))
  121. configPath = HttpContext.Current.Request.MapPath(Path.Combine(configPath, "SmtpSetting.config"));
  122. else
  123. configPath = Path.Combine(configPath, "SmtpSetting.config");
  124. }
  125. return configPath;
  126. }
  127. }
  128. public SmtpSetting SmtpSetting
  129. {
  130. get
  131. {
  132. XmlDocument doc = new XmlDocument();
  133. doc.Load(this.ConfigFile);
  134. SmtpSetting smtpSetting = new SmtpSetting();
  135. smtpSetting.Server = doc.DocumentElement.SelectSingleNode("Server").InnerText;
  136. smtpSetting.Authentication = Convert.ToBoolean(doc.DocumentElement.SelectSingleNode("Authentication").InnerText);
  137. smtpSetting.User = doc.DocumentElement.SelectSingleNode("User").InnerText;
  138. smtpSetting.Password = doc.DocumentElement.SelectSingleNode("Password").InnerText;
  139. smtpSetting.Sender = doc.DocumentElement.SelectSingleNode("Sender").InnerText;
  140. return smtpSetting;
  141. }
  142. }
  143. private SmtpConfig()
  144. {
  145. }
  146. public static SmtpConfig Create()
  147. {
  148. if (_smtpConfig == null)
  149. {
  150. _smtpConfig = new SmtpConfig();
  151. }
  152. return _smtpConfig;
  153. }
  154. }
  155. }