Read the complete article on how to move emails to a specific folder on Microsoft Exchange Server in Java: https://blog.aspose.com/2022/03/10/move-email-to-a-folder-on-ms-exchange-server-in-java/
Last active
March 30, 2022 07:55
-
-
Save aspose-com-gists/ee4dc729258c1f97c40226b8481f7644 to your computer and use it in GitHub Desktop.
Move Email to a Folder on Microsoft Exchange Server in 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
try { | |
// Create instance of EWSClient 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) { | |
if (msgInfo.getFrom().getAddress().contains("jhon.vick")) { | |
String strMessageURI = msgInfo.getUniqueUri(); | |
// Copy to particular folder | |
String newMessageUri = client.copyItem(strMessageURI, client.getMailboxInfo().getOutboxUri()); | |
} | |
} | |
} catch (java.lang.RuntimeException ex) { | |
System.out.println(ex.getMessage()); | |
} |
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
try { | |
// Create instance of EWSClient class by giving credentials | |
IEWSClient client = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", | |
"pwd", "domain"); | |
// Create a new message | |
MailMessage message = new MailMessage("from@domain.com", "to@domain.com", | |
"EMAILNET-34997 - " + UUID.randomUUID().toString(), | |
"EMAILNET-34997 Exchange: Copy a message and get reference to the new Copy item"); | |
// Get URI | |
String messageUri = client.appendMessage(message); | |
// Move to folder | |
String newMessageUri = client.copyItem(messageUri, client.getMailboxInfo().getDeletedItemsUri()); | |
} catch (java.lang.RuntimeException ex) { | |
System.out.println(ex.getMessage()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment