Skip to content

Send messages with Kotlin/Java

This page explains how to use the Nylas Kotlin/Java SDK and Email API to send messages, reply to a message, and attach files to a draft. For more information, see the Email documentation.

Before you begin

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

Create and send an email draft

This section walks through how to create and send an email draft with the Nylas Kotlin/Java SDK.

The following example creates a Draft object and assigns it a subject and some body text.

You can also add file attachments, message tracking features, and reply-to values. For more information about the data you can add to a draft, see the Drafts references.

Next, add a recipient to the draft.

You can also set the CC and BCC contacts using a similar construction.

Finally, it’s time to send the draft!

Below are the full examples showing how to draft and send a message.

Reply to a message

The first step to reply to a message is to find the thread you want to reply to. The examples below get the most recent message in a user’s inbox by returning only the first thread.

Next, create a draft that has the same thread_id and subject as the thread you’re replying to.

Finally, assign the appropriate recipients and send the message.

Below are the full examples showing how to reply to a message.

Attach a file to a message

The Attachments endpoint allows you to create and modify files that you can attach to messages. The following examples show how to take a file that’s saved locally and upload it to Nylas for use with the Email API.

Next, create a draft to attach the file to.

Finally, attach the file that you uploaded to the draft, and send the draft as a message.

Below are the full examples showing how to attach a file to a message.

Track messages

The Nylas Email API allows you to track messages, and Nylas generates webhook notifications when certain conditions are met. For more information, see the message tracking documentation.

The examples below enable message open, link clicked, and thread replied tracking for a message, then send the message.