Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created March 21, 2021 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aspose-com-gists/b89a7274c26beb2dfba09662ea3244d0 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/b89a7274c26beb2dfba09662ea3244d0 to your computer and use it in GitHub Desktop.
Read emails from Exchange Server using 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 ImapClient class
ImapClient imapClient = new ImapClient("imap.gmail.com", 993, "username", "password");
// 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());
}
}
// 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());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment