private IEnumerable<MessageContainer> GetMessages(Mailbox mailbox) { var messageCollection = new List<MessageContainer>(); //var selectedMailbox = Imap.Client.SelectedMailbox; /*var unreadMessagesQuery = Imap.Client.Messages .Where(x => !x.Flags.HasFlag(MessageFlags.Seen)) .Select(x => new MessageContainer { Uid = x.Uid, Envelope = x.Envelope, BodyStructure = x.BodyStructure, Size = x.Size });*/ using(var session = App.DocumentStore.OpenSession()) { var indexRange = MailController.LastUid - 25; var databaseMessages = session.Query<MessageContainer>().Where(x => x.Uid > indexRange).OrderBy(x => x.Uid).ToList(); messageCollection.AddRange(databaseMessages); } var messagesQuery = Imap.Client.Messages .Where(x => x.Uid > MailController.LastUid) //.Where(x => !x.Flags.HasFlag(MessageFlags.Seen)) .Select(x => new MessageContainer { Uid = x.Uid, Envelope = x.Envelope, //BodyStructure = x.BodyStructure, Size = x.Size }).ToList(); // Save the emails to the database foreach (var messageContainer in messagesQuery) { if (messageContainer == null) continue; if (messageContainer.Uid > MailController.LastUid) { MailController.LastUid = messageContainer.Uid; } else if(messageContainer.Uid == MailController.LastUid) { continue; } using (var session = App.DocumentStore.OpenSession()) { var loadedMessageContainer = session.Load<MessageContainer>(messageContainer.Uid.ToString()); // If there is no message already in the database. Store it. if (loadedMessageContainer == null) { try { /*// Load the Envelope and the Size from the server var container = messageContainer; var message = Imap.Client.Messages .Where(x => x.Uid == container.Uid) .Select(x => new MessageContainer { Uid = container.Uid, Mailbox = mailbox, Envelope = x.Envelope, Size = x.Size, }).ToList().FirstOrDefault();*/ App.Log.Info("Writing mail " + messageContainer.Uid + " to the database."); session.Store(messageContainer, messageContainer.Uid.ToString()); messageCollection.Add(messageContainer); } catch (Exception ex) { App.Log.Error(ex); throw; } } // We already have the message in the database. Load it. else { messageCollection.Add(loadedMessageContainer); } session.SaveChanges(); } } App.Log.Info("Messages saved to database."); //App.Log.Info("There are " + unreadMessagesQuery.AsEnumerable().Count() + " unread messages."); /*foreach (var unreadEnvelope in unreadMessagesQuery) { if (unreadEnvelope == null) continue; App.Log.Debug("Unread message: " + unreadEnvelope.Subject); messageCollection.Add(unreadEnvelope); }*/ messageCollection.Reverse(); //lstEnvelopes.ItemsSource = unreadCollection; return messageCollection; }
private void AddMessagesOneByOne(Mailbox mailbox) { var indexRange = MailController.LastUid - 100; for (int i = indexRange; i < MailController.LastUid; i++) { // Open messages from database using (var session = App.DocumentStore.OpenSession()) { var message = session.Load<MessageContainer>(i.ToString()); if (message != null && message.Mailbox.Name.Equals(mailbox.Name)) { AddEnvelopeSafely(message); } } } //var databaseMessages = session.Query<MessageContainer>().Where(x => x.Uid > indexRange).OrderBy(x => x.Uid).ToList(); //messageCollection.AddRange(databaseMessages); /*foreach (var messageContainer in databaseMessages) { AddEnvelopeSafely(messageContainer); }*/ var messagesQuery = Imap.Client.Messages .Where(x => x.Uid > MailController.LastUid) //.Where(x => !x.Flags.HasFlag(MessageFlags.Seen)) .Select(x => new MessageContainer { Uid = x.Uid, Envelope = x.Envelope, Size = x.Size }).ToList(); // Save the emails to the database foreach (var messageContainer in messagesQuery) { if (messageContainer == null) continue; if (messageContainer.Uid > MailController.LastUid) { MailController.LastUid = messageContainer.Uid; } /*else if (messageContainer.Uid == MailController.LastUid) { continue; }*/ messageContainer.Mailbox = mailbox; using (var session = App.DocumentStore.OpenSession()) { var loadedMessageContainer = session.Load<MessageContainer>(messageContainer.Uid.ToString()); // If there is no message already in the database. Store it. if (loadedMessageContainer == null) { try { App.Log.Info("Writing mail " + messageContainer.Uid + " to the database."); session.Store(messageContainer, messageContainer.Uid.ToString()); //messageCollection.Add(messageContainer); AddEnvelopeSafely(messageContainer); } catch (Exception ex) { App.Log.Error(ex); throw; } session.SaveChanges(); } // We already have the message in the database. Load it. else { //messageCollection.Add(loadedMessageContainer); AddEnvelopeSafely(loadedMessageContainer); } } } }