MailHelper.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net.Mail;
  6. using System.Configuration;
  7. using Bowin.Common.Utility;
  8. using LumiSoft.Net.POP3.Client;
  9. using LumiSoft.Net.Mail;
  10. using LumiSoft.Net.SMTP.Client;
  11. using LumiSoft.Net.AUTH;
  12. using LumiSoft.Net.MIME;
  13. using System.IO;
  14. namespace Bowin.Common.Mail
  15. {
  16. public class MailHelper
  17. {
  18. private static string smtpHost = ConfigurationManager.AppSettings["Mail_SMTPServer"];
  19. private static int? smtpPort = ConfigurationManager.AppSettings["Mail_SMTPPort"].ParseStrTo<int>();
  20. private static bool? smtpIsSSL = ConfigurationManager.AppSettings["Mail_SMTPIsSSL"].ParseStrTo<bool>();
  21. private static string popHost = ConfigurationManager.AppSettings["Mail_Pop3Server"];
  22. private static int? popPort = ConfigurationManager.AppSettings["Mail_Pop3Port"].ParseStrTo<int>();
  23. private static bool? popIsSSL = ConfigurationManager.AppSettings["Mail_Pop3IsSSL"].ParseStrTo<bool>();
  24. private static string userName = ConfigurationManager.AppSettings["Mail_UserName"];
  25. private static string password = ConfigurationManager.AppSettings["Mail_Password"];
  26. private const int _DEFAULT_SMTP_PORT = 25;
  27. private const int _DEFAULT_POP3_PORT = 110;
  28. private static string GetSenderEmail()
  29. {
  30. string hostName = smtpHost.Substring(smtpHost.IndexOf('.') + 1);
  31. return userName + "@" + hostName;
  32. }
  33. public static void SendMail(string receiver, string title, string content)
  34. {
  35. if (string.IsNullOrEmpty(smtpHost))
  36. {
  37. throw new SendMailException("请指定发送邮件的STMP服务器地址。");
  38. }
  39. if (!smtpPort.HasValue)
  40. {
  41. smtpPort = _DEFAULT_SMTP_PORT;
  42. }
  43. if (!smtpIsSSL.HasValue)
  44. {
  45. smtpIsSSL = false;
  46. }
  47. if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
  48. {
  49. throw new SendMailException("请指定发送邮件的账号和密码。");
  50. }
  51. //try
  52. //{
  53. // string sender = GetSenderEmail();
  54. // using (SMTP_Client client = new SMTP_Client())
  55. // {
  56. // var authInfo = new AUTH_SASL_Client_Plain(userName, password);
  57. // client.Connect(smtpHost, smtpPort.Value, smtpIsSSL.Value);
  58. // client.Auth(authInfo);
  59. // Mail_Message message = new Mail_Message();
  60. // message.From = new Mail_t_MailboxList();
  61. // message.From.Add(new Mail_t_Mailbox(sender, sender));
  62. // message.To = new Mail_t_AddressList();
  63. // message.To.Add(new Mail_t_Mailbox(receiver, receiver));
  64. // message.Subject = title;
  65. // //--- multipart/mixed -----------------------------------
  66. // MIME_h_ContentType contentType_multipartMixed = new MIME_h_ContentType(MIME_MediaTypes.Multipart.mixed);
  67. // contentType_multipartMixed.Param_Boundary = Guid.NewGuid().ToString().Replace('-', '.');
  68. // MIME_b_MultipartMixed multipartMixed = new MIME_b_MultipartMixed(contentType_multipartMixed);
  69. // message.Body = multipartMixed;
  70. // //--- multipart/alternative -----------------------------
  71. // MIME_Entity entity_multipartAlternative = new MIME_Entity();
  72. // MIME_h_ContentType contentType_multipartAlternative = new MIME_h_ContentType(MIME_MediaTypes.Multipart.alternative);
  73. // contentType_multipartAlternative.Param_Boundary = Guid.NewGuid().ToString().Replace('-', '.');
  74. // MIME_b_MultipartAlternative multipartAlternative = new MIME_b_MultipartAlternative(contentType_multipartAlternative);
  75. // entity_multipartAlternative.Body = multipartAlternative;
  76. // multipartMixed.BodyParts.Add(entity_multipartAlternative);
  77. // //--- text/plain ----------------------------------------
  78. // MIME_Entity entity_text_plain = new MIME_Entity();
  79. // MIME_b_Text text_plain = new MIME_b_Text(MIME_MediaTypes.Text.plain);
  80. // entity_text_plain.Body = text_plain;
  81. // //普通文本邮件内容,如果对方的收件客户端不支持HTML,这是必需的
  82. // string plainTextBody = "如果你邮件客户端不支持HTML格式,或者你切换到“普通文本”视图,将看到此内容";
  83. // text_plain.SetText(MIME_TransferEncodings.QuotedPrintable, Encoding.UTF8, plainTextBody);
  84. // multipartAlternative.BodyParts.Add(entity_text_plain);
  85. // //--- text/html -----------------------------------------
  86. // string htmlText = content;//"<html>这是一份测试邮件,<img src=\"cid:test.jpg\">来自<font color=red><b>LumiSoft.Net</b></font></html>";
  87. // MIME_Entity entity_text_html = new MIME_Entity();
  88. // MIME_b_Text text_html = new MIME_b_Text(MIME_MediaTypes.Text.html);
  89. // entity_text_html.Body = text_html;
  90. // text_html.SetText(MIME_TransferEncodings.QuotedPrintable, Encoding.UTF8, htmlText);
  91. // multipartAlternative.BodyParts.Add(entity_text_html);
  92. // using (MemoryStream stream = new MemoryStream())
  93. // {
  94. // message.ToStream(stream, new MIME_Encoding_EncodedWord(MIME_EncodedWordEncoding.Q, Encoding.UTF8), Encoding.UTF8);
  95. // stream.Position = 0;
  96. // client.SendMessage(stream);
  97. // }
  98. // if (message != null)
  99. // {
  100. // message.Dispose();
  101. // }
  102. // client.Disconnect();
  103. // }
  104. //}
  105. //catch (Exception ex)
  106. //{
  107. // throw new SendMailException("邮件发送失败,详情请参考InnerException", ex);
  108. //}
  109. MailMessage message = new MailMessage();
  110. message.From = new MailAddress(GetSenderEmail());
  111. message.To.Add(receiver);
  112. message.Subject = title;
  113. message.IsBodyHtml = true;
  114. message.Body = content;
  115. SmtpClient client = new SmtpClient(smtpHost, smtpPort.Value);
  116. client.EnableSsl = smtpIsSSL.Value;
  117. client.Credentials = new System.Net.NetworkCredential(userName, password);
  118. try
  119. {
  120. client.Send(message);
  121. }
  122. catch (Exception ex)
  123. {
  124. throw new SendMailException("邮件发送失败,详情请参考InnerException", ex);
  125. }
  126. }
  127. public static List<Mail_Message> ReceiveMail(string mailAddress)
  128. {
  129. if (string.IsNullOrEmpty(popHost))
  130. {
  131. throw new SendMailException("请指定发送邮件的POP3服务器地址。");
  132. }
  133. if (!popPort.HasValue)
  134. {
  135. popPort = _DEFAULT_POP3_PORT;
  136. }
  137. if (!popIsSSL.HasValue)
  138. {
  139. popIsSSL = false;
  140. }
  141. if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
  142. {
  143. throw new SendMailException("请指定接收邮件的账号和密码。");
  144. }
  145. List<Mail_Message> mailList = new List<Mail_Message>();
  146. using (POP3_Client client = new POP3_Client())
  147. {
  148. client.Connect(popHost, popPort.Value, popIsSSL.Value);
  149. client.Login(userName, password);
  150. POP3_ClientMessageCollection coll = client.Messages;
  151. for (int i = 0; i < coll.Count; i++)
  152. {
  153. POP3_ClientMessage message = coll[i];
  154. Mail_Message mm = Mail_Message.ParseFromByte(coll[i].MessageToByte());
  155. mailList.Add(mm);
  156. }
  157. }
  158. return mailList;
  159. }
  160. }
  161. }