Version:
Only show these results:

Read email messages and threads with Kotlin/Java

This page explains how to use the Nylas Kotlin/Java SDK and Email API to read and search your end users' email messages and threads. For more information, see the Email documentation.

Messages versus Threads

Messages are the fundamental object of the Nylas platform and the core building block for most email applications. Messages contain several pieces of information, such as when the 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 collections of messages which are related to each other, and result from people replying to an email conversation. For Gmail and Microsoft Exchange accounts, Nylas threads messages together so that they are as similar as possible to their representation in those environments. For all other providers, including generic IMAP, Nylas threads messages using a custom JWZ-inspired algorithm.

For more information, see the Messages and Threads reference documentation.

Before you begin

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

Read email messages from an inbox

This section walks through how to read email messages from an end user's inbox.

First, create a Message object and pass a query to it. This returns a list containing only the most recent email message in the end user's inbox.

ListMessagesQueryParams queryParams = new ListMessagesQueryParams.Builder().limit(1).build();
ListResponse<Message> message = nylas.messages().list("<NYLAS_GRANT_ID>", queryParams);
val queryParams : ListMessagesQueryParams = ListMessagesQueryParams.Builder().limit(1).build()
val messages : List<Message> = nylas.messages().list("<NYLAS_GRANT_ID>", queryParams).data

Next, print the message subject, unread status, and ID.

System.out.printf("Subject: %s | Unread: %s | Id: %s",
message.getData().get(0).getSubject(),
message.getData().get(0).getUnread(),
message.getData().get(0).getId());
println("Subject: ${messages[0].subject} | " +
"Unread: ${messages[0].unread} | " +
"Id: ${messages[0].id}")

Read threads from an inbox

This section shows how to read threads from an end user's inbox.

First, create a Threads object and pass a query to it. This returns a list containing only the five most recent unread threads in an end user's inbox.

ListThreadsQueryParams queryParams = new ListThreadsQueryParams.Builder().
limit(5).
unread(true).
build();

ListResponse<Thread> thread = nylas.threads().list("<NYLAS_GRANT_ID>", queryParams);
val queryParams = ListThreadsQueryParams(limit = 5, unread = true)
val threads : List<Thread> = nylas.threads().list("<NYLAS_GRANT_ID>", queryParams).data

Next, print the threads' subjects and participant email addresses.

for(Thread message : thread.getData()){
List<EmailName> participants = message.getParticipants();
assert participants != null;

for(EmailName participant : participants) {
System.out.println("Subject: " + message.getSubject() +
" | Participant: " + participant.getName());
}
}
for(message in threads) {
val participants = message.participants

if (participants != null) {
for(participant : EmailName in participants) {
println("Subject: " + message.subject +
" | Participant: " + participant.name)
}
}
}

Search an end user's inbox

When Nylas runs a search on an end user's inbox, that search is proxied to the account's provider. Nylas matches results with synced objects, and returns them. You can search for both email messages and threads.

The examples below show how to search for and read email messages and threads from an end user's inbox.

import com.nylas.NylasClient;
import com.nylas.models.*;
import com.nylas.models.Thread;
import java.util.List;

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

ListMessagesQueryParams messagesQueryParams = new ListMessagesQueryParams.Builder().
limit(1).
build();

ListResponse<Message> messages = nylas.messages().list("<NYLAS_GRANT_ID>", messagesQueryParams);

for(Message message : messages.getData()) {
System.out.printf("Subject: %s | Unread: %s | ID: %s\n",
message.getSubject(),
message.getUnread(),
message.getId());
}

ListThreadsQueryParams queryParams = new ListThreadsQueryParams.Builder().
limit(5).
build();

ListResponse<Thread> thread = nylas.threads().list("<NYLAS_GRANT_ID>", queryParams);

for(Thread message : thread.getData()) {
List<EmailName> participants = message.getParticipants();
assert participants != null;

for(EmailName participant : participants) {
System.out.println("Subject: " + message.getSubject() +
" | Participant: " + participant.getName());
}
}

ListMessagesQueryParams searchQueryParams = new ListMessagesQueryParams.Builder().
searchQueryNative("from:swag@example.com").
build();

ListResponse<Message> message = nylas.messages().list("<NYLAS_GRANT_ID>", searchQueryParams);

System.out.println(message.getData().get(0).getSubject());
}
}
import com.nylas.NylasClient
import com.nylas.models.*
import java.util.*

fun main(args: Array<String>) {
val nylas: NylasClient = NylasClient(apiKey = "<NYLAS_API_KEY>")

val listQueryParams : ListMessagesQueryParams = ListMessagesQueryParams.
Builder().
limit(1).
build()

val messages : ListResponse<Message> = nylas.messages().list("<NYLAS_GRANT_ID>", listQueryParams)

for (message in messages.data) {
System.out.printf(
"Subject: %s | Unread: %s | ID: %s\n",
message.subject,
message.unread,
message.id
)
}

val threadQueryParams : ListThreadsQueryParams = ListThreadsQueryParams.
Builder().
limit(5).
build()

val threads: List<com.nylas.models.Thread> = nylas.threads().list("<NYLAS_GRANT_ID>", threadQueryParams).data

for(message in threads) {
val participants = message.participants

if (participants != null) {
for(participant : EmailName in participants) {
println("Subject: " + message.subject +
" | Participant: " + participant.name)
}
}
}

val queryParams = ListMessagesQueryParams(searchQueryNative = "from:swag@example.com")
val search = nylas.messages().list("<NYLAS_GRANT_ID>", queryParams).data

println(search[0].subject)
}

Explore the Email API

If you've made it to this point, congratulations! You've learned how to search for and read email messages and threads 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: