Created
March 21, 2021 16:19
-
-
Save aspose-com-gists/b89a7274c26beb2dfba09662ea3244d0 to your computer and use it in GitHub Desktop.
Read emails from Exchange Server using Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create an instance of IEWSClient class to connect to Exchange Server | |
IEWSClient client = EWSClient.getEWSClient("https://exchange.domain.com/ews/Exchange.asmx/", "user", "password", ""); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Connect to Exchange Server using ImapClient class | |
ImapClient imapClient = new ImapClient("imap.gmail.com", 993, "username", "password"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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