Gmail is one of the popular and widely used email applications around the globe. Along with managing emails, it allows working with calendars, contacts, chats, etc. and provides other collaboration services. In the previous article, you have seen how to import contacts from a Gmail account within a .NET application. In this article, we will cover how to create, update, and delete contacts in a Gmail account using C# .NET.
- C# .NET API to Manage Gmail Contacts
- Create a Contact on Gmail
- Update a Contact on Gmail
- Delete a Contact on Gmail
C# .NET API to Create, Update and Delete Gmail Contacts
To create and manipulate contacts in a Gmail account, we will use Aspose.Email for .NET. It is an email processing API that lets you manipulate emails and work with popular email clients. You can either download the API’s DLL or install it from NuGet using the following command.
PM> Install-Package Aspose.Email
Before you start working, 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.
To access and manipulate contacts in a Gmail account, we need to write some code to handle the user’s information and perform authentication. For the Gmail user, we will first create a class named TestUser and then inherit it from GoogleUser class. The following is the complete implementation of both of the classes.
Now, we need to create a helper class that will take care of the authentication of a Gmail account. We will name this class as GoogleOAuthHelper. The following is the complete implementation of this class.
Create a Contact on Gmail in C#
The following are the steps to create a contact on Gmail in C#.
- 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.PhysicalAddresses.Add(PostalAddress) method.
- Set phone number details using PhoneNumber class.
- Add phone number details to collection using Contact.PhoneNumbers.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 C#.
Update a Contact on Gmail in C#
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 C#.
- First, create a Google user, get an access token, and initialize an IGmailClient object.
- Get contacts in an array using IGmailClient.GetAllContacts() method.
- Fetch required contact from the array in a Contact object.
- Update the details of contact and call IGmailClient.UpdateContact(contact) method.
The following code sample shows how to update a contact in Gmail in C#.
Delete a Contact on Gmail in C#
Finally, let’s see how to delete a Gmail contact using C#. The following are the steps to perform this operation.
- First, create a Google user, get an access token, and initialize an IGmailClient object.
- 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 C#.
Get a Free API License
You can use Aspose.Email for .NET 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 C# .NET. 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 .NET. In case you would have any questions, you can post to our forum.