Inbox Folders and Labels with Java
Learn how to create email folders and labels and organize inboxes with the Nylas Java SDK. Build your email integration in 15 minutes.
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 manage inbox folders and labels.
This tutorial explains how to use the Nylas Java SDK and Email API to manage inbox folders and labels. It covers the following functionality
- Set up your Nylas developer account and get your API keys
- Install the Nylas Java SDK
- Manage Email Folders and Labels
- 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.
View Labels and Folders for an Email Account
Depending on the account provider, there are two possible ways a user's email inbox can be organized: folders and labels. Gmail uses labels and all other providers, including Microsoft, Yahoo, and IMAP providers, use folders. Nylas makes it easy to detect the organization method an account uses by providing an organization_unit
field on the account, which returns either folder
or label
. Take a look at the API reference for the Account endpoint to learn more about the values it returns.
The following code uses .getOrganizationUnit()
to detect if the account uses folders or labels and and returns the display_name
for all folders or labels the account has.
if (accountDetail.getOrganizationUnit().equals("label")) {
List<Label> labels = account.labels().list();
labels.stream().forEach((Label label) -> {
System.out.println(label.getDisplayName());
});
}else if (accountDetail.getOrganizationUnit().equals("folder")){
List<Folder> folders = account.folders().list();
folders.stream().forEach((Folder folder) -> {
System.out.println(folder.getDisplayName());
});
}
Create a Label or Folder
Now, it’s time to create a new label or folder. In the following example, change the value of the string passed to account.labels().create()
to your preferred name.
if (accountDetail.getOrganizationUnit().equals("label")) {
Label label = account.labels().create("My Label");
}else if (accountDetail.getOrganizationUnit().equals("folder")){
Folder folder = account.folders().create("My Folder");
}
Organize Email Inbox with Labels and Folders
The last thing we'll do is get the most recent email in the user's inbox and apply the label or move it into the folder we created.
if (accountDetail.getOrganizationUnit().equals("label")) {
Message firstMessage = account.messages().list(
new MessageQuery().in("Inbox").limit(1)).get(0);
account.threads().addLabel(
firstMessage.getId(),
label.getId());
}else if (accountDetail.getOrganizationUnit().equals("folder")){
Thread firstThread = account.threads().list(
new ThreadQuery().in("Inbox").limit(1)).get(0);
account.messages().setFolderId(
firstThread.getId(),
folder.getId());
}
This example uses the Message
class for labels and Thread
class for folders, but both classes offer .addLabel()
and .setFolderId()
functions. Take a look at the API reference for the Messages and Threads endpoint to learn more about these objects.
Here is the entire code example to read an email account’s labels or folders and organize email inbox content with them.
import java.util.List;
import java.io.IOException;
import com.nylas.NylasAccount;
import com.nylas.NylasClient;
import com.nylas.RequestFailedException;
import com.nylas.Label;
import com.nylas.Folder;
import com.nylas.AccountDetail;
import com.nylas.Message;
import com.nylas.MessageQuery;
import com.nylas.ThreadQuery;
import com.nylas.Thread;
public class OrganizeFoldersLabels {
public static void main(String[] args) throws IOException, RequestFailedException {
// 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");
AccountDetail accountDetail = nylas.account(accessToken).fetchAccountByAccessToken();
// The organizationUnit attribute for the account object lets you know if the account uses folders or labels for organization
// Gmail uses labels, all other providers use folders
// Account object reference:/docs/api/v2/#tag--Accounts
if (accountDetail.getOrganizationUnit().equals("label")) { // Gmail uses labels
// Labels reference: /docs/api/v2/#tag--Labels
List<Label> labels = account.labels().list();
// Gmail includes a number of default labels,
// including Inbox, Important, Trash, Spam, and more
labels.stream().forEach((Label label) -> {
System.out.println(label.getDisplayName());
});
// Create a new label by passing it's display name
Label label = account.labels().create("My Label");
// Assign this label to the most recent message in the account's inbox
Message firstMessage = account.messages().list(
new MessageQuery().in("Inbox").limit(1)).get(0);
account.threads().addLabel(
firstMessage.getId(),
label.getId());
// Messages can have multiple labels
// You can also pass a list of id strings to .updateLabels() to batch update.
// .updateLabels() will overwrite all labels for the message
// Remove the label with account.messages().removeLabel(messageId, labelId)
}else if (accountDetail.getOrganizationUnit().equals("folder")){ // All other providers use folders
// Folders reference: /docs/api/v2/#tag--Folders
List<Folder> folders = account.folders().list();
// Microsoft accounts include a number of default folders,
// including Inbox, Important, Trash, Spam, and more
folders.stream().forEach((Folder folder) -> {
System.out.println(folder.getDisplayName());
});
//// Create a new folder by passing it's display name
Folder folder = account.folders().create("My Folder");
// Now we'll move the most recent email thread to our new folder
Thread firstThread = account.threads().list(
new ThreadQuery().in("Inbox").limit(1)).get(0);
account.messages().setFolderId(
firstThread.getId(),
folder.getId());
// Note: an email message can only be contained in a single folder, but
// a thread with multiple messages can span multiple folders
}
}
}
Explore the Nylas Email API
If you’ve made it to this point, congratulations! You now know how to organize an email inbox 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 The Basics to learn about what it takes to integrate the Nylas Email API into your app.