Ejemplo n.º 1
0
        public async Task <IList <GmailMessageInfo> > GetMessageIdsAsync(IList <string> uids)
        {
            await CheckConnectedAsync();

            IList <GmailMessageInfo> ids;

            if (uids.Count == 0)
            {
                return(new GmailMessageInfo[0]);
            }

            ids = new List <GmailMessageInfo>();

            // TODO: Consider comma joined list of UIDs
            await Client.GetFieldsAsync(uids[0], uids[uids.Count - 1], uid : true,
                                        fields : new[] { GConstants.LabelsHeader, GConstants.MessageIdHeader, GConstants.ThreadIdHeader, "UID", "FLAGS", "INTERNALDATE" },
                                        onFieldsReceived : fields =>
            {
                GmailMessageInfo messageInfo = new GmailMessageInfo()
                {
                    Uid       = fields["UID"],
                    Flags     = fields["Flags"],
                    MessageId = fields[GConstants.MessageIdHeader],
                    ThreadId  = fields[GConstants.ThreadIdHeader],
                    Labels    = FixUpLabels(fields[GConstants.LabelsHeader]),
                    Date      = Utilities.RemoveQuotes(fields["INTERNALDATE"]).ToNullDate() ?? DateTime.MinValue,
                };
                ids.Add(messageInfo);
            });

            return(ids);
        }
Ejemplo n.º 2
0
        public async Task <List <ConversationThread> > GetConversationsAsync(Scope scope, TimeSpan range, CancellationToken cancellationToken)
        {
            await CheckConnectedAsync();

            SearchCondition condition = SearchCondition.Since(DateTime.Now - range);

            string[] uids = await Client.SearchAsync(condition, uid : true);

            List <ConversationThread> conversations = new List <ConversationThread>();
            IList <MailMessage>       messages;

            if (uids.Length == 0)
            {
                messages = new MailMessage[0];
            }
            else
            {
                if (scope == Scope.HeadersAndBody || scope == Scope.Headers)
                {
                    // TODO: Consider comma joined list of UIDs
                    messages = await Client.GetMessagesAsync(uids[0], uids[uids.Length - 1], scope);
                }
                else if (scope == Scope.HeadersAndMime)
                {
                    messages = new List <MailMessage>();
                    IList <GmailMessageInfo> infos = await GetMessageIdsAsync(uids);

                    await GetEnvelopeAndStructureAsync(uids,
                                                       message =>
                    {
                        // Find the matching Ids
                        string messageId      = message.GetMessageId();
                        GmailMessageInfo info = infos.First(i => i.MessageId.Equals(messageId));

                        message.SetLabels(info.Labels);
                        message.SetFlags(info.Flags);
                        message.Uid = info.Uid;

                        messages.Add(message);

                        return(Task.FromResult(0));
                    },
                                                       cancellationToken);
                }
                else
                {
                    throw new NotSupportedException(scope.ToString());
                }
            }

            // Group by thread ID
            foreach (IGrouping <string, MailMessage> group in messages.GroupBy(message => message.GetThreadId()))
            {
                ConversationThread conversation = new ConversationThread(group.OrderByDescending(message => message.Date).ToList());
                conversation.Messages.ForEach(message => FixUpLabels(message));
                conversations.Add(conversation);
            }
            return(conversations.OrderByDescending(conversation => conversation.LatestDate).ToList());
        }
Ejemplo n.º 3
0
        public async Task<IList<GmailMessageInfo>> GetMessageIdsAsync(IList<string> uids)
        {
            await CheckConnectedAsync();

            IList<GmailMessageInfo> ids;
            if (uids.Count == 0)
            {
                return new GmailMessageInfo[0];
            }

            ids = new List<GmailMessageInfo>();

            // TODO: Consider comma joined list of UIDs
            await Client.GetFieldsAsync(uids[0], uids[uids.Count - 1], uid: true,
                fields: new[] { GConstants.LabelsHeader, GConstants.MessageIdHeader, GConstants.ThreadIdHeader, "UID", "FLAGS", "INTERNALDATE" },
                onFieldsReceived: fields =>
                {
                    GmailMessageInfo messageInfo = new GmailMessageInfo()
                    {
                        Uid = fields["UID"],
                        Flags = fields["Flags"],
                        MessageId = fields[GConstants.MessageIdHeader],
                        ThreadId = fields[GConstants.ThreadIdHeader],
                        Labels = FixUpLabels(fields[GConstants.LabelsHeader]),
                        Date = Utilities.RemoveQuotes(fields["INTERNALDATE"]).ToNullDate() ?? DateTime.MinValue,
                    };
                    ids.Add(messageInfo);
                });

            return ids;
        }