private void FixupMailRecord(MailRecord previousValue) { if (previousValue != null && ReferenceEquals(previousValue.User, this)) { previousValue.User = null; } if (MailRecord != null) { MailRecord.User = this; } }
private void SendMesseges(IList<FeedRecord> package, int threadId) { var smtp = new SmtpClient(); var sendedCount = 0; var emptyMails = 0; _log.Write(string.Format("----- Поток {0} начинает рассылку ({1} сообщений) -----", threadId, package.Count )); foreach (var messageContent in package) { var messageTemplate = RazorBasedTemplateRenderEngine.NewMailMessageTemplate(messageContent, _assembly); messageTemplate.Execute(); var message = new MailMessage(); message.To.Add(messageTemplate.UserEmail); message.Headers.Add("Precedence", "bulk"); try { if (messageContent.PackFailed) throw new Exception("Сборка рассылки для пользователя провалилась"); if (messageContent.IsThereUpdates) { message.Subject = "Демократия2. Обновления за " + messageTemplate.Date.ToString("dd MMMM yyyy"); message.IsBodyHtml = true; message.Body = messageTemplate.Body; message.To.Add(messageTemplate.UserEmail); message.Headers.Add("Precedence", "bulk"); var sendingMessage = "Поток " + threadId + " Отправляем: " + messageTemplate.UserFullName + " подписка: " + messageContent.Content.Count + ", сообщений: " + (messageContent.CommentNotices.Count + messageContent.GroupMemberNotices.Count + messageContent.GroupModeratorNotices.Count + messageContent.PetitionNotices.Count + messageContent.PollNotices.Count + messageContent.PrivateMessages.Count + messageContent.SystemMessages.Count); _log.Write(sendingMessage); if (MainClass.TESTING) { if (!Directory.Exists("messages")) Directory.CreateDirectory("messages"); File.WriteAllText("messages\\" + messageTemplate.UserId + ".html", message.Body); sendedCount++; Thread.Sleep(5000); } else { smtp.Send(message); sendedCount++; Thread.Sleep(5000); } } else { var sendingMessage = "Поток " + threadId + " Нечего отправлять: " + messageTemplate.UserFullName; _log.Write(sendingMessage); emptyMails++; } var mailRecord = DataService.PerThread.MailRecordSet.SingleOrDefault(mr => mr.User.Id == messageTemplate.UserId); if (mailRecord != null) { mailRecord.Date = DateTime.Now; mailRecord.IsDelivered = true; } else { var user = DataService.PerThread.BaseUserSet.OfType<User>().SingleOrDefault(u => u.Id == messageTemplate.UserId); if (user != null) { mailRecord = new MailRecord { Date = DateTime.Now, User = user, IsDelivered = true }; DataService.PerThread.MailRecordSet.AddObject(mailRecord); } } } catch (Exception exception) { _log.Write(exception); var mailRecord = DataService.PerThread.MailRecordSet.SingleOrDefault(mr => mr.User.Id == messageTemplate.UserId); if (mailRecord != null) { mailRecord.Date = DateTime.Now; mailRecord.IsDelivered = false; } else { var user = DataService.PerThread.BaseUserSet.OfType<User>().SingleOrDefault(u => u.Id == messageTemplate.UserId); if (user != null) { mailRecord = new MailRecord { Date = DateTime.Now, User = user, IsDelivered = false }; DataService.PerThread.MailRecordSet.AddObject(mailRecord); } } } finally { message.Dispose(); DataService.PerThread.SaveChanges(); } } smtp.Dispose(); _log.Write(string.Format("----- Поток {0} закончил рассылку ({3} сообщений: {1} отправлено, {2} пустых) -----", threadId, sendedCount, emptyMails, package.Count)); }