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 Manage Gmail Contacts
- Create a Contact on Gmail
- Update a Contact on Gmail
- Delete a Contact on Gmail
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.
- First, create a Google user, get an access token, and initialize an IGmailClient object.
- Then, create an object of the Contact class.
- Set properties of the contact such as name, prefix, profession, etc.
- To set the postal address, create an instance of PostalAddress and set its properties.
- Add newly created address to the collection using the Contact.getPhysicalAddresses().add(PostalAddress) method.
- Set phone number details using PhoneNumber class.
- Add phone number details to collection using Contact.getPhoneNumbers().add(PhoneNumber) method.
- Create an instance of EmailAddress class, set the email address, and assign it to the contact.
- Finally, call IGmailClient.createContact(Contact) method to create Gmail contact.
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.
- First, create a Google user, get an access token, and initialize an IGmailClient object.
- Then, get contacts in an array using IGmailClient.getAllContacts() method.
- After that, fetch the required contact from the array in a Contact object.
- Finally, update the details of contact and call IGmailClient.updateContact(contact) method.
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.
- First, create a Google user, get an access token, and initialize an IGmailClient object.
- Then, get contacts in an array using IGmailClient.getAllContacts() method.
- Filter the desired contact from the array in a Contact object.
- Finally, call IGmailClient.deleteContact(Contact.Id.GoogleId) method to delete the contact.
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.