Only show these results:

Read an Inbox with Java

Learn how to read emails from Gmail, Outlook, and Exchange with the Nylas Java SDK. Build your email integration in 15 minutes.

Read an Inbox with Java

Java has been one of the most popular programming languages for many years now because of its ability to run on nearly any platform, gigantic selection of libraries, and high level of reliability. It’s no wonder that it’s an extremely popular language for enterprise applications, and its use in Android development has further cemented it as a major player in the software development industry.

The Nylas Email API connects to all major providers, including Gmail, Outlook, Office365, Exchange, Yahoo, and more, and our Java SDK makes it simple to read data from an email inbox.

This tutorial explains how to use the Nylas Java SDK and Email API to read and search through inbox emails. It covers the following functionality

  1. Set up your Nylas developer account and get your API keys
  2. Install the Nylas Java SDK
  3. Read Email Messages and Threads
  4. Search an Email Inbox
  5. Explore the Nylas Email API

Prerequisites

Before you can start using the Nylas Java SDK, make sure you have done the following:

  • Sign up for your developer account.
  • Get your developer keys. You need to have your:
    • CLIENT_ID - The CLIENT ID found on the dashboard page for your Nylas App.
    • CLIENT_SECRET - The CLIENT SECRET found on the dashboard page for your Nylas App.
    • ACCESS_TOKEN - The access token provided when you authenticate an account to your Nylas App.

Install the Nylas Java SDK

The Nylas Java SDK requires Java 8 or above.

For the following examples, replace X.X.X with the version you want to use. Take a look at the list of releases to learn about the versions that are available.

Setup with Gradle

If you're using Gradle, add the following to your dependencies section of build.gradle:

implementation("com.nylas.sdk:nylas-java-sdk:X.X.X")   

Setup with Maven

For projects using Maven, add the following to your POM file:

<dependency>
<groupId>com.nylas.sdk</groupId>
<artifactId>nylas-java-sdk</artifactId>
<version>X.X.X</version>
</dependency>

Configure the API Client

At its core, the Nylas Communication Platform is an API client that interfaces with all of the major email providers. First, import the NylasClient class and create a new instance of this class. Then, import the Account class and create an instance of it, passing the access token you gathered when you got your developer API keys. In the following example, replace ACCESS_TOKEN with the appropriate value.

    import java.io.IOException;

import com.nylas.RequestFailedException;
import com.nylas.NylasClient;
import com.nylas.Account;

public class ApiClient {
public static void main(String[] args) throws IOException, RequestFailedException {
NylasClient nylas = new NylasClient();
Account account = nylas.account("ACCESS_TOKEN");
}
}

Be Careful with Secrets!

Follow best practices to include secrets like this in your code. A more secure way to provide these values is to use a KeyStore to protect sensitive information.

Read Data From an Email Inbox

Messages Versus Threads

Before reading content from your email inbox, it’s important to understand the difference between messages and threads.
Messages are the fundamental object of the Nylas platform, and the core building block for most email applications. They contain several pieces of information, such as when a message was sent, the sender's address, to whom it was sent, and the message body. They can also contain files (attachments), calendar event invitations, and more.
Threads are first-class objects that represent a collection of messages. Messages are threaded together with a variety of heuristics to match the format of third-party accounts as closely as possible.

Now, let’s take a look at how to read messages and threads from an email inbox.

Reading Messages and Threads

First, we’ll create a new Message object using messages.list(), passing a MessageQuery object that returns a list containing only the most recent email message.

    Message message = account.messages.list(new MessageQuery().limit(1)).get(0);   

Now, we’ll print out the message subject, unread status, and ID.

    System.out.printf("Subject: %s | Unread: %s | ID: %s\n", 
message.getSubject(),
message.getUnread(),
message.getId()
);

Take a look at the API reference for the Messages endpoint to learn more about that attributes that are available.

Next, we’ll use account.threads() a ThreadQuery object to return a list of the 5 most recent unread threads in the user’s inbox.

    ThreadQuery unread = new ThreadQuery().limit(5).unread(true);        
List<Thread> threads = account.threads().list(unread);

Finally, print the subject of the unread threads and a list of the participant’s emails addresses. The NameEmail class is used for the latter.

    for (Thread thread : threads) {            
StringBuilder participants = new StringBuilder();
for (NameEmail participant : thread.getParticipants()) {
participants.append(participant.getEmail()).append(" ");
}
System.out.printf("Subject: %s | Participants: %s\n",
thread.getSubject(),
participants
);
}

For more information about what you can do with the Threads endpoint, please take a look at the API reference.

Searching Messages and Threads

The search sub-endpoint is used to run a full-text search, that is proxied to the account's provider. Results are matched with objects that have been synced, and then returned. Search functionality is available for messages and threads, and the following example demonstrates how to search for messages that are from a specific email address.

    // Search for the most recent email from a specific address
Message message = account.messages().search("from:swag@nylas.com").get(0);
System.out.println(message.getSubject());

Here is the entire code example for searching for and reading messages and threads from an email inbox.

    import com.nylas.NylasAccount;
import com.nylas.NylasClient;
import java.util.List;
import com.nylas.Message;
import com.nylas.MessageQuery;
import com.nylas.Thread;
import com.nylas.ThreadQuery;
import com.nylas.NameEmail;

public class ReadMessagesThreads {
public static void main(String[] args) throws Exception {
// Create client object and connect it to Nylas using
// an account's access token
NylasClient client = new NylasClient();
// Provide the access token for a specific account
NylasAccount account = client.account("ACCESS_TOKEN");

// Return the most recent email message
Message message = messages.list(new MessageQuery().limit(1)).get(0);
System.out.printf("Subject: %s | Unread: %s | ID: %s\n",
message.getSubject(),
message.getUnread(),
message.getId()
);

// Return the 5 most recent unread threads.
ThreadQuery unread = new ThreadQuery().limit(5).unread(true);
List<Thread> threads = account.threads().list(unread);

for (Thread thread : threads) {
StringBuilder participants = new StringBuilder();
for (NameEmail participant : thread.getParticipants()) {
participants.append(participant.getEmail()).append(" ");
}
System.out.printf("Subject: %s | Participants: %s\n",
thread.getSubject(),
participants
);
}
// Search for the most recent email from a specific address
Message message = account.messages().search("from:swag@nylas.com").get(0);
System.out.println(message.getSubject());
}
}