Nylas Docs

The Nylas Developer Hub

Welcome to the Nylas developer hub. You'll find comprehensive guides and documentation to help you start working with Nylas as quickly as possible, as well as support if you get stuck. Let's jump right in!

Developer Guide

Send an Email with Java

Learn how to send emails from Gmail, Outlook, and Exchange 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 send and reply to emails.

This tutorial explains how to use the Nylas Java SDK and Email API to send emails. It covers the following functionality

  1. Set up your Nylas developer account and get your API keys
  2. Install the Nylas Java SDK
  3. Send an Email
  4. Reply to an Email
  5. Attach Files to Email Drafts
  6. 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 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

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.

Create and Send Email Drafts

Now, it’s time to create a draft.

Draft draft = new Draft();
draft.setSubject("With Love, from Nylas");
draft.setBody("This email was sent using the Nylas Email API. Visit https://nylas.com for details.");

This example creates a new draft object and assigns it a subject and body text. You can also add file attachments, message tracking features, and reply-to values. Take a look at the Nylas API reference to learn more about what you can add to an email draft with the Nylas Email API.

Next, let’s add a recipient to this email.

draft.setTo(Arrays.asList(new NameEmail("My Nylas Friend", "[email protected]")));

The setTo() function accepts a NamerEmail() class that contains strings representing a name and email address. You can also use setCc() and setBcc() in the same manner.

Finally, it’s time to send the email!

account.drafts().send(draft);

Take a look at the API reference for the Send endpoint to learn more about sending emails with Nylas.

Ready to Build Your Email Integration?

Here is the entire example to draft and send an email.

import java.util.Arrays;
import java.io.IOException;
import com.nylas.NylasAccount;
import com.nylas.NylasClient;
import com.nylas.RequestFailedException;
import com.nylas.Draft;
import com.nylas.NameEmail;

public class SendEmail {
    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");

       Draft draft = new Draft();
       draft.setSubject("With Love, from Nylas");
       draft.setBody("This email was sent using the Nylas Email API. Visit https://nylas.com for details.");
       draft.setTo(Arrays.asList(new NameEmail("My Nylas Friend", "[email protected]")));

       account.drafts().send(draft);
    }
}

Reply to an Email

The first step to reply to an email is to find the thread you want to reply to. For our example, we’ll get the most recent email in the user’s inbox using account.threads() and a ThreadQuery class to return only the first thread.

// Get the most recent email thread
Thread thread = account.threads().expanded(new ThreadQuery().limit(1)).get(0);

Now, we will use the .createReply() function to create a new Draft that has the same thread_id and subject as the thread we are replying too.

// Send a reply to a thread
Draft reply = thread.createReply();

Finally, use setTo() and setCc() to assign the appropriate recipients and send the email.

reply.setTo(message.getFrom());
reply.setCc(message.getCc());
reply.setBody("This is my reply.");
account.drafts().send(reply);
Ready to Build Your Email Integration?

Here is the entire code example to reply to an email with the Nylas Java SDK.

import java.util.List;
import java.io.IOException;
import com.nylas.NylasAccount;
import com.nylas.NylasClient;
import com.nylas.RequestFailedException;
import com.nylas.Thread;
import com.nylas.ThreadQuery;
import com.nylas.Message;
import com.nylas.Draft;

public class ReplyEmail {
  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");
    
    // Get the most recent email thread
    Thread thread = account.threads().expanded(new ThreadQuery().limit(1)).get(0);
    
    // Send a reply to a thread
    Draft reply = thread.createReply();
    reply.setTo(message.getFrom());
    reply.setCc(message.getCc());
    reply.setBody("This is my reply.");
    account.drafts().send(reply);
  }        
}

Attach a File to an Email

The Files endpoint allows you to create and modify files that can be attached to emails. This example demonstrates how to take a file that’s saved locally and upload it to Nylas for use with our Email API.

byte[] myFile = java.nio.file.Files.readAllBytes(Paths.get("/path/to/myFile.pdf"));
// .files.upload() saves the file to Nylas, file.id can then be used to attach the file to an email
File upload = files.upload("My Upload.pdf", "application/pdf", myFile);

Now, we’ll create an email draft to attach this file to.

//Create a new email draft to attach the file to.
Draft draft = new Draft();
draft.setSubject("With Love, From Nylas");
draft.setBody("This email was sent using the Nylas Email API. Visit https://nylas.com for details.");
draft.setTo(Arrays.asList(new NameEmail("My Nylas Friend", "[email protected]")));

You can also use account.drafts().get('{id}') to choose an existing draft for this example, replacing “{id}” with the appropriate value. Take a look at the API reference for Drafts to learn more about the draft object.

Finally, we’ll attach the file we uploaded earlier to the draft using .attach(), and send the email draft.

File attachment = account.files().get(upload.getId());
draft.attach(attachment);
account.drafts().send(draft);
Ready to Build Your Email Integration?

Here is the entire example that demonstrates how to attach a file to an email .

import java.util.Arrays;
import java.nio.file.Paths;
import java.io.IOException;
import com.nylas.NylasAccount;
import com.nylas.NylasClient;
import com.nylas.RequestFailedException;
import com.nylas.File;
import com.nylas.Files;
import com.nylas.NameEmail;
import com.nylas.Draft;

public class AttachFile {

    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");
        Files files = account.files();
        
        
        byte[] myFile = java.nio.file.Files.readAllBytes(Paths.get("/path/to/myFile.pdf"));
        // .files.upload() saves the file to Nylas, file.id can then be used to attach the file to an email
        File upload = files.upload("My Upload.pdf", "application/pdf", myFile);
        
        //Create a new email draft to attach the file to.
        Draft draft = new Draft();
        draft.setSubject("With Love, From Nylas");
        draft.setBody("This email was sent using the Nylas Email API. Visit https://nylas.com for details.");
        draft.setTo(Arrays.asList(new NameEmail("My Nylas Friend", "[email protected]")));
        
        File attachment = account.files().get(upload.getId());
        draft.attach(attachment);
        account.drafts().send(draft);

    }        
}

Explore the Nylas Email API

If you’ve made it to this point, congratulations! You’ve sent your very first email 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.

Updated 3 months ago

Send an Email with Java


Learn how to send emails from Gmail, Outlook, and Exchange with the Nylas Java SDK. Build your email integration in 15 minutes.

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.