Create, Update, and Delete Contacts in Gmail using Java

Gmail is one of the commonly used online applications for sending and receiving emails. In addition, it allows working with calendars, contacts, chats, etc., and provides various other collaboration services. Recently, in an article, we have covered how to import contacts from a Gmail account. Today, you will learn how to create, update, and delete contacts in a Gmail account using Java.

Java API to Create, Update and Delete Gmail Contacts

To create and manipulate contacts in a Gmail account, we will use Aspose.Email for Java. It is a feature-rich API that lets you create and send emails and work with popular email clients. 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>

After installing the API, you need to create a project on the Google Developer Console, which will allow you to communicate with Gmail. To create one, you can follow this guide.

Now, create a helper class named GoogleOAuthHelper to take care of the authentication of a Gmail account. Also, create a class named OAuthUser to store user information. The following is the complete implementation of both classes.

Create a Contact on Gmail in Java

The following are the steps to create a contact on Gmail in Java.

The following code sample shows how to create a contact on Gmail in Java.

Update a Contact on Gmail in Java

You can also update the details of a Gmail contact after accessing it. The following are the steps to update a contact in a Gmail account in Java.

The following code sample shows how to update a contact in Gmail in Java.

Delete a Contact on Gmail in Java

Finally, let’s see how to delete a Gmail contact using Java. The following are the steps to perform this operation.

The following code sample shows how to delete a contact on Gmail in Java.

Get a Free API License

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

Conclusion

In this article, you have learned how to create and update contacts in a Gmail account in Java. Furthermore, you have seen how to delete a Gmail contact programmatically. Besides, you can visit the documentation to explore other features of Aspose.Email for Java. In case you would have any questions, you can post to our forum.

See Also