Пример #1
0
        private void consumeQueue()
        {
            string prefix = nameof(consumeQueue) + Constants.FNSUFFIX;

            if (!validSettings(_emailOptions.Password))
            {
                return;
            }

            // Make one connection to email server for all emails to send
            using (var client = new SmtpClient())
            {
                try
                {
                    client.ServerCertificateValidationCallback = ((s, c, h, e) => true);

                    client.Connect(
                        _emailOptions.Host,
                        _emailOptions.Port,
                        _emailOptions.EnableSsl);

                    client.AuthenticationMechanisms.Remove("XOAUTH2");

                    client.Authenticate(_emailOptions.Username, _emailOptions.Password);
                }
                catch (Exception ex)
                {
                    _logger.LogInformation(prefix + $"ERROR: Exception during attempt to connect to email server; Exception:[{ex.ToString()}]");
                    return;
                }

                if (!client.IsConnected)
                {
                    _logger.LogInformation(prefix + $"ERROR: Failed to connect to email server;  No exception occurred, but SMTP client is not connected.");
                    client.Disconnect(true);
                    return;
                }

                if (!client.IsAuthenticated)
                {
                    _logger.LogInformation(prefix + $"ERROR: Connection to email server was successful but SMTP client is not authenticated.");
                    client.Disconnect(true);
                    return;
                }

                // Should be connected and authenticated
                EmailContext emailContext = null;
                while (_queue.TryDequeue(out emailContext))
                {
                    processQueuedItem(client, emailContext, _queue.Count);
                }

                client.Disconnect(true);
            } // end of using()
        }
Пример #2
0
        /// <summary>
        /// Add an email context to the queue to send.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public bool Enqueue(EmailContext context)
        {
            int countQueued = _queue.Count;

            if (checkCapacity(countQueued))
            {
                _queue.Enqueue(context);
                _eventThreadAction.Set();
                return(true);
            }
            else
            {
                return(false);
            }
        }
Пример #3
0
        private static void sendEmail(string msg, LogLevel logLevel)
        {
            // Exit if no email set
            if (_emailService == null)
            {
                return;
            }

            string       subject = _assembly + " " + logLevel.ToString() + " on " + Environment.MachineName;
            EmailContext ec      = new EmailContext {
                Subject = subject, Body = msg
            };

            _emailService.Enqueue(ec);
        }
Пример #4
0
        private void processQueuedItem(SmtpClient client, EmailContext emailContext, int countQueued)
        {
            string prefix = nameof(processQueuedItem) + Constants.FNSUFFIX;

            _logger.LogInformation(prefix + $"About to attempt send for Context:[{JsonConvert.SerializeObject(emailContext)}]");

            char[]        delimiters    = new char[] { ',', ';', ' ' }; // Permit addresses to be separated by commas, semis or spaces
            List <string> addressesTo   = (List <string>)processAddressField(emailContext.To, _emailOptions.To, delimiters);
            List <string> addressesFrom = (List <string>)processAddressField(emailContext.From, _emailOptions.From, delimiters);

            if (addressesTo.Count == 0)
            {
                string error = "The receiver (To) address list is empty.";
                emailContext.Errors.Add(error);
                emailContext.IsCompleted = true;
                _logger.LogInformation(prefix + $"ERROR: Not sending this message; " + error);
                return;
            }

            if (addressesFrom.Count == 0)
            {
                string error = "The send (From) address list is empty.";
                emailContext.Errors.Add(error);
                emailContext.IsCompleted = true;
                _logger.LogInformation(prefix + $"ERROR: Not sending this message; " + error);
                return;
            }


            var message = new MimeMessage();

            foreach (string addressTo in addressesTo)
            {
                message.To.Add(new MailboxAddress(addressTo));
            }

            foreach (string addressFrom in addressesFrom)
            {
                message.From.Add(new MailboxAddress(addressFrom));
            }

            message.Subject = string.IsNullOrWhiteSpace(emailContext.Subject) ? "(no subject)" : emailContext.Subject;

            string body = string.IsNullOrWhiteSpace(emailContext.Body) ? "(no email body text)" : emailContext.Body;

            message.Body = new TextPart("plain")
            {
                Text = body
            };

            try
            {
                client.Send(message);
            }
            catch (Exception ex)
            {
                string error = $"Exception:[{ex.ToString()}].";
                emailContext.Errors.Add(error);
                emailContext.IsCompleted = true;
                _logger.LogInformation(prefix + $"ERROR: Exception during Send() attempt for Context:[{JsonConvert.SerializeObject(emailContext)}]; " + error);
                return;
            }

            emailContext.IsCompleted = true;
            _logger.LogInformation(prefix + $"Send completed for Context:[{JsonConvert.SerializeObject(emailContext)}]");
        }