Introducing the Nylas Java SDK [Beta]

Ben Lloyd Pearson | February 27, 2020

Nylas is excited to announce the beta release of the Nylas Java SDK. This SDK is the quickest way to integrate the Nylas Email, Calendar, and Contacts APIs into your application with Java.

Why Java?

Sun Microsystems released Java back in 1995 and it was sold as a platform that enables developers to “write once, run anywhere.” This notion comes from the fact that once a Java application is compiled into bytecode, it can be run on any platform that supports the Java Virtual Machine, regardless of the underlying hardware. Throughout the 90’s and into the early 2000’s, Java experienced a fairly high level of popularity because it was very good for building client/server web applications during a time that internet access was becoming more commonplace. 

In the late 2000’s, its popularity exploded as Android hit the stage and brought Java to the mobile operating system market; Android quickly became the dominant operating system in this space. According to StackOverflow, Java is still an extremely popular programming language even to this day, and it doesn’t seem like it’s going away anytime soon. So, Nylas has decided to release a new Java SDK to enable developers who still prefer to work with this long standing programming language to build the apps they love.

Get Started with the Nylas Java SDK

The best way to get started with the Nylas Java SDK is to take a look at our Java Quickstart Guide where we walk you through the process of setting up your Nylas app and adding the Nylas Java SDK to your app.  At the very least, you need to follow our guide to get your developer API keys where you’ll generate an access token that can be used to test out the Nylas Java SDK.

Now, let’s take a look at a couple of things we can do with the Nylas Java SDK.

Send an Email

One of the more popular features of the Nylas Email API is the ability to send emails using the end user account.  The next example demonstrates how to use the access token for an email account you control to draft and send a simple email. Try it yourself! Just replace the value assigned to accessToken.

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 {
        // Provide the access token for a specific account
        String accessToken = "XXX";

        // Create client object and connect it to Nylas using
        // an account's access token
        NylasClient client = new NylasClient();
        NylasAccount account = client.account(accessToken);

       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);
    }
}

For more information, take a look at the Nylas Email API Quickstart Guide.

Manage Calendar Events

Now, let’s take a look at what it’s like to use the Java SDK to access functionality from the Nylas Calendar API. The following example lists all of the calendars a connected user account has and then shows details about the first 5 events from one of them. Replace the string assigned to accessToken to run this example yourself.

import java.util.List;
import java.io.IOException;
import com.nylas.NylasAccount;
import com.nylas.NylasClient;
import com.nylas.RequestFailedException;
import com.nylas.Calendar;
import com.nylas.CalendarQuery;
import com.nylas.Event;
import com.nylas.EventQuery;

public class ReadCalendarsEvents {

    public static void main(String[] args) throws IOException, RequestFailedException {
        // Provide the access token for a specific account
        String accessToken = "XXX";
      
        // Create client object and connect it to Nylas using
        // an account's access token
        NylasClient nylas = new NylasClient();
        NylasAccount account = nylas.account(accessToken);
        
        // Most user account have multiple calendars
        List<Calendar> calendars = account.calendars().list();
        calendars.stream().forEach((Calendar calendar) -> {
            // Print the name and description for each calendar
            // and whether or not the calendar is read only.
            System.out.printf(
                    "Name: %s | Description %s | Read Only: %s\n", 
                    calendar.getName(),
                    calendar.getDescription(),
                    calendar.isReadOnly());
        });
      
        Calendar calendar = account.calendars().list(
                new CalendarQuery().limit(1)).get(0);
        
        // List the 5 first events from a specified calendar.
        List<Event> events = account.events().list(
                new EventQuery().calendarId(calendar.getId()).limit(5));
        events.stream().forEach((Event event) -> {
        System.out.printf(
                    "Title: %s | When %s | Participants: %s\n", 
                    event.getTitle(),
                    event.getWhen(),
                    event.getParticipants());
    });
    }        
}

For more information, take a look at the Nylas Calendar API Quickstart Guide.

Ready to Start Building With Nylas?

Whether you want to build the next great Android app, or you want to add new functionality to your Java server application, Nylas is the easiest way to integrate full email, calendar, and contacts functionality from 100% of providers in the world. You can build your first integration in as little as 15 minutes, get started today!

About the Author

Ben is the Developer Advocate for Nylas. He is a triathlete, musician, avid gamer, and loves to seek out the best breakfast tacos in Austin, Texas.

Ready to Start Building?