EmailHelper.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using FluentEmail.Core;
  7. using System.Net;
  8. using System.Net.Mail;
  9. using FluentEmail.Smtp;
  10. using FluentEmail.Core.Models;
  11. using System.IO;
  12. using log4net;
  13. namespace Ropin.Inspection.Common.Helper
  14. {
  15. public class EmailHelper
  16. {
  17. private static readonly ILog log = LogManager.GetLogger(typeof(EmailHelper));
  18. /// <summary>
  19. /// 发送邮件
  20. /// </summary>
  21. /// <returns></returns>
  22. public static bool SendEmail(string toSomeOne, string subject, string title, string conent)
  23. {
  24. bool result=false;
  25. result=SendEmailFun(toSomeOne, subject, title, conent,"yan-daniu@qq.com", "xdzwhrasuhrmbhad");
  26. if(!result)
  27. {
  28. result = SendEmailFun(toSomeOne, subject, title, conent, "154817501@qq.com", "aocrnaezwfygbhbf");
  29. }
  30. return result;
  31. }
  32. /// <summary>
  33. /// 发送邮件
  34. /// </summary>
  35. /// <returns></returns>
  36. private static bool SendEmailFun(string toSomeOne,string subject,string title, string conent,string emails,string paw)
  37. {
  38. //MailAddress mailfrom = new MailAddress(from, senderDisplayName, encoding);//发件人邮箱地址,名称,编码UTF8
  39. //MailAddress mailto = new MailAddress(tomail, recipientsDisplayName, encoding);//收件人邮箱地址,名称,编码UTF8
  40. //MailMessage message = new MailMessage(mailfrom, mailto);//创建mailMessage对象
  41. log.Info($"发送邮件-BEGIN 【emails={emails}】【toSomeOne={toSomeOne};subject={subject};title={title};conent={conent}】");
  42. try
  43. {
  44. SmtpClient smtp = new SmtpClient
  45. {
  46. //smtp服务器地址
  47. EnableSsl = true,//启用ssl
  48. Host = "smtp.qq.com",//
  49. Port = 587,//25,465
  50. UseDefaultCredentials = false,//是否使用默认凭据
  51. DeliveryMethod = SmtpDeliveryMethod.Network,
  52. //这里输入你在发送smtp服务器的用户名和密码
  53. //Credentials = new NetworkCredential("154817501@qq.com", "aocrnaezwfygbhbf")
  54. //Credentials = new NetworkCredential("yan-daniu@qq.com", "xdzwhrasuhrmbhad")
  55. Credentials = new NetworkCredential(emails, paw)
  56. };
  57. //设置默认发送信息
  58. Email.DefaultSender = new SmtpSender(smtp);
  59. var email = Email
  60. //发送人
  61. .From(emails, "环保数字")
  62. //收件人
  63. .To(toSomeOne)
  64. //抄送人
  65. //.CC("154817501@qq.com")
  66. //邮件标题
  67. .Subject(subject)
  68. //邮件内容
  69. // .Body("邮件内容");
  70. .Body("<h1 align=\"center\">"+ title + "</h1><p>"+ "</p>" + conent, true);
  71. //依据发送结果判断是否发送成功
  72. log.Info($"发送邮件-Send 【emails={emails}】【toSomeOne={toSomeOne};subject={subject};title={title};conent={conent}】");
  73. var result = email.Send();
  74. //或使用异步的方式发送
  75. //await email.SendAsync();
  76. if (result.Successful)
  77. {
  78. //发送成功逻辑
  79. log.Info($"发送邮件-SendEND 【emails={emails}】【toSomeOne={toSomeOne};subject={subject};title={title};conent={conent}】");
  80. return true;
  81. }
  82. else
  83. {
  84. log.Info($"发送邮件-发送失败 【emails={emails}】【toSomeOne={toSomeOne};subject={subject};title={title};conent={conent} ;ErrorMessages={result.ErrorMessages}】");
  85. //发送失败可以通过result.ErrorMessages查看失败原因
  86. return false;
  87. }
  88. }
  89. catch (Exception ex)//SmtpException
  90. {
  91. log.Info($"发送邮件异常:{ex.Message}【emails={emails}】【toSomeOne={toSomeOne};subject={subject};title={title};conent={conent}】");
  92. return false;
  93. }
  94. }
  95. public static bool SendEmail(List<string> toSomeOne, string subject, string title, string conent, string attachmentName, string attachmentType, Stream attachmentContent)
  96. {
  97. bool result = false;
  98. result = SendEmailFun(toSomeOne, subject, title, conent, attachmentName, attachmentType, attachmentContent, "yan-daniu@qq.com", "xdzwhrasuhrmbhad");
  99. if (!result)
  100. {
  101. result = SendEmailFun(toSomeOne, subject, title, conent, attachmentName, attachmentType, attachmentContent, "154817501@qq.com", "aocrnaezwfygbhbf");
  102. }
  103. if (!result)
  104. {
  105. throw new Exception("发送邮件失败");
  106. }
  107. return result;
  108. }
  109. private static bool SendEmailFun(List<string> toSomeOne, string subject, string title, string conent, string attachmentName, string attachmentType, Stream attachmentContent, string emails, string paw)
  110. {
  111. try
  112. {
  113. SmtpClient smtp = new SmtpClient
  114. {
  115. //smtp服务器地址
  116. EnableSsl = true,//启用ssl
  117. Host = "smtp.qq.com",//smtp.qq.com
  118. Port = 587,//25,465
  119. UseDefaultCredentials = false,//是否使用默认凭据
  120. DeliveryMethod = SmtpDeliveryMethod.Network,
  121. //这里输入你在发送smtp服务器的用户名和密码
  122. //Credentials = new NetworkCredential("154817501@qq.com", "aocrnaezwfygbhbf")
  123. //Credentials = new NetworkCredential("yan-daniu@qq.com", "xdzwhrasuhrmbhad")
  124. Credentials = new NetworkCredential(emails, paw)
  125. };
  126. //设置默认发送信息
  127. Email.DefaultSender = new SmtpSender(smtp);
  128. var email = Email
  129. //发送人
  130. .From(emails, "环保数字")
  131. //收件人
  132. .To(toSomeOne.Select(x => new Address { EmailAddress = x }).ToList())
  133. //抄送人
  134. //.CC("154817501@qq.com")
  135. //邮件标题
  136. .Subject(subject)
  137. .Attach(new FluentEmail.Core.Models.Attachment
  138. {
  139. ContentType = attachmentType,
  140. Data = attachmentContent,
  141. Filename = attachmentName
  142. })
  143. //邮件内容
  144. // .Body("邮件内容");
  145. .Body("<h1 align=\"center\">" + title + "</h1><p>" + "</p>" + conent, true);
  146. //依据发送结果判断是否发送成功
  147. var result = email.Send();
  148. //或使用异步的方式发送
  149. //await email.SendAsync();
  150. if (result.Successful)
  151. {
  152. //发送成功逻辑
  153. return true;
  154. }
  155. else
  156. {
  157. //发送失败可以通过result.ErrorMessages查看失败原因
  158. return false;
  159. }
  160. }
  161. catch (Exception ex)
  162. {
  163. //throw new Exception("发送邮件错误");
  164. return false;
  165. }
  166. }
  167. //public static void SendMainTwo(string[] args)
  168. //{
  169. // MailMessage mailMsg = new MailMessage();//实例化对象
  170. // mailMsg.From = new MailAddress("154817501@qq.com", "严某人");//源邮件地址和发件人
  171. // mailMsg.To.Add(new MailAddress("154817501@qq.com"));//收件人地址
  172. // mailMsg.Subject = "邮件发送测试";//发送邮件的标题
  173. // StringBuilder sb = new StringBuilder();
  174. // sb.Append("测试测试测试测试");
  175. // sb.Append("嘿嘿");
  176. // mailMsg.Body = sb.ToString();//发送邮件的内容
  177. // //指定smtp服务地址(根据发件人邮箱指定对应SMTP服务器地址)
  178. // SmtpClient client = new SmtpClient();//格式:smtp.126.com smtp.164.com
  179. // client.Host = "smtp.qq.com";
  180. // //要用587端口
  181. // client.Port = 587;//端口
  182. // //加密
  183. // client.EnableSsl = true;
  184. // //通过用户名和密码验证发件人身份
  185. // client.Credentials = new NetworkCredential("154817501@qq.com", "aocrnaezwfygbhbf"); //
  186. // //发送邮件
  187. // try
  188. // {
  189. // client.Send(mailMsg);
  190. // }
  191. // catch (SmtpException ex)
  192. // {
  193. // }
  194. // Console.WriteLine("邮件已发送,请注意查收!");
  195. // Console.ReadKey();
  196. // mailMessage.Attachments.Clear();
  197. // //添加邮件附件,可发送多个文件
  198. // if (file != null && file.Count() > 0)
  199. // {
  200. // foreach (var filename in file)
  201. // {
  202. // mailMessage.Attachments.Add(new Attachment(filename, MediaTypeNames.Application.Octet));
  203. // }
  204. //}
  205. //mailMessage.Body = body;
  206. //try
  207. //{
  208. // smtpClient.Send(mailMessage);
  209. //}
  210. //catch (SmtpException ex)
  211. //{
  212. // throw new Exception("邮箱异常!" + ex.Message);
  213. //}
  214. //}
  215. //public static void test()
  216. //{
  217. // System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
  218. // try
  219. // {
  220. // mail.To = "****@qq.com";
  221. // mail.From = "****@163.com";
  222. // mail.Subject = "啊啊啊";
  223. // mail.BodyFormat = System.Web.Mail.MailFormat.Html;
  224. // mail.Body = "哈哈哈哈哈";
  225. // mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //身份验证
  226. // mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", mail.From); //邮箱登录账号,这里跟前面的发送账号一样就行
  227. // mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "授权码"); //这个密码要注意:如果是一般账号,要用授权码;企业账号用登录密码
  228. // mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);//端口
  229. // mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");//SSL加密
  230. // System.Web.Mail.SmtpMail.SmtpServer = "smtp.163.com"; //企业账号用smtp.exmail.qq.com
  231. // System.Web.Mail.SmtpMail.Send(mail);
  232. // //邮件发送成功
  233. // Console.Write("成功");
  234. // Console.ReadKey();
  235. // }
  236. // catch (Exception ex)
  237. // {
  238. // //失败,错误信息:ex.Message;
  239. // Console.WriteLine(ex.Message);
  240. // Console.ReadKey();
  241. // }
  242. //}
  243. }
  244. }