title: Sending email with attachments using Gmail API in Android (execute() hangs)
body: <p>Everything seems to work as long as the attachment is small.<br>
  However, as I try to attach a larger file (7MB for example), the <code>execute()</code> method of <code>Send</code> just hangs.<br>
  I tried to go over the documentation and if I understand correctly I should use a <code>send</code> <a href="https://developers.google.com/gmail/api/v1/reference/users/messages/send" rel="nofollow">API which actually performs <code>upload</code></a>  however, I didn't figure where I should provide these parameters.<br>
  Here is the email generation method :</p>
  
  <pre><code>public MimeMessage toMimeMessage(String from, Context context) throws MessagingException {
      Properties props = new Properties();
      Session session = Session.getDefaultInstance(props, null);
  
      MimeMessage mimeMessage = new MimeMessage(session);
  
      mimeMessage.setFrom(new InternetAddress(from));
      mimeMessage.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(recipient));
      mimeMessage.setSubject(subject);
  
      MimeBodyPart mimeBodyText = new MimeBodyPart();
      mimeBodyText.setContent(body, "text/html");
      mimeBodyText.setHeader("Content-Type", "text/plain; charset=\"UTF-8\"");
  
      Multipart mp = new MimeMultipart();
      mp.addBodyPart(mimeBodyText);
  
      if (attachments != null &amp;&amp; attachments.size() &gt; 0) {
          MimeBodyPart mimeBodyAttachments = new MimeBodyPart();
          for (Uri uri : attachments) {
              String fileName = UriUtils.getFileName(uri, context);
              String mimeType = UriUtils.getMimeType(uri, context);
              Log.d(TAG, "Generating file info, uri=" + uri.getPath() + ", mimeType=" + mimeType);
              FileInputStream is = UriUtils.generateFileInfo(context, uri, mimeType);
              if (is == null) {
                  throw new MessagingException("Failed to get file for uri=" + uri.getPath());
              }
              try
              {
                  DataSource source = new ByteArrayDataSource(is, mimeType);
                  mimeBodyAttachments.setDataHandler(new DataHandler(source));
                  mimeBodyAttachments.setFileName(fileName);
                  mimeBodyAttachments.setHeader("Content-Type", mimeType + "; name=\"" + fileName + "\"");
              } catch (IOException e) {
                  throw new MessagingException(e.getMessage());
              }
          }
          mimeBodyAttachments.setHeader("Content-Transfer-Encoding", "base64");
          mimeBodyAttachments.setDisposition(MimeBodyPart.ATTACHMENT);
          mp.addBodyPart(mimeBodyAttachments);
      }
  
      mimeMessage.setContent(mp);
  
      return mimeMessage;
  }
  </code></pre>
  
  <p>.</p>
  
  <pre><code>Message createMessageWithEmail(MimeMessage mimeMessage) throws MessagingException, IOException {
          ByteArrayOutputStream bytes = new ByteArrayOutputStream();
          mimeMessage.writeTo(bytes);
          String encodedEmail = Base64.encodeBase64URLSafeString(bytes.toByteArray());
          Message message = new Message();
          message.setRaw(encodedEmail);
          return message;
      }
  </code></pre>
  
  <p>Followed by :</p>
  
  <pre><code>MimeMessage mimeMessage = email.toMimeMessage(userId, context);
  Message message = createMessageWithEmail(mimeMessage);
  Gmail.Users.Messages messages = service.users().messages();
  Gmail.Users.Messages.Send send = messages.send(userId, message);
  send.execute(); // method hangs when using large attachment
  </code></pre>