/// <summary>
        /// 获取安全套接字选项
        /// </summary>
        /// <returns></returns>
        protected virtual SecureSocketOptions GetSecureSocketOption()
        {
            var config = _mailKitConfigProvider.GetConfig();

            if (config.SecureSocketOption.HasValue)
            {
                return(config.SecureSocketOption.Value);
            }

            var emailConfig = _emailConfigProvider.GetConfig();

            return(emailConfig.EnableSsl ? SecureSocketOptions.SslOnConnect : SecureSocketOptions.StartTlsWhenAvailable);
        }
Exemple #2
0
        /// <summary>
        /// 配置SMTP客户端
        /// </summary>
        /// <param name="client">SMTP客户端</param>
        protected virtual void ConfigureClient(SmtpClient client)
        {
            var emailConfig   = _emailConfigProvider.GetConfig();
            var mailKitConfig = _mailKitConfigProvider.GetConfig();

            if (mailKitConfig.ServerCertificateValidationCallback.HasValue)
            {
                client.ServerCertificateValidationCallback = (s, c, h, e) => mailKitConfig.ServerCertificateValidationCallback.Value;
            }
            client.Connect(emailConfig.Host, emailConfig.Port, GetSecureSocketOption());
            if (emailConfig.UseDefaultCredentials)
            {
                return;
            }
            client.Authenticate(emailConfig.UserName, emailConfig.Password);
        }
        /// <summary>
        /// 开始发送邮件
        /// </summary>
        protected void StartSendMail()
        {
            var sw = new Stopwatch();

            try
            {
                while (true)
                {
                    if (_tryStop)
                    {
                        break;
                    }

                    if (_mailQueueProvider.IsEmpty)
                    {
                        WriteLog("队列是空,开始睡眠", LogLevel.Trace);
                        var config = _emailConfigProvider.GetConfig();
                        Thread.Sleep(config.SleepInterval);
                        continue;
                    }

                    if (_mailQueueProvider.TryDequeue(out var box))
                    {
                        WriteLog($"开始发送邮件 标题:{box.Subject},收件人:{box.To.First()}", LogLevel.Information);
                        sw.Restart();
                        SendMail(box);
                        sw.Stop();
                        WriteLog($"发送邮件结束 标题:{box.Subject},收件人:{box.To.First()},耗时:{sw.Elapsed.TotalSeconds}",
                                 LogLevel.Information);
                    }
                }
            }
            catch (Exception e)
            {
                WriteLog($"循环中出错,线程即将结束:{e.Message}", LogLevel.Error);
                IsRunning = false;
            }

            WriteLog("邮件发送线程即将停止,人为跳出循环,没有异常发生", LogLevel.Information);
            _tryStop  = false;
            IsRunning = false;
        }
Exemple #4
0
        /// <summary>
        /// 生成SMTP客户端
        /// </summary>
        /// <returns></returns>
        public SmtpClient BuildClient()
        {
            var config = _configProvider.GetConfig();
            var host   = config.Host;
            var port   = config.Port;

            var smtpClient = new SmtpClient(host, port);

            try
            {
                if (config.EnableSsl)
                {
                    smtpClient.EnableSsl = true;
                }

                if (config.UseDefaultCredentials)
                {
                    smtpClient.UseDefaultCredentials = true;
                }
                else
                {
                    smtpClient.UseDefaultCredentials = false;
                    var userName = config.UserName;
                    if (!userName.IsEmpty())
                    {
                        var password = config.Password;
                        var domain   = config.Domain;
                        smtpClient.Credentials = !domain.IsEmpty()
                            ? new NetworkCredential(userName, password, domain)
                            : new NetworkCredential(userName, password);
                    }
                }

                return(smtpClient);
            }
            catch
            {
                smtpClient.Dispose();
                throw;
            }
        }