
Microsoft Exchange Server is a mail server that provides email, calendaring, contact management, and other collaboration services. It can easily be connected and accessed from within your web or desktop applications. Thus, you can communicate with the Exchange Server and access emails, contacts, calendars, etc. In this article, you will learn how to connect to MS Exchange Server and read emails using Java.
- Java API to Work with MS Exchange Server
- Connect to MS Exchange Server in Java
- Read Emails from MS Exchange Server in Java
- Get a Free API License
Java API to Work with MS Exchange Server
Aspose.Email for Java is a powerful API that provides you with a wide range of email management features. It lets you implement feature-rich email clients from within your Java applications. Furthermore, the API allows you to access folders, subfolders, emails, calendars, contacts, etc. from MS Exchange Server. You can either download the API’s JAR or install it using the following Maven configurations.
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>http://repository.aspose.com/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-email</artifactId>
<version>21.2</version>
<classifier>jdk16</classifier>
</dependency>
Connect to MS Exchange Server in Java
In order to work with MS Exchange Server, you need to establish a connection first. Aspose.Email for Java provides the following ways to connect to an Exchange Server.
- Using Exchange Web Service (EWS)
- Using Internet Message Access Protocol (IMAP)
Connect to Exchange Server using EWS in Java
The following are the steps to connect to Exchange Server via EWS.
- Create an instance of IEWSClient class.
- Use EWSClient.getEWSClient(String, String, String, String) method to initialize EWSClient object.
The following code sample shows how to connect to MS Exchange Server using EWS in Java.
// Create an instance of IEWSClient class to connect to Exchange Server | |
IEWSClient client = EWSClient.getEWSClient("https://exchange.domain.com/ews/Exchange.asmx/", "user", "password", ""); |
Connect to Exchange Server using IMAP in Java
Before attempting to connect to Exchange Server using IMAP, please make sure that you have enabled IMAP services. Once done, the following are the steps to connect to an Exchange Server using IMAP.
- Create an instance of ImapClient class and initialize it with address, port, username, and password.
- Access messages, contacts, etc. using the ImapClient object.
The following code sample shows how to connect to Exchange Sever using IMAP.
// Connect to Exchange Server using ImapClient class | |
ImapClient imapClient = new ImapClient("imap.gmail.com", 993, "username", "password"); |
Read Emails from MS Exchange Server in Java
Once you have connected to the Exchange Server, you can read messages from the server. You can do it using EWS or IMAP.
Read Emails from MS Exchange Server using EWS
The following are the steps to read emails from Exchange Server using EWS.
- Create an instance of IEWSClient class.
- Use EWSClient.getEWSClient(String, String, String, String) method to initialize EWSClient object.
- Get list of message info from inbox into ExchangeMessageInfoCollection object using EWSClient.listMessages(EWSClient.getMailboxInfo().getInboxUri()) method.
- Loop through each ExchangeMessageInfo object in the collection to read the message.
The following code sample shows how to read messages from Exchange Server via EWS in Java.
// Create instance of ExchangeWebServiceClient class by giving credentials | |
IEWSClient client = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain"); | |
// Call ListMessages method to list messages info from Inbox | |
ExchangeMessageInfoCollection msgCollection = client.listMessages(client.getMailboxInfo().getInboxUri()); | |
// Loop through the collection to get Message URI | |
for (ExchangeMessageInfo msgInfo : msgCollection) { | |
String strMessageURI = msgInfo.getUniqueUri(); | |
// Now get the message details using FetchMessage() | |
MailMessage msg = client.fetchMessage(strMessageURI); | |
// Display message details | |
System.out.println("Subject: " + msg.getSubject()); | |
//Console.WriteLine("HTML Body: " + msg.HtmlBody); | |
// How many attachments | |
System.out.println("Number of attachments: " + msg.getAttachments().size()); | |
// List the attachments | |
for (Attachment att : msg.getAttachments()) { | |
System.out.println("Attachment Name: " + att.getName()); | |
} | |
} |
Read Emails from MS Exchange Server using IMAP
The following are the steps to read emails from Exchange Server using IMAP.
- Create an instance of ImapClient class and initialize it with address, port, username, and password.
- Get the list of message info into ImapMessageInfoCollection object using ImapClient.listMessages() method.
- Loop through each ImapMessageInfo object in the collection to read the emails.
The following code sample shows how to read emails from Exchange Server via IMAP using Java.
// Connect to Exchange Server using ImapClient class | |
ImapClient imapClient = new ImapClient("imap.gmail.com", 993, "username", "password"); | |
// Select the Inbox folder | |
imapClient.selectFolder(ImapFolderInfo.IN_BOX); | |
// Get the list of messages | |
ImapMessageInfoCollection msgCollection = imapClient.listMessages(); | |
for (ImapMessageInfo msgInfo : msgCollection) { | |
System.out.println(msgInfo.getSubject()); | |
} |
Get a Free API License
You can try Aspose.Email for Java without evaluation limitations by getting a temporary license.
Live Demo
Conclusion
In this article, you have learned how to connect to MS Exchange Server and read emails using Java. Furthermore, you have seen how to access MS Exchange Server using EWS or IMAP. You can explore more about Aspose.Email for Java using documentation. In case you would have any questions or queries, feel free to let us know via our forum.