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

Free Support Forum - aspose.com

How to Use IGraphClient and ITokenProvider?

Hi

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?

Regards,

@elie.kach,
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[]
{
    "https://graph.microsoft.com/.default",
};

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

for (var folderInfo : graphClient.listFolders())
{
    System.out.println(folderInfo.getDisplayName());
}

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

Documents:
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?

Regards

@elie.kach,
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.

Thanks,
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)

@elie.kach,
To investigate this case on our side, please share the following:

  • code example reproducing the error
  • stack trace output

Hi

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());

Regards,

@elie.kach,
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?

Regards,

@elie.kach,
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.

Regards,

@elie.kach,
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.