123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- 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)
- {
-
-
-
- log.Info($"发送邮件-BEGIN 【emails={emails}】【toSomeOne={toSomeOne};subject={subject};title={title};conent={conent}】");
- try
- {
- SmtpClient smtp = new SmtpClient
- {
-
- EnableSsl = true,
- Host = "smtp.qq.com",
- Port = 587,
- UseDefaultCredentials = false,
- DeliveryMethod = SmtpDeliveryMethod.Network,
-
-
-
- Credentials = new NetworkCredential(emails, paw)
- };
-
- Email.DefaultSender = new SmtpSender(smtp);
- var email = Email
-
- .From(emails, "环保数字")
-
- .To(toSomeOne)
-
-
-
- .Subject(subject)
-
-
- .Body("<h1 align=\"center\">"+ title + "</h1><p>"+ "</p>" + conent, true);
-
- log.Info($"发送邮件-Send 【emails={emails}】【toSomeOne={toSomeOne};subject={subject};title={title};conent={conent}】");
- var result = email.Send();
-
-
- 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}】");
-
- return false;
- }
- }
- catch (Exception ex)
- {
- log.Info($"发送邮件异常:{ex.Message}【emails={emails}】【toSomeOne={toSomeOne};subject={subject};title={title};conent={conent}】");
- return false;
- }
- }
- public static bool SendEmail(List<string> 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<string> toSomeOne, string subject, string title, string conent, string attachmentName, string attachmentType, Stream attachmentContent, string emails, string paw)
- {
- try
- {
- SmtpClient smtp = new SmtpClient
- {
-
- EnableSsl = true,
- Host = "smtp.qq.com",
- Port = 587,
- UseDefaultCredentials = false,
- DeliveryMethod = SmtpDeliveryMethod.Network,
-
-
-
- Credentials = new NetworkCredential(emails, paw)
- };
-
- Email.DefaultSender = new SmtpSender(smtp);
- var email = Email
-
- .From(emails, "环保数字")
-
- .To(toSomeOne.Select(x => new Address { EmailAddress = x }).ToList())
-
-
-
- .Subject(subject)
- .Attach(new FluentEmail.Core.Models.Attachment
- {
- ContentType = attachmentType,
- Data = attachmentContent,
- Filename = attachmentName
- })
-
-
- .Body("<h1 align=\"center\">" + title + "</h1><p>" + "</p>" + conent, true);
-
- var result = email.Send();
-
-
- if (result.Successful)
- {
-
- return true;
- }
- else
- {
-
- return false;
- }
- }
- catch (Exception ex)
- {
-
- return false;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- }
|