public bool GetAttachments(GmailService service, string userId, string messageId) { bool flag = false; //string outputDir = "temp"; try { Message message = service.Users.Messages.Get(userId, messageId).Execute(); if (message.Payload.Parts != null) { IList <MessagePart> parts = message.Payload.Parts; foreach (MessagePart part in parts) { if (!String.IsNullOrEmpty(part.Filename)) { String attId = part.Body.AttachmentId; MessagePartBody attachPart = service.Users.Messages.Attachments.Get(userId, messageId, attId).Execute(); String attachData = attachPart.Data.Replace('-', '+'); attachData = attachData.Replace('_', '/'); byte[] data = Convert.FromBase64String(attachData); string filetmp = Path.Combine(pathinit, part.Filename); File.WriteAllBytes(filetmp, data); attach.file = part.Filename; attach.source = filetmp; attach.CheckPath(); flag = attach.Move(); } } } } catch (Exception e) { Console.WriteLine("An error occurred: " + e.Message); } return(flag); }
private IList <UniqueId> ListUid(IMailFolder mailFolder) { mailFolder.Open(FolderAccess.ReadOnly); IList <UniqueId> list = mailFolder.Search(SearchQuery.All); Console.WriteLine("Folder{0} ", mailFolder.Name); foreach (UniqueId item2 in list) { try { MimeMessage message = mailFolder.GetMessage(item2); foreach (var attachment in message.Attachments) { if (attachment is MessagePart) { var fileName = attachment.ContentDisposition?.FileName ?? attachment.ContentType.Name; var rfc822 = (MessagePart)attachment; rfc822.Message.WriteTo(Path.Combine(this._pathinit, fileName)); attach.file = fileName; attach.source = Path.Combine(this._pathinit, fileName); attach.CheckPath(); attach.Move(); } else { var part = (MimePart)attachment; var fileName = part.FileName; using (var stream = File.Create(Path.Combine(this._pathinit, fileName))) { part.Content.DecodeTo(stream); } attach.file = fileName; attach.source = Path.Combine(this._pathinit, fileName); attach.CheckPath(); attach.Move(); } } var getChilds = mailFolder.GetSubfolders(StatusItems.None, false); if (getChilds.Count() > 0) { foreach (var item in getChilds) { if (item.Name == ".before attachs" || item.Name == ".before heart") { continue; } else { this.ListUid(item); } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } } return(list); }