We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to Use IGraphClient and ITokenProvider?


I want to use IGraphClient and TokenProvider in order to get token and access my outlook inbox.
I have these informations: ApplicationID, ObjectID, TenantID, Secret ID and secretValue

which of these information I should use in TokenProvider.getInstance call as the client ID and the client secret parameter?

Is there any java code as an example to see we can manipulate emails using GraphClient?


Thank you for posting the query.

For example, the sample code below shows you how to use the IGraphClient interface for listing mailbox folders.

// put your credential here
var tenantId = "";
var clientId = "";
var userName = "";
var password = "";

var scopes = new String[]

var tokenProvider = new AzureROPCTokenProvider(tenantId, clientId, userName, password, scopes);
var graphClient = GraphClient.getClient(tokenProvider, tenantId);

for (var folderInfo : graphClient.listFolders())

This code example uses an implementation of ITokenProvider interface that can be found here (with some changes).

Microsoft Graph Utility Features
How to use GraphClient for Microsoft Graph

API Reference:
IGraphClient interface
ITokenProvider interface

Thank you, it works fine.
Question: I need to know the size of an email. I used GraphMessageInfo.getSize() but value is always -1.
How can I get the correct size?


Unfortunately, this method has not been implemented for GraphMessageInfo yet. I logged the issue with ID EMAILJAVA-34961 in our tracking system. You will be notified when this issue is resolved.

I also tried to move message to another folder and got exception:
The string contains invalid characters

I use method IgraphClient.moveMessage(new outlook folder, itemId)

To investigate this case on our side, please share the following:

  • code example reproducing the error
  • stack trace output


Code example:
AzureROPCTokenProvider ta = new AzureROPCTokenProvider(this.tenantId, this.clientId, this.secretId,
user, password, AsposeGraphEmailClient.scopes);
IGraphClient client = GraphClient.getClient(ta, this.tenantId);
GraphFolderInfo inboxFolder = client.listFolders(INBOX);
GraphMessageInfoCollection coll = client.listMessages(inboxFolder.getItemId());
GraphMessageInfo info = coll.get(0);
MapiMessage eml = client.fetchMessage(info.getItemId());;
client.moveMessage(newFolder, eml.getItemId());


Try to use the next code example:

GraphMessageInfoCollection coll = client.listMessages(GraphKnownFolders.Inbox);
GraphMessageInfo info = coll.get(0);
MapiMessage eml = client.fetchMessage(info.getItemId());
GraphFolderInfo newFolder = client.createFolder("NEW_FOLDER");
MapiMessage newEml = client.moveMessage(newFolder.getItemId(), eml.getItemId());
System.out.println("Moved Message ID " + newEml.getItemId());

API Reference: GraphKnownFolders Class

But my new folder already exists. I dont need to create a new one.
In the moveMessage method should I pass the folder name or folder itemId?


You should pass itemId of the new folder to the moveMessage method. You can find the folder by using IGraphClient.listFolders methods like this:

var graphFolderInfoCollection = client.listFolders();

Thanks, It works fine.
Can you please give an ETA for getSize() Method?
We need this method as we have huge email (30 megs file) and such email are ignored to avoid fetchMessage timeout.


The issue has been resolved. The update will be included in version 21.10. This release will be published around the beginning of November.

Note: the method GraphMessageInfo.getSize will return approximated message size.

The issues you have found earlier (filed as EMAILJAVA-34961) have been fixed in this update.