Google mail api android

Gmail API Overview

The Gmail API is a RESTful API that can be used to access Gmail mailboxes and send mail. For most web applications the Gmail API is the best choice for authorized access to a user’s Gmail data and is suitable for various applications, such as:

  • Read-only mail extraction, indexing, and backup
  • Automated or programmatic message sending
  • Email account migration
  • Email organization including filtering and sorting of messages
  • Standardization of email signatures across an organization

Note: The Gmail API shouldn’t be used to replace IMAP for developing a full-fledged email client. Instead, see IMAP, POP, and SMTP.

Following is a list of common terms used in the Gmail API:

Message An email message containing the sender, recipients, subject, and body. After a message has been created, a message cannot be changed. A message is represented by a message resource. Thread A collection of related messages forming a conversation. In an email client app, a thread is formed when one or more recipients respond to a message with their own message. Label

A mechanism for organizing messages and threads. For example, the label «taxes» might be created and applied to all messages and threads having to do with a user’s taxes. There are two types of labels:

System labels Internally-created labels, such as INBOX , TRASH , or SPAM . These labels cannot be deleted or modified. However, some system labels, such as INBOX can be applied to, or removed from, messages and threads. User labels Labels created by a user. These labels can be deleted or modified by the user or an application. A user label is represented by a label resource. Draft

An unsent message. A message contained within the draft can be replaced. Sending a draft automatically deletes the draft and creates a message with the SENT system label. A draft is represented by a draft resource.

Next steps

To get started with Gmail API:

To learn about developing with Google Workspace APIs, including handling authentication and authorization, refer to Get started as a Workspace developer.

To learn how to configure and run a simple Gmail API app, read the Quickstarts overview.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Источник

Java Quickstart

Complete the steps described in the rest of this page to create a simple Java command-line application that makes requests to the Gmail API.

Prerequisites

To run this quickstart, you need the following prerequisites:

  • A Google account with Gmail enabled.

Step 1: Prepare the project

To prepare the project:

In your working directory, run the following commands to create a new project structure:

Copy the credentials.json file you downloaded as a prerequisite into the newly-created src/main/resources/ directory.

Open the default build.gradle file and replace its contents with the following code:

Читайте также:  Android tv box root права

Step 2: Set up the sample

To set up the sample:

    In src/main/java/ , create a new Java file with a name that matches the mainClassName value in your build.gradle file.

Include the following code in your new Java file:

Step 3: Run the sample

To run the sample:

Execute the following command:

This command builds and runs the sample.

(optional). If this is your first time running the sample, the sample opens a new window prompting you to authorize access to your data:

The sample executes.

If you have problems, refer to the Troubleshoot the sample section.

Troubleshoot the sample

This section describes a common issue that you can encounter while attempting to run this quickstart.

This app isn’t verified

If the OAuth consent screen displays the warning «This app isn’t verified,» your app is requesting scopes that provide access to sensitive user data. If your application uses sensitive scopes, your your app must go through the verification process to remove that warning and other limitations. During the development phase you can continue past this warning by clicking Advanced > Go to (unsafe).

Further reading

For further information on the APIs used in this quickstart, refer to the following:

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Источник

Sending Email

There are two ways to send email using the Gmail API:

  • You can send it directly using the messages.send method.
  • You can send it from a draft, using the drafts.send method.

Emails are sent as base64url encoded strings within the raw property of a message resource. The high-level workflow to send an email is to:

  1. Create the email content in some convenient way and encode it as a base64url string.
  2. Create a new message resource and set its raw property to the base64url string you just created.
  3. Call messages.send , or, if sending a draft, drafts.send to send the message.

The details of this workflow can vary depending on your choice of client library and programming language.

Creating messages

The Gmail API requires MIME email messages compliant with RFC 2822 and encoded as base64url strings. Many programming languages have libraries or utilities that simplify the process of creating and encoding MIME messages. The following code examples demonstrate how to create a MIME message using the Google APIs client libraries for various languages.

Creating an email message can be greatly simplified with the MimeMessage class in the javax.mail.internet package. The following example shows how to create the email message, including the headers:

The next step is to encode the MimeMessage , instantiate a Message object, and set the base64url encoded message string as the value of the raw property.

Python

The following code sample demonstrates creating a MIME message, encoding to a base64url string, and assigning it to the raw field of the Message resource:

Creating messages with attachments

Creating a message with an attachment is like creating any other message, but the process of uploading the file as a multi-part MIME message depends on the programming language. The following code examples demonstrate possible ways of creating a multi-part MIME message with an attachment.

The following example shows how to create a multi-part MIME message, the encoding and assignment steps are the same as above.

Python

Similar to the previous example, this example also handles encoding the message to base64url and assigning it to the raw field of the Message resource.

Читайте также:  Как переслать скриншот с андроида

Sending messages

Once you have created a message, you can send it by supplying it in the request body of a call to messages.send , as demonstrated in the following examples.

Python

If you’re trying to send a reply and want the email to thread, make sure that:

  1. The Subject headers match
  2. The References and In-Reply-To headers follow the RFC 2822 standard.

For information on sending a message from a draft, see Creating Drafts.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Источник

Send Emails with Gmail API

Updated on May 6, 2020

