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));
///
"+ "
" + 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" + "
" + 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(); // } //} } }