How to use GraphClient for Microsoft Graph
Contents
[
Hide
]
Working with GraphClient
An instance of the IGraphClient class handles building requests, sending them to the Microsoft Graph API, and processing the responses.
Create an ITokenProvider Object
To create a new instance of IGraphClient class, you need to provide an instance of ITokenProvider, which can authenticate requests to Microsoft Graph.
ITokenProvider tokenProvider = new ITokenProvider() {
Date expirationDate = null;
@Override
public void dispose() {
}
@Override
public OAuthToken getAccessToken(boolean ignoreExistingToken) {
// Gets oAuth access token.
// If ignoreExistingToken is true, requests new token from a server. Otherwise behavior is depended on whether token exists or not.
// If token exists and its expiration date is not expired returns current token, otherwise requests new token from a server.
return null;
}
@Override
public OAuthToken getAccessToken() {
// Gets oAuth access token.
// If token exists and its expiration date is not expired returns current token, otherwise requests new token from a server.
return new OAuthToken("token", expirationDate);
}
};
Get a GraphClient object
After you have set the TokenProvider, you must get a IGraphClient object to make requests against the service. After you have a IGraphClient that is authenticated, you can begin making calls against the service.
IGraphClient client = GraphClient.getClient(tokenProvider);
Folder Api
List Folders
GraphFolderInfoCollection folders = client.listFolders();
for (GraphFolderInfo folderInfo : folders) {
System.out.println(folderInfo.getDisplayName());
for (KeyValuePair<Long, MapiProperty> prop : folderInfo.getProperties()) {
System.out.println(prop.getValue().getDescriptor().toString() + " " + prop.getValue().getString());
}
}
List Subfolders from Inbox Folder
GraphFolderInfoCollection inboxFolders = client.listFolders(GraphKnownFolders.Inbox);
Create Root Folder
GraphFolderInfo newFolder = client.createFolder("TEST_FOLDER");
Create Subfolder
GraphFolderInfo inboxTestSubFolder1 = client.createFolder(GraphKnownFolders.Inbox, "TEST_SUBFOLDER_1");
GraphFolderInfo inboxTestSubFolder2 = client.createFolder(newFolder.getItemId(), "TEST_SUBFOLDER_2");
Get Folder
GraphFolderInfo sentItemsFolder = client.getFolder(GraphKnownFolders.SentItems);
Update Folder
GraphFolderInfo originalFolder = client.createFolder("TEST_FOLDER");
originalFolder.setDisplayName("NEW_TEST_FOLDER");
GraphFolderInfo updatedFolder = client.updateFolder(originalFolder);
Copy Folder and Content
GraphFolderInfo parentFolder = client.createFolder("PARENT_FOLDER");
GraphFolderInfo testFolder = client.createFolder("TEST_FOLDER");
GraphFolderInfo testSubFolder = client.createFolder(testFolder.getItemId(), "TEST_SUBFOLDER");
MapiMessage message = new MapiMessage();
message.setSubject("Test subject");
message.setBody("Test body");
message.setProperty(KnownPropertyList.DISPLAY_TO, "to@host.com");
message.setProperty(KnownPropertyList.SENDER_NAME, "from");
message.setProperty(KnownPropertyList.SENT_REPRESENTING_EMAIL_ADDRESS, "from@host.com");
MapiMessage createdMessage = client.createMessage(testSubFolder.getItemId(), message);
GraphFolderInfo folderCopy = client.copyFolder(parentFolder.getItemId(), testFolder.getItemId());
GraphFolderInfoCollection folderColl = client.listFolders(parentFolder.getItemId());
// TEST_FOLDER
System.out.println(folderColl.get(0).getDisplayName());
folderColl = client.listFolders(folderColl.get(0).getItemId());
// TEST_SUBFOLDER
System.out.println(folderColl.get(0).getDisplayName());
GraphMessageInfoCollection listMessages = client.listMessages(folderColl.get(0).getItemId());
// Test subject
System.out.println(listMessages.get(0).getSubject());
Move Folder and Content
GraphFolderInfo folder = client.moveFolder(parentFolder.getItemId(), testFolder.getItemId());
Delete Folder
client.delete(testFolder.getItemId());
Messages Api
List Messages
GraphMessageInfoCollection messageInfoColl = client.listMessages(GraphKnownFolders.Inbox);
for (GraphMessageInfo messageInfo : messageInfoColl) {
MapiMessage message = client.fetchMessage(messageInfo.getItemId());
}
Fetch Message
GraphMessageInfo messageInfo = messageInfoColl.get(0);
MapiMessage fetchedMessage = client.fetchMessage(messageInfo.getItemId());
Create Message
MapiMessage message = new MapiMessage();
message.setSubject("Subject");
message.setBody("Body");
message.setProperty(KnownPropertyList.DISPLAY_TO, "to@host.com");
message.setProperty(KnownPropertyList.SENDER_NAME, "from");
message.setProperty(KnownPropertyList.SENT_REPRESENTING_EMAIL_ADDRESS, "from@host.com");
MapiMessage createdMessage = client.createMessage(GraphKnownFolders.Inbox, message);
Update Message
fetchedMessage.setSubject("Update message");
MapiMessage updatedMessage = client.updateMessage(fetchedMessage);
Send Message
client.send(message);
Send Draft Message
MapiMessage draftMessage = client.createMessage(GraphKnownFolders.Drafts, message);
client.send(draftMessage.getItemId());
Copy Message
MapiMessage copiedMessage = client.copyMessage(GraphKnownFolders.Inbox, draftMessage.getItemId());
Move Message
MapiMessage movedMessage = client.moveMessage(GraphKnownFolders.Inbox, draftMessage.getItemId());
Message Attachments
MapiAttachmentCollection attachments = client.listAttachments(fetchedMessage.getItemId());
for (MapiAttachment att : attachments) {
client.deleteAttachment(att.getItemId());
}
Delete Message
client.delete(message.getItemId());
Categories Api
String categoryName = "Test Category";
int preset = CategoryPreset.Preset10;
OutlookCategory category = client.createCategory(categoryName, preset);
OutlookCategory fetchedCategory = client.fetchCategory(category.getId());
List<OutlookCategory> categories = client.listCategories();
fetchedCategory.setDisplayName("Update Category");
fetchedCategory.setPreset(CategoryPreset.Preset11);
OutlookCategory updatedCategory = client.updateCategory(fetchedCategory);
client.delete(category.getId());
Overrides Api
int focusedType = ClassificationType.Focused;
ClassificationOverride override = client.createOrUpdateOverride(new MailAddress("testUser@host.com", "testUser"), focusedType);
List<ClassificationOverride> list = client.listOverrides();
for (ClassificationOverride item : list)
System.out.println(item.getSender().getAddress());
ClassificationOverride fetchedOverride = list.get(0);
fetchedOverride.setClassifyAs(ClassificationType.Other);
fetchedOverride.getSender().setDisplayName("testUser_1");
ClassificationOverride updatedOverride = client.createOrUpdateOverride(fetchedOverride);
fetchedOverride.setClassifyAs(ClassificationType.Focused);
updatedOverride = client.updateOverride(fetchedOverride);
client.delete(updatedOverride.getId());
Rules Api
InboxRule rule = new InboxRule();
rule.setDisplayName("Test rule");
rule.setPriority(1);
rule.setEnabled(true);
rule.setConditions(new RulePredicates());
rule.getConditions().containsSenderStrings(new StringCollection());
rule.getConditions().containsSenderStrings().addItem("testUser");
rule.setActions(new RuleActions());
rule.getActions().setForwardToRecipients(new MailAddressCollection());
rule.getActions().getForwardToRecipients().addMailAddress(new MailAddress("testUser@host.com", "testUser", true));
rule.getActions().setStopProcessingRules(true);
InboxRule createdRule = client.createRule(rule);
List<InboxRule> listedRules = client.listRules();
for (InboxRule item : listedRules)
System.out.println(item.getDisplayName());
InboxRule fetchedRule = client.fetchRule(createdRule.getRuleId());
createdRule.setDisplayName("Test rule 1");
createdRule.setEnabled(false);
InboxRule updatedRule = client.updateRule(createdRule);
client.delete(createdRule.getRuleId());
Using Resource to Support Multiple Mailboxes
сlient.setResource(ResourceType.Users);
сlient.setResourceId("mailbox");
сlient.listMessages("mailfolder")
// back to the current mailbox
сlient.setResource(ResourceType.Me);