示例#1
0
        public Message GetMessage(string traceId, string messageId)
        {
            if (MessageCache.TryGetValue(messageId, out var msg))
            {
                return(msg);
            }

            var request = _service.Users.Messages.Get("me", messageId);

            request.Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Full;

            var gMessage = request.Execute();

            List <Header> headers   = GetHeaders(gMessage.Payload).ToList();
            List <string> bodyParts = GetBody(gMessage.Payload).ToList();

            var message = new Message
            {
                Id      = gMessage.Id,
                Body    = string.Join(Environment.NewLine, bodyParts),
                Headers = headers
            };

            List <Tuple <string, string> > attachmentIds = GetAttachmentIds(gMessage.Payload).ToList();

            foreach (Tuple <string, string> attachmentId in attachmentIds)
            {
                try
                {
                    byte[] data = GetAttachment(messageId, attachmentId.Item1);
                    message.Attachments.Add(new Attachment
                    {
                        Id        = attachmentId.Item1,
                        Name      = attachmentId.Item2,
                        MessageId = messageId,
                        Data      = data
                    });
                }
                catch (Exception ex)
                {
                    _log.Info(traceId, "GetMessage.Attachment.Exception", $"id={attachmentId.Item1}, name={attachmentId.Item2}, msgId={message.Id}, exception=\"{ex.Message}\"");
                }
            }

            MessageCache.AddOrUpdate(messageId, message, (id, m) => message);
            return(message);
        }
示例#2
0
        public List <Message> GetMessagesWithLabel(string traceId, params string[] labelIds)
        {
            var request = _service.Users.Messages.List("me");

            request.LabelIds = labelIds;

            var messageIds = request.Execute().Messages.Select(m => m.Id).ToList();
            var messages   = new ConcurrentBag <Message>();

            Parallel.ForEach(messageIds, id =>
            {
                Message message = GetMessage(traceId, id);
                messages.Add(message);
            });

            return(messages.ToList());
        }