title: Google API - Gmail. Downloading attachments
body: <p>When calling this method please help by advising what values to pass through such as GmailService? Im guessing userID is the Gmail account and the messageID( i want to download all of them) .</p>
  
  <p>How can i change this to download all the attachments in the inbox.</p>
  
  <p>Thank you in advance and I hope someone can help me.</p>
  
  <p>Method im using is below.</p>
  
  <pre><code>public static void GetAttachments(GmailService service, String userId, String messageId, String outputDir)
  {
    try
    {
      Message message = service.Users.Messages.Get(userId, messageId).Execute();
      IList&lt;MessagePart&gt; 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();
  
          // Converting from RFC 4648 base64 to base64url encoding
          // see http://en.wikipedia.org/wiki/Base64#Implementations_and_history
          String attachData = attachPart.Data.Replace('-', '+');
          attachData = attachData.Replace('_', '/');
  
          byte[] data = Convert.FromBase64String(attachData);
          File.WriteAllBytes(Path.Combine(outputDir, part.Filename), data);                        
         }
       }
     }
     catch (Exception e)
     {
       Console.WriteLine("An error occurred: " + e.Message);
     }
   }
  </code></pre>