Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active March 30, 2022 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/ee4dc729258c1f97c40226b8481f7644 to your computer and use it in GitHub Desktop.
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
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());
}
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