

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
- Set up your Nylas developer account and get your API keys
- Install the Nylas Java SDK
- Read Email Messages and Threads
- Search an Email Inbox
- Explore the Nylas Email API
Create Your Free Developer Account
Ready to build your email integration? Create your Nylas developer account to get started.
Setup Your Developer Account
After signing up for your Nylas developer account, follow our guide to get your API keys and authorize your first email account. When you complete this guide, you will have three tokens that you need to run the code examples below:
CLIENT_ID
- The CLIENT ID found on the dashboard page for your Nylas AppCLIENT_SECRET
- The CLIENT SECRET found on the dashboard page for your Nylas AppACCESS_TOKEN
- The access token provided when you authenticate an account to your Nylas App
Install the Nylas Java SDK
Note: 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 via 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 via 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");
}
}
Take Care With Your Secrets!
It’s not best practice 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 Vs. 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:[email protected]").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:[email protected]").get(0);
System.out.println(message.getSubject());
}
}
Explore the Nylas Email API
If you’ve made it to this point, congratulations! You’ve read email inbox data with the Nylas Email API! 🎉 There is plenty more that you can do with it; take a look at the following resources to learn more about the Nylas Communications Platform capabilities.
- Learn about some of the more important features of the Nylas Email API.
- Take a look at additional code examples for the Java SDK to see what else you can do with email, calendars, and contacts.
- Read about how the Nylas Communications Platform works.
- Review our app integration guide to learn about what it takes to integrate the Nylas Email API into your app.
Updated 6 months ago