Only show these results:

Manage folders and labels with Kotlin/Java

This page explains how to use the Nylas Kotlin/Java SDK and Email API to manage the folders and labels in an email inbox. For more information, see the Email documentation.

Before you begin

Before you start, you must have done the following tasks:

List folders and labels

Depending on the end user's email provider, there are two possible ways their inbox might be organized: using either folders or labels. Gmail uses labels, and all other providers use folders. Nylas consolidates both folders and labels under the Folders endpoint.

The following examples list all folders and labels from an end user's inbox.

import com.nylas.NylasClient;
import com.nylas.models.*;

public class SendDraft {
public static void main(String[] args) throws NylasSdkTimeoutError, NylasApiError {
NylasClient nylas = new NylasClient.Builder("<NYLAS_API_KEY>").build();
ListResponse<Folder> folders = nylas.folders().list("<GRANT_ID>");

for(Folder folder : folders.getData()){
System.out.println(folder.getName());
}
}
}
import com.nylas.NylasClient

fun main(args: Array<String>) {
val nylas: NylasClient = NylasClient(apiKey = "<NYLAS_API_KEY>")
val labels = nylas.folders().list("<GRANT_ID>")

for (label in labels.data){
println(label.name)
}
}

Create folders and labels

The examples below create either a folder or label. Nylas automatically determines which to create based on the end user's provider.

import com.nylas.NylasClient;
import com.nylas.models.*;

public class CreateLabels {
public static void main(String[] args) throws NylasSdkTimeoutError, NylasApiError {
NylasClient nylas = new NylasClient.Builder("<NYLAS_API_KEY>").build();
CreateFolderRequest request = new CreateFolderRequest("My Custom folder", "", "", "");
Response<Folder> label = nylas.folders().create("<GRANT_ID>", request);

System.out.println(label);
}
}
import com.nylas.NylasClient
import com.nylas.models.CreateFolderRequest

fun main(args: Array<String>) {
val nylas: NylasClient = NylasClient(apiKey = "<NYLAS_API_KEY>")
val request = CreateFolderRequest("My Custom folder")
val labels = nylas.folders().create("<GRANT_ID>", request).data

for(label in labels) {
println(label.name)
}
}

Organize an inbox with folders and labels

The following examples use the Kotlin/Java SDK to get the most recent email message in an end user's inbox and either move it to your new folder or apply the new label (see Create folders and labels).

ListMessagesQueryParams queryParams = new ListMessagesQueryParams.Builder().
inFolder(Collections.singletonList("inbox")).
limit(1).
build();

UpdateMessageRequest messageRequest = new UpdateMessageRequest.Builder().
folders(Collections.singletonList(label.getData().getId())).build();

ListResponse<Message> message = nylas.messages().list(dotenv.get("GRANT_ID"), queryParams);
assert message.getData().get(0).getId() != null;

nylas.messages().update(dotenv.get("GRANT_ID"), message.getData().get(0).getId(), messageRequest);
val queryParams = ListMessagesQueryParams.Builder().inFolder(listOf("inbox")).limit(1).build()
val message = nylas.messages().list(dotenv["GRANT_ID"], queryParams)
val messageRequest = UpdateMessageRequest.Builder().folders(listOf(labels.id)).build()
val messageId = message.data[0].id

if (messageId != null) {
nylas.messages().update(dotenv["GRANT_ID"], messageId, messageRequest)
}

Explore the Email API

If you've made it to this point, congratulations! You've learned how to organize an email inbox with the Nylas Kotlin/Java SDK and Email API! 🎉

There's plenty more that you can do with Nylas. Take a look at the following resources to learn more.