public GMessage(GmailService service) { this.service = service; this.email = "me"; ResetParametres(); gDownloads = new GDownloads(); }
public void GetFile(string messageId, ref GRule gRule, Callback callback, Callback progressbar) { byte[] data = null; string filename = ""; var emailRequest = service.Users.Messages.Get("me", messageId); emailRequest.Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Full; var parts = emailRequest.Execute().Payload.Parts; if (parts != null) { foreach (var part in parts) { if (!String.IsNullOrEmpty(part.Filename)) { callback(part.Filename); string[] strs = Regex.Split(part.Filename, "[.]+"); string type = String.Empty; if (String.IsNullOrEmpty(gRule.type)) { type = strs[strs.Length - 1]; } else { type = gRule.type; } if (type == strs[strs.Length - 1]) { ++counterAllFiles; String attId = part.Body.AttachmentId; MessagePartBody attachPart = service.Users.Messages.Attachments.Get("me", messageId, attId).Execute(); String attachData = attachPart.Data.Replace('-', '+'); attachData = attachData.Replace('_', '/'); data = Convert.FromBase64String(attachData); string name = Path.Combine(gRule.path, part.Filename); if (File.Exists(name)) { File.Delete(name); } using (FileStream fileStream = new FileStream(name, FileMode.OpenOrCreate, FileAccess.Write)) { fileStream.Seek(0, SeekOrigin.Begin); int d = data.Length; int offset = 0; for (offset = 0; (data.Length - offset) > 500000; offset += 500000) { int l = data.Length - offset; fileStream.Write(data, offset, 500000); fileStream.Flush(); } fileStream.Write(data, offset, data.Length - offset); fileStream.Close(); } //File.WriteAllBytes(Path.Combine(gRule.path, part.Filename), data); filename = part.Filename; GDownloads gDownloads = new GDownloads(part.Filename, gRule.path, DateTime.Now); Act.FileParser.Add(GDownloads.pathfile, gDownloads); ++downloadFiles; } } } } gRule.lastMesId = messageId; ++processedMessages; progressbar(((processedMessages / allMessages) * 100)); //return filename; }