Example #1
0
 /// <summary>
 /// Instantiate w new email model
 /// </summary>
 public Email(string fromName, string fromEmail, string toName, string toEmail, string subject, EmailTemplates template)
 {
     this.fromName  = fromName;
     this.fromEmail = fromEmail;
     this.toName    = toName;
     this.toEmail   = toEmail;
     this.subject   = subject;
     this.template  = RepoEmail.GetTemplate(template);
 }
Example #2
0
        /// <summary>
        /// Add this email message to the queue for sending out by the email service.
        /// </summary>
        public void AddToSendingQueue()
        {
            if (MergeFields.Count == 0)
            {
                throw new InvalidOperationException("Cannot proceed without any MergeFields!");
            }

            var sb = new StringBuilder(template);

            foreach (var f in MergeFields)
            {
                sb.Replace("{" + f.Key + "}", f.Value);
            }

            template = sb.ToString();

            RepoEmail.Save(ToEntity());
        }
Example #3
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 = RepoEmail.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;
                                    RepoEmail.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);
                    }
                }
            }
        }