Gmail is one of the most popular email services so far, and you will very probably want to use it as a mailbox for your web or mobile app. It is safe and credible, which is crucial to prevent your emails from going into the spam folder. That’s why we decided to flesh out how to send emails with Gmail API.

Gmail API – why you should consider using it

The API provides you with a RESTful access to the features you usually have with Gmail:

  • Send and receive HTML emails
  • Send and receive emails with attachments
  • CRUD (create, read, update, and delete) operations with messages, drafts, threads, and labels
  • Access control of your Gmail inbox
  • Full search capabilities of the web UI
  • Perform specific queries
  • And many more…

Developers love Gmail API because it’s easy to implement. We’ll talk about that a bit later. Also, you can use this option for versatile cases like:

  • automated email sending
  • mail backup
  • mail migration from other email services

Resource types and methods

With Gmail API, you can deal with several resource types and manage them using the following methods:

Resource type Method
Draft
an unsent message that you can modify once created
  • create (creating a new draft)
  • delete (removing the specified draft)
  • get (obtaining the specified draft)
  • list (listing drafts in the mailbox)
  • send (sending the specified draft according to the To, Cc, and Bcc headers)
  • update (updating the specified draft’s content)
  • Message
    an immutable resource that you cannot modify
  • batchDelete (removing messages by message ID)
  • batchModify (modifying labels on the specified messages)
  • delete (removing the specified message)
  • get (obtaining the specified message)
  • import (importing the message into the mailbox (similar to receiving via SMTP))
  • insert (inserting the message into the mailbox (similar to IMAP)
  • list (listing messages in the mailbox)
  • modify (modifying labels on the specified message)
  • send (sending the specified message according to the To, Cc, and Bcc headers)
  • trash (transferring the specified message to the trash)
  • untrash (transferring the specified message from the trash)
  • Thread
    a collection of messages within a single conversation
  • delete (removing the specified thread)
  • get (obtaining the specified thread)
  • list (listing threads in the mailbox)
  • modify (modifying labels in the thread)
  • trash (transferring the specified thread to the trash)
  • untrash (transferring the specified thread from the trash)
  • Label
    a resource to organize messages and threads (for example, inbox, spam, trash, etc.)
  • create (creating a new label)
  • delete (removing the specified label)
  • get (obtaining the specified label)
  • list (listing labels in the mailbox)
  • patch (patching the specified label) – this method supports patch semantics
  • update (updating the specified label).
  • History
    a collection of changes made to the mailbox
  • list (listing the history of all changes to the mailbox)
  • Settings
    setting up Gmail features
  • getAutoForwarding (auto-forwarding setting)
  • updateAutoForwarding (updating the auto-forwarding setting)
  • getImap (IMAP settings)
  • updateImap (updating IMAP settings)
  • getLanguage (language settings)
  • updateLanguage (updating language settings)
  • getPop (POP3 settings)
  • updatePop (updating POP3 settings)
  • getVacation (vacation responder settings)
  • updateVacation (updating vacation responder settings)
  • How to make your app send emails with Gmail API

    Step 1: Create a project at Google API Console

    If you want to have access to your Gmail from your mobile or web app, you should start with Google Developers Console. Those who visit this page for the first time ever will have to agree with the Terms of Service and pick their Country of residence. Then click Select a project and create a new one.

    Name your new project and press Create at the bottom.

    Step 2: Enable Gmail API

    Once that’s done, you can press the Library tab on the left and find yourself in the API Library page. Enter “Gmail API” in the search bar and click on it once found. Now, you need to enable the API for your project.

    Note that you’ll have to enable it separately for each new project you work on.

    Step 3: Credentials and authentication with OAuth 2.0

    Once the API is enabled, you’ll be taken to a nice dashboard that says, “To use this API, you may need credentials”. If you click Create credentials, you’ll have to pass through a set of questions to find out what kind of credentials you need. We advise you to go another way since we already know what it is: OAuth client ID. So, click the Credential tab on the left, and then pick OAuth client ID from the drop-down list of the Create Credentials button.

    You’ll see the Configure consent screen button. It will bring you to a page with many fields. You can just enter the name of your app and specify authorized domains. Fill in other fields if you want.

    Click save and then pick the type of your app (web app, Android, Chrome App, iOS, or other). After that, name your OAuth Client ID. Also, enter JavaScript origins and redirect domains for use with requests from a browser or a web server respectively. Click create to finalize. That’s it. Download a JSON file with your credentials – you’ll need it later.

    Step 4: Pick a quickstart guide

    The next step is to select a quickstart guide according to the technology your app is built with. So far, there are the following options:

    For mobile apps, there are G Suite APIs for iOS and Android as well.

    What you need first in this quickstart guide is the Prerequisites section. Let’s say your choice is PHP. In this case, make sure your PHP version corresponds to the given one. Also, install the JSON extension and the Composer dependency management tool if you haven’t already. After that, you can install the Google Client Library. For Java, you’ll need to create a new project structure and the src/main/resources/ directory. Then, copy the JSON file with credentials to this directory and replace the content of the build.gradle file with this code. So, pay attention when preparing your project.

    Step 5: API client library

    Google provides client libraries to work with the API:

    Источник

    Читайте также:  Не могу установить андроид авто
    Оцените статью