Esempio n. 1
0
        public void marking_email_as_sent()
        {
            repo.Save(new EmailMessage
            {
                FromEmail = email,
                FromName  = "From Me",
                Subject   = "Test Email",
                ToEmail   = email,
                ToName    = email,
                BodyHTML  = "this is a test email message"
            });

            var id = repo.FetchNextBatch(1).Single().ID;

            repo.MarkAsSent(id);

            DB.Find <EmailMessage>()
            .Match(e => e.ID == id)
            .Project(e => new EmailMessage {
                Sent = e.Sent
            })
            .Execute()
            .Single()
            .Sent.Should().BeTrue();
        }
Esempio n. 2
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            using (var smtp = new SmtpClient())
            {
                var lastActiveAt = DateTime.UtcNow;
                var msgs         = new List <EmailMessage>();

                while (isProduction && !stoppingToken.IsCancellationRequested)
                {
                    if (!startMsgLogged)
                    {
                        startMsgLogged = true;
                        log.LogWarning("EMAIL SERVICE HAS STARTED..." + "[" + settings.Username + "]" + Environment.NewLine);
                    }

                    msgs = repo.FetchNextBatch(settings.BatchSize);

                    if (msgs.Count > 0)
                    {
                        if (!smtp.IsConnected)
                        {
                            try
                            {
                                await smtp.ConnectAsync(settings.Server, settings.Port, true, stoppingToken);

                                await smtp.AuthenticateAsync(settings.Username, settings.Password, stoppingToken);

                                lastActiveAt = DateTime.UtcNow;
                            }
                            catch (Exception x)
                            {
                                log.LogError(x, "COULD NOT CONNECT TO SMTP SERVER SUCCESSFULLY!!!" + "[" + settings.Username + "]" + Environment.NewLine);
                                await Task.Delay((int)TimeSpan.FromMinutes(10).TotalMilliseconds);
                            }
                        }

                        if (smtp.IsConnected && smtp.IsAuthenticated)
                        {
                            foreach (var m in msgs)
                            {
                                try
                                {
                                    await smtp.SendAsync(ComposeEmail(m), stoppingToken);

                                    lastActiveAt = DateTime.UtcNow;
                                    repo.MarkAsSent(m.ID);
                                }
                                catch (Exception x)
                                {
                                    log.LogError(x, "COULD NOT SEND EMAIL VIA SMTP SERVER!!!" + Environment.NewLine);
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        if (smtp.IsConnected && DateTime.UtcNow.Subtract(lastActiveAt).TotalSeconds >= 30)
                        {
                            await smtp.DisconnectAsync(true, stoppingToken);
                        }
                        await Task.Delay((int)TimeSpan.FromSeconds(10).TotalMilliseconds);
                    }
                }
            }
        }