using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using FluentEmail.Core; using System.Net; using System.Net.Mail; using FluentEmail.Smtp; using FluentEmail.Core.Models; using System.IO; using log4net; namespace Ropin.Inspection.Common.Helper { public class EmailHelper { private static readonly ILog log = LogManager.GetLogger(typeof(EmailHelper)); /// /// 发送邮件 /// /// public static bool SendEmail(string toSomeOne, string subject, string title, string conent) { bool result=false; result=SendEmailFun(toSomeOne, subject, title, conent,"yan-daniu@qq.com", "xdzwhrasuhrmbhad"); if(!result) { result = SendEmailFun(toSomeOne, subject, title, conent, "154817501@qq.com", "aocrnaezwfygbhbf"); } return result; } /// /// 发送邮件 /// /// private static bool SendEmailFun(string toSomeOne,string subject,string title, string conent,string emails,string paw) { //MailAddress mailfrom = new MailAddress(from, senderDisplayName, encoding);//发件人邮箱地址,名称,编码UTF8 //MailAddress mailto = new MailAddress(tomail, recipientsDisplayName, encoding);//收件人邮箱地址,名称,编码UTF8 //MailMessage message = new MailMessage(mailfrom, mailto);//创建mailMessage对象 log.Info($"发送邮件-BEGIN 【emails={emails}】【toSomeOne={toSomeOne};subject={subject};title={title};conent={conent}】"); try { SmtpClient smtp = new SmtpClient { //smtp服务器地址 EnableSsl = true,//启用ssl Host = "smtp.qq.com",// Port = 587,//25,465 UseDefaultCredentials = false,//是否使用默认凭据 DeliveryMethod = SmtpDeliveryMethod.Network, //这里输入你在发送smtp服务器的用户名和密码 //Credentials = new NetworkCredential("154817501@qq.com", "aocrnaezwfygbhbf") //Credentials = new NetworkCredential("yan-daniu@qq.com", "xdzwhrasuhrmbhad") Credentials = new NetworkCredential(emails, paw) }; //设置默认发送信息 Email.DefaultSender = new SmtpSender(smtp); var email = Email //发送人 .From(emails, "环保数字") //收件人 .To(toSomeOne) //抄送人 //.CC("154817501@qq.com") //邮件标题 .Subject(subject) //邮件内容 // .Body("邮件内容"); .Body("

"+ title + "

"+ "

" + conent, true); //依据发送结果判断是否发送成功 log.Info($"发送邮件-Send 【emails={emails}】【toSomeOne={toSomeOne};subject={subject};title={title};conent={conent}】"); var result = email.Send(); //或使用异步的方式发送 //await email.SendAsync(); if (result.Successful) { //发送成功逻辑 log.Info($"发送邮件-SendEND 【emails={emails}】【toSomeOne={toSomeOne};subject={subject};title={title};conent={conent}】"); return true; } else { log.Info($"发送邮件-发送失败 【emails={emails}】【toSomeOne={toSomeOne};subject={subject};title={title};conent={conent} ;ErrorMessages={result.ErrorMessages}】"); //发送失败可以通过result.ErrorMessages查看失败原因 return false; } } catch (Exception ex)//SmtpException { log.Info($"发送邮件异常:{ex.Message}【emails={emails}】【toSomeOne={toSomeOne};subject={subject};title={title};conent={conent}】"); return false; } } public static bool SendEmail(List toSomeOne, string subject, string title, string conent, string attachmentName, string attachmentType, Stream attachmentContent) { bool result = false; result = SendEmailFun(toSomeOne, subject, title, conent, attachmentName, attachmentType, attachmentContent, "yan-daniu@qq.com", "xdzwhrasuhrmbhad"); if (!result) { result = SendEmailFun(toSomeOne, subject, title, conent, attachmentName, attachmentType, attachmentContent, "154817501@qq.com", "aocrnaezwfygbhbf"); } if (!result) { throw new Exception("发送邮件失败"); } return result; } private static bool SendEmailFun(List toSomeOne, string subject, string title, string conent, string attachmentName, string attachmentType, Stream attachmentContent, string emails, string paw) { try { SmtpClient smtp = new SmtpClient { //smtp服务器地址 EnableSsl = true,//启用ssl Host = "smtp.qq.com",//smtp.qq.com Port = 587,//25,465 UseDefaultCredentials = false,//是否使用默认凭据 DeliveryMethod = SmtpDeliveryMethod.Network, //这里输入你在发送smtp服务器的用户名和密码 //Credentials = new NetworkCredential("154817501@qq.com", "aocrnaezwfygbhbf") //Credentials = new NetworkCredential("yan-daniu@qq.com", "xdzwhrasuhrmbhad") Credentials = new NetworkCredential(emails, paw) }; //设置默认发送信息 Email.DefaultSender = new SmtpSender(smtp); var email = Email //发送人 .From(emails, "环保数字") //收件人 .To(toSomeOne.Select(x => new Address { EmailAddress = x }).ToList()) //抄送人 //.CC("154817501@qq.com") //邮件标题 .Subject(subject) .Attach(new FluentEmail.Core.Models.Attachment { ContentType = attachmentType, Data = attachmentContent, Filename = attachmentName }) //邮件内容 // .Body("邮件内容"); .Body("

" + title + "

" + "

" + conent, true); //依据发送结果判断是否发送成功 var result = email.Send(); //或使用异步的方式发送 //await email.SendAsync(); if (result.Successful) { //发送成功逻辑 return true; } else { //发送失败可以通过result.ErrorMessages查看失败原因 return false; } } catch (Exception ex) { //throw new Exception("发送邮件错误"); return false; } } //public static void SendMainTwo(string[] args) //{ // MailMessage mailMsg = new MailMessage();//实例化对象 // mailMsg.From = new MailAddress("154817501@qq.com", "严某人");//源邮件地址和发件人 // mailMsg.To.Add(new MailAddress("154817501@qq.com"));//收件人地址 // mailMsg.Subject = "邮件发送测试";//发送邮件的标题 // StringBuilder sb = new StringBuilder(); // sb.Append("测试测试测试测试"); // sb.Append("嘿嘿"); // mailMsg.Body = sb.ToString();//发送邮件的内容 // //指定smtp服务地址(根据发件人邮箱指定对应SMTP服务器地址) // SmtpClient client = new SmtpClient();//格式:smtp.126.com smtp.164.com // client.Host = "smtp.qq.com"; // //要用587端口 // client.Port = 587;//端口 // //加密 // client.EnableSsl = true; // //通过用户名和密码验证发件人身份 // client.Credentials = new NetworkCredential("154817501@qq.com", "aocrnaezwfygbhbf"); // // //发送邮件 // try // { // client.Send(mailMsg); // } // catch (SmtpException ex) // { // } // Console.WriteLine("邮件已发送,请注意查收!"); // Console.ReadKey(); // mailMessage.Attachments.Clear(); // //添加邮件附件,可发送多个文件 // if (file != null && file.Count() > 0) // { // foreach (var filename in file) // { // mailMessage.Attachments.Add(new Attachment(filename, MediaTypeNames.Application.Octet)); // } //} //mailMessage.Body = body; //try //{ // smtpClient.Send(mailMessage); //} //catch (SmtpException ex) //{ // throw new Exception("邮箱异常!" + ex.Message); //} //} //public static void test() //{ // System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage(); // try // { // mail.To = "****@qq.com"; // mail.From = "****@163.com"; // mail.Subject = "啊啊啊"; // mail.BodyFormat = System.Web.Mail.MailFormat.Html; // mail.Body = "哈哈哈哈哈"; // mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //身份验证 // mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", mail.From); //邮箱登录账号,这里跟前面的发送账号一样就行 // mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "授权码"); //这个密码要注意:如果是一般账号,要用授权码;企业账号用登录密码 // mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);//端口 // mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");//SSL加密 // System.Web.Mail.SmtpMail.SmtpServer = "smtp.163.com"; //企业账号用smtp.exmail.qq.com // System.Web.Mail.SmtpMail.Send(mail); // //邮件发送成功 // Console.Write("成功"); // Console.ReadKey(); // } // catch (Exception ex) // { // //失败,错误信息:ex.Message; // Console.WriteLine(ex.Message); // Console.ReadKey(); // } //} } }