Java Library to Read Email Box O365
Read an Inbox with Java
Learn how to read emails from Gmail, Outlook, and Commutation with the Nylas Java SDK. Build your electronic mail integration in xv minutes.
Coffee has been i of the nigh popular programming languages for many years now considering of its ability to run on nearly any platform, gigantic choice of libraries, and high level of reliability. It's no wonder that it'due south an extremely pop language for enterprise applications, and its employ in Android development has further cemented it every bit a major player in the software evolution industry.
The Nylas Email API connects to all major providers, including Gmail, Outlook, Office365, Substitution, Yahoo, and more, and our Coffee SDK makes information technology simple to read information from an e-mail inbox.
This tutorial explains how to use the Nylas Java SDK and Email API to read and search through inbox emails. Information technology covers the following functionality
- Set your Nylas developer business relationship and get your API keys
- Install the Nylas Coffee SDK
- Read Email Letters and Threads
- Search an Email Inbox
- Explore the Nylas E-mail API
Prerequisites
Earlier you can start using the Nylas Coffee SDK, make sure you accept washed the following:
- Sign up for your developer business relationship.
- Become your developer keys. Y'all need to take your:
-
CLIENT_ID
- The Client ID institute on the dashboard page for your Nylas App. -
CLIENT_SECRET
- The Customer SECRET found on the dashboard page for your Nylas App. -
ACCESS_TOKEN
- The admission token provided when you authenticate an account to your Nylas App.
-
Install the Nylas Java SDK
The Nylas Java SDK requires Java 8 or above.
For the following examples, replace Ten.10.X
with the version yous desire to employ. Take a wait at the listing of releases to learn about the versions that are available.
Setup with Gradle
If yous're using Gradle, add the following to your dependencies section of build.gradle
:
implementation ( "com.nylas.sdk:nylas-java-sdk:X.X.X" )
Setup with 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 Customer
At its core, the Nylas Communication Platform is an API customer that interfaces with all of the major electronic mail providers. First, import the NylasClient
grade and create a new instance of this class. Then, import the Account
grade and create an instance of it, passing the admission token you gathered when you got your programmer 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. Business relationship ;
public class ApiClient {
public static void principal ( String [ ] args) throws IOException , RequestFailedException {
NylasClient nylas = new NylasClient ( ) ;
Business relationship business relationship = nylas. account ( "ACCESS_TOKEN" ) ;
}
}
Be Careful with Secrets!
Follow best practices to include secrets like this in your code. A more secure way to provide these values is to apply a KeyStore to protect sensitive information.
Read Data From an Email Inbox
Messages Versus Threads
Before reading content from your email inbox, information technology'due south important to understand the difference betwixt messages and threads.
Messages are the fundamental object of the Nylas platform, and the core edifice cake for well-nigh electronic mail applications. They contain several pieces of information, such as when a bulletin was sent, the sender'southward address, to whom information technology was sent, and the message body. They tin also contain files (attachments), calendar outcome invitations, and more.
Threads are commencement-class objects that represent a collection of messages. Messages are threaded together with a diverseness of heuristics to friction match the format of third-political party accounts as closely every bit possible.
At present, let's take a look at how to read messages and threads from an email inbox.
Reading Letters and Threads
Get-go, nosotros'll create a new Message
object using letters.list()
, passing a MessageQuery
object that returns a list containing merely the nearly recent email bulletin.
Bulletin message = account.messages. listing ( new MessageQuery ( ) . limit ( 1 ) ) . get ( 0 ) ;
At present, we'll impress out the message subject field, unread status, and ID.
System .out. printf ( "Subject: %s | Unread: %s | ID: %s\n" ,
message. getSubject ( ) ,
message. getUnread ( ) ,
message. getId ( )
) ;
Take a look at the API reference for the Letters endpoint to acquire more about that attributes that are available.
Next, we'll employ business relationship.threads()
a ThreadQuery
object to return a listing of the 5 most recent unread threads in the user's inbox.
ThreadQuery unread = new ThreadQuery ( ) . limit ( 5 ) . unread ( true ) ;
List < Thread > threads = account. threads ( ) . list (unread) ;
Finally, print the subject area of the unread threads and a list of the participant's emails addresses. The NameEmail
form is used for the latter.
for ( Thread thread : threads) {
StringBuilder participants = new StringBuilder ( ) ;
for ( NameEmail participant : thread. getParticipants ( ) ) {
participants. append (participant. getEmail ( ) ) . append ( " " ) ;
}
Arrangement .out. printf ( "Discipline: %due south | Participants: %s\n" ,
thread. getSubject ( ) ,
participants
) ;
}
For more than information about what you can do with the Threads endpoint, delight take a look at the API reference.
Searching Messages and Threads
The search sub-endpoint is used to run a total-text search, that is proxied to the account's provider. Results are matched with objects that have been synced, and and so returned. Search functionality is available for messages and threads, and the following example demonstrates how to search for messages that are from a specific electronic mail address.
// Search for the most recent electronic mail from a specific address
Message message = account. messages ( ) . search ( "from:swag@nylas.com" ) . become ( 0 ) ;
Organization .out. println (message. getSubject ( ) ) ;
Hither is the entire lawmaking example for searching for and reading letters and threads from an email inbox.
import java.util. Listing ;
import com.nylas. * ;
import com.nylas. Thread ;
public grade ReadMessagesThreads {
public static void main ( Cord [ ] args) throws Exception {
// Create client object and connect it to Nylas using
// an account'due south access token
NylasClient customer = new NylasClient ( ) ;
// Provide the access token for a specific account
NylasAccount business relationship = client. business relationship ( "ACCESS_TOKEN" ) ;
// Return the well-nigh contempo email message
RemoteCollection < Message > messagesList = account. messages ( ) . list ( new MessageQuery ( ) . limit ( 1 ) ) ;
for ( Bulletin message : messagesList) {
System .out. printf ( "Subject: %s | Unread: %s | ID: %s\n" ,
message. getSubject ( ) ,
bulletin. getUnread ( ) ,
bulletin. getId ( )
) ;
}
// Return the 5 most recent unread threads.
ThreadQuery unread = new ThreadQuery ( ) . limit ( 5 ) . unread ( true ) ;
RemoteCollection < Thread > threads = business relationship. threads ( ) . list (unread) ;
for ( Thread thread : threads) {
StringBuilder participants = new StringBuilder ( ) ;
for ( NameEmail participant : thread. getParticipants ( ) ) {
participants. append (participant. getEmail ( ) ) . append ( " " ) ;
}
System .out. printf ( "Subject: %due south | Participants: %south\northward" ,
thread. getSubject ( ) ,
participants
) ;
}
// Search for the nigh recent email from a specific accost
Message bulletin = business relationship. messages ( ) . search ( "from:swag@nylas.com" ) . get ( 0 ) ;
System .out. println (message. getSubject ( ) ) ;
}
}
Source: https://developer.nylas.com/docs/the-basics/tutorials/java/read-an-inbox-with-java/
0 Response to "Java Library to Read Email Box O365"
Enviar um comentário