Ejemplo n.º 1
0
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="config"></param>
        /// <param name="tos"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <param name="isHtml"></param>
        public async void Smtp(MailConfig config, IEnumerable <string> tos, string subject, string body, bool isHtml = false)
        {
            using (var client = new SmtpClient(config.Host, config.Port))
            {
                var message = new MailMessage();

                message.From = new MailAddress(config.Account);
                foreach (var mail in tos)
                {
                    if (!String.IsNullOrEmpty(mail))
                    {
                        message.To.Add(mail);
                    }
                }
                message.SubjectEncoding = Encoding.UTF8;
                message.BodyEncoding    = Encoding.UTF8;
                message.Subject         = subject;
                message.Body            = body;
                message.IsBodyHtml      = isHtml;
                client.DeliveryMethod   = SmtpDeliveryMethod.Network;
                client.EnableSsl        = true;
                client.Credentials      = new NetworkCredential(config.Account, config.Password);
                try
                {
                    await client.SendMailAsync(message);
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "邮件发送失败", config, tos);
                }
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 发送邮件
 /// </summary>
 /// <param name="config"></param>
 /// <param name="to"></param>
 /// <param name="subject"></param>
 /// <param name="body"></param>
 /// <param name="isHtml"></param>
 public void Smtp(MailConfig config, string to, string subject, string body, bool isHtml = false)
 {
     Smtp(config, new List <string>()
     {
         to
     }, subject, body, isHtml);
 }
Ejemplo n.º 3
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="config"></param>
 /// <param name="logger"></param>
 public MailSender(MailConfig config,
                   ILogger <MailSender> logger)
 {
     _config = config;
     _logger = logger;
 }