Sample code for 30+ languages & platforms
Java

Move a GMail Message to Trash

See more GMail REST API Examples

Moves a specific GMail email message to trash.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Http http = new Http();
        http.setAuthToken("GMAIL-ACCESS-TOKEN");

        // The id of the GMail message to move to Trash.
        String id = "16678c485e7f0a0c";
        String userId = "me";

        http.setUrlVar("userId", "me");
        http.setUrlVar("id", id);

        // Move to trash by POSTing w/ an empty request body.
        String url = "https://www.googleapis.com/gmail/v1/users/{$userId}/messages/{$id}/trash";
        HttpResponse resp = new HttpResponse();
        try {
            http.httpStr("POST", url, "", "", "", resp);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("status = " + resp.getStatusCode());

        // A 200 response status indicate success.
        if (resp.getStatusCode() != 200) {
            System.out.println(resp.getBodyStr());
            System.out.println("Failed.");
            return;
        }

        // A successful repsonse contains JSON that looks like this:

        // {
        //  "id": "16678c485e7f0a0c",
        //  "threadId": "16678c485e7f0a0c",
        //  "labelIds": [
        //   "TRASH",
        //   "CATEGORY_SOCIAL"
        //  ]
        // }

        System.out.println("response body:");
        System.out.println(resp.getBodyStr());

        System.out.println("Message moved to trash!");
    }
}