Create and Send Messages using Microsoft Graph API in Java

Microsoft Graph API allows you to access and utilize various services of Office 365 and Microsoft Cloud programmatically. In the previous article, we have shown you how to use Microsoft Graph API to create and update folders. In this article, we will demonstrate how to create and send messages using Graph API in Java.

Java Microsoft Graph API to Create and Send Messages

Aspose.Email for Java provides a range of features to create and process emails. In addition, it allows you to work with Microsoft Outlook, Exchange and Graph API seamlessly. You can either download the API or install it using the following Maven configurations.

Repository:

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>http://repository.aspose.com/repo/</url>
</repository>

Dependency:

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-email</artifactId>
    <version>22.3</version>
    <classifier>jdk16</classifier>
</dependency>

Create Messages using Microsoft Graph API in Java

To communicate with the Graph API, you need to first implement the ITokenProvider interface for authenticating the requests. The following is the basic implementation of ITokenProvider.

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

The following are the steps to create a message using Microsoft Graph API in Java.

The following code sample shows how to create a message using Microsoft Graph API in Java.

// Get graph client
IGraphClient client = GraphClient.getClient(tokenProvider);
// Create message object and set properties
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");
// Create message in inbox
MapiMessage createdMessage = client.createMessage(GraphKnownFolders.Inbox, message);

Send Messages using Microsoft Graph API in Java

Let’s now have a look at how to send a message using Microsoft Graph API in Java.

The following code sample shows how to send a message using Microsoft Graph API in Java.

// Get graph client
IGraphClient client = GraphClient.getClient(tokenProvider);
// Create message object and set properties
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");
// Send message
client.send(message);

Get a Free API License

You can get a free temporary license to use Aspose.Email for Java without evaluation limitations.

Conclusion

In this article, you have learned how to use Microsoft Graph API to create and send messages in Java. You can simply install Aspose.Email and integrate the provided code samples into your Java applications. In addition, you can have a look at other features offered by Aspose.Email for Java using the documentation. Also, in case you would have any questions, you can post to our forum.

See Also