Import Contacts from Gmail in C# .NET

Gmail is a popular and widely used email service provided by Google. Along with sending and receiving emails, it offers various additional features such as auto-reply, chats, etc. As a programmer, you may come across the scenario when you need to import contacts from a particular Gmail account. To accomplish that from within .NET applications, this article covers how to import Gmail contacts in C# .NET. Moreover, we will demonstrate how to fetch contacts from a specific email group.

C# .NET API to Import Gmail Contacts

To import contacts from a Gmail account, we will use Aspose.Email for .NET. It is a feature-rich API that allows you to create and send emails quite easily. Moreover, it lets you manipulate various email formats including MSG and EML. You can either download the API’s DLL or install it using NuGet.

PM> Install-Package Aspose.Email

Import Contacts from Gmail in C#

To access the contacts from a Gmail account, we need to write some code to handle the user’s information and to perform Gmail 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.

C# Import Contacts from a Gmail Account

At this stage, we are ready to access contacts in a Gmail account. The following are the steps to import contacts from a Gmail account in C#.

  • Create an object of GoogleUser class and initialize it with name, email, password, client ID and client secret.
  • Create two string objects to store access token and refresh token.
  • Call GoogleOAuthHelper.GetAccessToken(GoogleUser, out string, out string) method to get the access and refresh tokens.
  • Get an instance of GmailClient class into an IGmailClient object.
  • Create an array of Contact and get all the contacts using IGmailClient.GetAllContacts() method.
  • Loop through array to access each contact.

The following code sample shows how to import contacts from a Gmail account in C#.

Import Gmail Contacts from a Group in C#

You can also access contacts from a specific email group in Gmail. The following are the steps to perform this operation.

The following code sample shows how to import contacts from a specific email group in C#.

C# API to Import Gmail Contacts - Get a Free License

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

Conclusion

In this article, you have learned how to import Gmail contacts from accounts programmatically in C#. Moreover, you have seen how to access contacts from a particular email group in Gmail. Besides, you can explore other features of Aspose.Email for .NET using the documentation. Also, you can ask your questions via our forum.

See Also