예제 #1
0
        public void Hook(IPipelines p)
        {
            p.AfterRequest.AddItemToEndOfPipeline((ctx) =>
            {
                if (_Outbox == null)
                {
                    return;
                }

                if (ctx.Items.ContainsKey("SiteSettings") == false)
                {
                    return;
                }

                var toSend = _Outbox.ToList();
                _Outbox    = null;

                var site     = ctx.Items["SiteSettings"] as JObject;
                var db       = ctx.Items["SiteDatabase"] as NancyBlackDatabase;
                var settings = site.Property("smtp").Value.ToObject <SmtpSettings>();

                Task.Run(() =>
                {
                    SmtpClient client  = new SmtpClient(settings.server);
                    client.Port        = settings.port;
                    client.Credentials = new System.Net.NetworkCredential(settings.username, settings.password);
                    client.EnableSsl   = settings.useSSL;
                    client.Timeout     = 30000;

                    foreach (var mail in toSend)
                    {
                        var log      = new NcbMailSenderLog();
                        log.Body     = mail.Body;
                        log.To       = string.Join(",", from m in mail.To select m.Address);
                        log.Subject  = mail.Subject;
                        log.Settings = settings;

                        try
                        {
                            mail.From = new MailAddress(settings.fromEmail);
                            client.Send(mail);
                        }
                        catch (Exception e)
                        {
                            log.Exception = e;
                        }

                        db.UpsertRecord(log);
                    }
                });
            });
        }
예제 #2
0
        public void Hook(IPipelines p)
        {
            p.AfterRequest.AddItemToEndOfPipeline((ctx) =>
            {
                if (_Outbox == null)
                {
                    return;
                }

                if (ctx.Items.ContainsKey("SiteSettings") == false)
                {
                    return;
                }

                var toSend = _Outbox.ToList();
                if (toSend.Count == 0)
                {
                    return;
                }

                _Outbox = null;

                var site     = ctx.Items["SiteSettings"] as JObject;
                var db       = ctx.Items["SiteDatabase"] as NancyBlackDatabase;
                var settings = site.Property("smtp").Value.ToObject <SmtpSettings>();

                Task.Run(() =>
                {
                    SmtpClient client  = new SmtpClient(settings.server);
                    client.Port        = settings.port;
                    client.Credentials = new System.Net.NetworkCredential(settings.username, settings.password);
                    client.EnableSsl   = settings.useSSL;
                    client.Timeout     = 30000;

                    foreach (var mail in toSend)
                    {
                        var log         = new NcbMailSenderLog();
                        log.Body        = mail.Body;
                        log.To          = string.Join(",", from m in mail.To select m.Address);
                        log.Subject     = mail.Subject;
                        log.Settings    = settings;
                        log.__createdAt = DateTime.Now;
                        log.__updatedAt = DateTime.Now;

                        var key = log.To + "-" + log.Subject + log.Body.GetHashCode();
                        if (MemoryCache.Default[key] != null)
                        {
                            continue; // we just send this email to this user recently, skip
                        }

                        MemoryCache.Default.Add(key, 1, DateTimeOffset.Now.AddMinutes(10));

                        try
                        {
                            mail.From = new MailAddress(settings.fromEmail);
                            client.Send(mail);
                        }
                        catch (Exception e)
                        {
                            log.Exception = e;
                        }

                        db.DelayedInsert(log);
                    }
                });
            });
        }
예제 #3
0
        /// <summary>
        /// Immediately Send out email
        /// </summary>
        public static void ProcessQueue(Nancy.NancyContext ctx)
        {
            if (_Outbox == null)
            {
                return;
            }

            if (ctx.Items.ContainsKey("SiteSettings") == false)
            {
                return;
            }

            lock (BaseModule.GetLockObject("MailSenderModule.ProcessQueue"))
            {
                if (_Outbox == null)
                {
                    return;
                }

                var toSend = _Outbox.ToList();
                _Outbox = null;

                var site = ctx.Items["SiteSettings"] as JObject;

                Task.Run(() =>
                {
                    Func <SmtpSettings, SmtpClient> getClient = (s) =>
                    {
                        SmtpClient client  = new SmtpClient(s.server);
                        client.Port        = s.port;
                        client.Credentials = new System.Net.NetworkCredential(s.username, s.password);
                        client.EnableSsl   = s.useSSL;
                        client.Timeout     = 30000;

                        return(client);
                    };

                    var db            = NancyBlackDatabase.GetSiteDatabase(BootStrapper.RootPath);
                    var settings      = site.Property("smtp").Value.ToObject <SmtpSettings>();
                    var count         = 0;
                    SmtpClient sender = getClient(settings);

                    foreach (var mail in toSend)
                    {
                        if (count % 100 == 0)
                        {
                            sender.Dispose();
                            count = 0;

                            sender = getClient(settings);
                        }

                        var log         = new NcbMailSenderLog();
                        log.Body        = mail.Body;
                        log.To          = string.Join(",", from m in mail.To select m.Address);
                        log.Subject     = mail.Subject;
                        log.MessageHash = (log.Body + log.To + log.Subject).GetHashCode();
                        log.Settings    = settings;
                        log.__createdAt = DateTime.Now;
                        log.__updatedAt = DateTime.Now;

                        var today   = DateTime.Now.Date;
                        var lastLog = db.Query <NcbMailSenderLog>().Where(l => l.MessageHash == log.MessageHash).FirstOrDefault();
                        if (lastLog != null)
                        {
                            if (DateTime.Now.Subtract(lastLog.__createdAt).TotalHours < 12)
                            {
                                log.IsSkipped = true;
                            }
                        }

                        if (log.IsSkipped == false)
                        {
                            try
                            {
                                log.IsAttempted = true;

                                mail.From = new MailAddress(settings.fromEmail);
                                sender.Send(mail);

                                log.IsSent = true;
                            }
                            catch (Exception e)
                            {
                                log.Exception = e;
                            }
                        }

                        db.UpsertRecord(log);
                        count++;
                    }

                    db.Dispose();
                });
            }
        }