Example #1
0
        public void SetUpConfigurationBuilderTest()
        {
            var config = ConfigurationHandler.GetAppConfiguration();

            //Assert.IsNotNull(config.ConnectionStrings["Database"].ConnectionString);
            //Assert.AreEqual("false", config.AppSettings["ZipWebRequests"]);
        }
        public void Initialize()
        {
            var services = new ServiceCollection();

            services.AddSingleton(ins => ConfigurationHandler.GetAppConfiguration());


            var databaseManager = new MSSQLManager(ConfigurationHandler.GetConfiguration());

            services.AddSingleton(provider => databaseManager);

            ServiceLocator.SetLocatorProvider(services.BuildServiceProvider());
        }
Example #3
0
        public static void SendMail(EMailMessage message, bool sendAsync = false)
        {
#if NETFRAMEWORK
            var appConfiguration = ConfigurationManager.AppSettings;
#else
            var appConfiguration = ConfigurationHandler.GetAppConfiguration();
#endif
            if (string.IsNullOrEmpty(message.From))
            {
                var settings = FetchSmtpSettings();

                if (settings == null)
                {
                    throw new ArgumentNullException(nameof(message), "No valid mail settings were found in the configuration.");
                }

                message.From = settings.Smtp.From;
            }

            bool.TryParse(appConfiguration["TestMode"], out var testMode);

            if (testMode)
            {
                var originalRecipients = "";
                foreach (var i in message.To)
                {
                    originalRecipients += $"<br/>{i}";
                }

                message.To.Clear();
                message.To.Add(appConfiguration["TestModeEmail"]);
                message.Body = "<b>TEST MODE</b> Original Recipients:" + originalRecipients + "<br/><br/>" + message.Body;
            }

            var mailSeparatorCharacter = appConfiguration["EmailSeparatorCharacter"];
            mailSeparatorCharacter = !string.IsNullOrWhiteSpace(mailSeparatorCharacter) ? mailSeparatorCharacter : ",";

            SendMail(message.Subject,
                     message.Body,
                     message.To == null ? "" : string.Join(mailSeparatorCharacter, message.To.Where(a => !string.IsNullOrWhiteSpace(a))),
                     message.CC == null ? "" : string.Join(mailSeparatorCharacter, message.CC.Where(a => !string.IsNullOrWhiteSpace(a))),
                     message.Bcc == null ? "" : string.Join(mailSeparatorCharacter, message.Bcc.Where(a => !string.IsNullOrWhiteSpace(a))),
                     message.From,
                     message.Attachments?.Select(a => a.Attachment).ToList(),
                     sendAsync);
        }
Example #4
0
        private static void SendMail(MailMessage message, bool sendAsync = false)
        {
            var log      = LogManager.GetLogger(typeof(Email));
            var settings = FetchSmtpSettings();

#if NETFRAMEWORK
            var appConfiguration = ConfigurationManager.AppSettings;
#else
            var appConfiguration = ConfigurationHandler.GetAppConfiguration();
#endif

            SmtpClient client = null;
            try
            {
                if (message.To.Count == 0)
                {
                    log.Warn("No message recipients!");
                    return;
                }

                client = new SmtpClient(settings.Smtp.Network.Host, settings.Smtp.Network.Port)
                {
                    Credentials = new NetworkCredential(settings.Smtp.Network.UserName,
                                                        settings.Smtp.Network.Password)
                };

                log.Debug("host: " + client.Host);
                log.Debug("port: " + client.Port);

                if (bool.TryParse(appConfiguration["EnableSSL"], out var ssl))
                {
                    client.EnableSsl = ssl;
                }

                log.Debug("SSL: " + client.EnableSsl);

                var emailSecureConnectionType = appConfiguration["EmailSecureConnectionType"];
                if (string.IsNullOrWhiteSpace(emailSecureConnectionType))
                {
                    emailSecureConnectionType = "TLS";
                }

                if (!string.IsNullOrWhiteSpace(message.Body))
                {
                    // this is to avoid BARE LFs that some email servers reject
                    message.Body = message.Body.Replace("\n", "\r\n").Replace("\r\r", "\r");
                }

                if (emailSecureConnectionType != "TLS")
                {
                    log.Debug("Sending email using SSL");
                    throw new NotImplementedException();
                    //SendMailOverSSL(message);
                    //return;
                }

                if (sendAsync)
                {
                    log.Debug("Sending email using TLS asynchronously");

                    new Thread(() => {
                        try
                        {
                            client.Send(message);
                        }
                        catch (Exception e)
                        {
                            LogManager.GetLogger(typeof(Email)).Error("Error sending Email", e);
                            return;
                        }
                        finally
                        {
                            client?.Dispose();
                        }

                        foreach (var att in message.Attachments)
                        {
                            att.ContentStream?.Dispose();
                        }

                        LogManager.GetLogger(typeof(Email)).Debug("Sent Email successfully");
                    }).Start();

                    //client.SendCompleted += (sender, args) =>
                    //               {
                    //                   if (args.Error != null)
                    //                   {
                    //                       log.Error(args.Error);
                    //                   }
                    //               };
                    //client.SendAsync(message, CancellationToken.None);
                }
                else
                {
                    log.Debug("Sending email using TLS synchronously");
                    client.Send(message);

                    foreach (var att in message.Attachments)
                    {
                        att.ContentStream?.Dispose();
                    }
                }
            }
            catch (Exception e)
            {
                var exc = e;

                while (exc != null)
                {
                    log.Error("Problem with sending email", exc);
                    exc = exc.InnerException;
                }
            }
            finally
            {
                if (!sendAsync)
                {
                    client?.Dispose();
                }
            }
        }