private async void GetAttachmentsUpdate(string updateQuery, string roothPath) { LockControl(); try { Console.SetOut(new ConsoleWriter(richTextBoxDebug)); Console.WriteLine("Starting to download..."); _messages = _service.GetListMessages("me", updateQuery); var filenames = new List <string>(); if (_messages != null && _messages.Count > 0) { CountStatus(); foreach (var message in _messages) { var msg = _service.GetMessage("me", message.Id); if (msg.LabelIds.Contains("UNREAD")) { foreach (var head in msg.Payload.Headers) { if (head.Name == "Subject") { filenames.Add(head.Value.GetSubject()); var root = roothPath; if (!Directory.Exists(root + head.Value.GetSubject())) { Directory.CreateDirectory(root + head.Value.GetSubject()); _gateways++; } try { Task <bool> T_attachmentResult = Task.Factory.StartNew(() => _service.GetAttachments("me", message.Id, root + head.Value.GetSubject())); T_attachmentResult.ContinueWith((antecedent) => { //_service.GetAttachments("me", message.Id, root + head.Value.GetSubject()); _files++; progressBarStatus.Invoke((Action) delegate { progressBarStatus.Increment(1); }); _service.GetModifyMessage("me", message.Id, new List <string>() { "UNREAD" }); //Console.WriteLine("Message mark as read."); }); //Task.WaitAny(T_attachmentResult); //if (T_attachmentResult.Result) //{ // _files++; // progressBarStatus.Invoke((Action)delegate { progressBarStatus.Increment(1); }); //} //_service.GetAttachments("me", message.Id, root + head.Value.GetSubject()); //_files++; //progressBarStatus.Invoke((Action)delegate { progressBarStatus.Increment(1); }); //_service.GetModifyMessage("me", message.Id, new List<string>() { "UNREAD" }); //Console.WriteLine("Message mark as read."); } catch (Exception e) { log.Debug(e); Console.WriteLine(e); throw; } var oks = "File Download Successfully " + message.Id; //Console.WriteLine(oks); log.Info(oks); } } } //Console.WriteLine(message.LabelIds); Debug.WriteLine(message.Id.Length); } } else { Console.WriteLine("No message found."); log.Info("No message found."); } labelFile.Invoke((Action) delegate { labelFile.Text = "No. of downloaded file: " + _files; }); labelGateway.Invoke((Action) delegate { labelGateway.Text = "No. of gateway: " + filenames.Distinct().Count(); }); if (_files == 0 && filenames.Distinct().Count() == 0) { labelStatus.Invoke((Action) delegate { labelStatus.Text = "Status: Files are up to date"; labelStatus.ForeColor = Color.Green; }); notifyIcon.BalloonTipText = "Files are up to date"; } else { labelStatus.Invoke((Action) delegate { labelStatus.Text = "Status: Completed"; labelStatus.ForeColor = Color.Green; }); notifyIcon.BalloonTipText = "New updates is now downloaded"; _files = 0; } notifyIcon.Visible = true; notifyIcon.ShowBalloonTip(500); } catch (Exception e) { Console.WriteLine(e); labelStatus.Invoke((Action) delegate { labelStatus.Text = "Status: Error"; labelStatus.ForeColor = Color.Red; }); log.Debug(e); throw; //check the bug here } UnlockControl(); }
public static IList <Message> GetMessages(this GmailService service, int limit = 0, DateTime?start = null, DateTime?end = null, string label = null) { List <Message> result = new List <Message>(); try { var request = service.Users.Messages.List("me"); do { var response = request.Execute(); foreach (Message partial in response.Messages) { Message message = partial; message = service.GetMessage(partial.Id); if (isInDateRange(message)) { result.Add(message); } if (doesLabelMatch(message)) { result.Add(message); } #if DEBUG message.Snippet.LogInDebug(); #endif } request.PageToken = response.NextPageToken; } while (isAtLimit(request)); return(result); } catch (Exception ex) { throw ex; } bool doesLabelMatch(Message m) { return(label == null ? true : m.LabelIds != null?m.LabelIds.Any(b => b == label) : false); } bool isAtLimit(UsersResource.MessagesResource.ListRequest r) { bool isTrue = string.IsNullOrEmpty(r.PageToken);; if (limit > 0) { isTrue = limit <= result.Count; } return(isTrue); } bool isInDateRange(Message m) { DateTime emailDate = m.InternalDate.HasValue ? DateTimeOffset.FromUnixTimeMilliseconds(m.InternalDate.Value).DateTime : default(DateTime); bool isTrue = false; if (start != null && end != null && emailDate == default(DateTime)) { isTrue = true; } else if (start != null && end != null) { isTrue = true; } else if (emailDate == default(DateTime)) { isTrue = false; } else if (end != null) { isTrue = end >= emailDate; } else if (start != null) { isTrue = emailDate >= start; } else if (start != null && end != null) { isTrue = emailDate >= start && end >= emailDate; } return(isTrue); } }
static void GetMessage(GmailService gmailService, Token token, string messageId) { var result = gmailService.GetMessage(token, messageId); }