Users.drafts: list

Requires authorization

Lists the drafts in the user's mailbox. Try it now or see an example.

Request

HTTP request

GET https://www.googleapis.com/gmail/v1/users/userId/drafts

Parameters

Parameter name Value Description
Path parameters
userId string The user's email address. The special value me can be used to indicate the authenticated user.
Optional query parameters
includeSpamTrash boolean Include drafts from SPAM and TRASH in the results. (Default: false)
maxResults unsigned integer Maximum number of drafts to return.
pageToken string Page token to retrieve a specific page of results in the list.
q string Only return draft messages matching the specified query. Supports the same query format as the Gmail search box. For example, "from:someuser@example.com rfc822msgid: is:unread".

Authorization

This request requires authorization with at least one of the following scopes (read more about authentication and authorization).

Scope
https://mail.google.com/
https://www.googleapis.com/auth/gmail.modify
https://www.googleapis.com/auth/gmail.compose
https://www.googleapis.com/auth/gmail.readonly

Request body

Do not supply a request body with this method.

Response

If successful, this method returns a response body with the following structure:

{
  "drafts": [
    users.drafts Resource
  ],
  "nextPageToken": string,
  "resultSizeEstimate": unsigned integer
}
Property name Value Description Notes
drafts[] list List of drafts.
nextPageToken string Token to retrieve the next page of results in the list.
resultSizeEstimate unsigned integer Estimated total number of results.

Examples

Note: The code examples available for this method do not represent all supported programming languages (see the client libraries page for a list of supported languages).

Java

Uses the Java client library.

import com.google.api.services.gmail.Gmail;
import com.google.api.services.gmail.model.ListDraftsResponse;
import com.google.api.services.gmail.model.Draft;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

// ...

public class MyClass {

  // ...

  /**
   * List the drafts in the user's account.
   *
   * @param service Authorized Gmail API instance.
   * @param userId User's email address. The special value "me"
   * can be used to indicate the authenticated user.
   * @throws IOException
   */
  public static void listDrafts(Gmail service, String userId) throws IOException {
    ListDraftsResponse response = service.users().drafts().list(userId).execute();
    List<Draft> drafts = response.getDrafts();
    for (Draft draft : drafts) {
      System.out.println(draft.toPrettyString());
    }
  }

  // ...

}

.NET

Uses the .NET client library.

using Google.Apis.Gmail.v1;
using Google.Apis.Gmail.v1.Data;

using System.Collections.Generic;

// ...

public class MyClass {

  // ...

  /// <summary>
  /// List the drafts in the user's mailbox.
  /// </summary>
  /// <param name="service">Gmail API service instance.</param>
  /// <param name="userId">User's email address. The special value "me"
  /// can be used to indicate the authenticated user.</param>
  public static List<Draft> ListDrafts(GmailService service, String userId)
  {
      List<Draft> result = new List<Draft>();

      try
      {
          ListDraftsResponse response = service.Users.Drafts.List(userId).Execute();
          result.AddRange(response.Drafts);
      }
      catch (Exception e)
      {
          Console.WriteLine("An error occurred: " + e.Message);
      }

      return result;
  }

  // ...

}

PHP

Does not use a client library.

/**
 * Retrieve a List of Drafts.
 *
 * @param  Google_Service_Gmail $service Authorized Gmail API instance.
 * @param  string $userId User's email address. The special value 'me'
 * can be used to indicate the authenticated user.
 * @return Array of Drafts.
 */
function listDrafts($service, $userId) {
  $drafts = array();

  try {
    $draftsResponse = $service->users_drafts->listUsersDrafts($userId);
    if ($draftsResponse->getDrafts()) {
      $drafts = array_merge($drafts, $draftsResponse->getDrafts());
    }
  } catch (Exception $e) {
    print 'An error occurred: ' . $e->getMessage();
  }

  foreach ($drafts as $draft) {
    print 'Draft with ID: ' . $draft->getId() . '<br/>';
  }

  return $drafts;
}

Python

Uses the Python client library.

"""Get a list of Drafts from the user's mailbox.
"""

from apiclient import errors


def ListDrafts(service, user_id):
  """Get a list of all drafts in the user's mailbox.

  Args:
    service: Authorized Gmail API service instance.
    user_id: User's email address. The special value "me"
    can be used to indicate the authenticated user.

  Returns:
    A list of all Drafts in the user's mailbox.
  """
  try:
    response = service.users().drafts().list(userId=user_id).execute()
    drafts = response['drafts']
    for draft in drafts:
      print 'Draft id: %s' % draft['id']
    return drafts
  except errors.HttpError, error:
    print 'An error occurred: %s' % error

JavaScript

Does not use a client library.

/**
 * Retrieve an array of Drafts.
 *
 * @param  {String} userId User's email address. The special value 'me'
 * can be used to indicate the authenticated user.
 * @param  {Function} callback Function to call when the request is complete.
 */
function listDrafts(userId, callback) {
  var request = gapi.client.gmail.users.drafts.list({
    'userId': userId
  });
  request.execute(function(resp) {
    var drafts = resp.drafts;
    callback(drafts);
  });
}

Try it!

Use the APIs Explorer below to call this method on live data and see the response.

フィードバックを送信...

ご不明な点がありましたら、Google のサポートページをご覧ください。