Last updated on March 24, 2022 by Usman Aziz
Blog Home

Import Gmail Contacts in C# .NET

Share on Facebook
Facebook
Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin
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.

  • .NET API to Import Contacts from Gmail
  • Import Contacts from Gmail in C#
    • Import Gmail Contacts
    • Get Contacts from a Specific 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.

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

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

  • 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 token.
  • Get an instance of GmailClient class into an IGmailClient object.
  • Get all email groups into a ContactGroupCollection object using IGmailClient.GetAllGroups() method.
  • Access the desired GoogleContactGroup using its name.
  • Create an array of Contact and get all the contacts from group using IGmailClient.GetContactsFromGroup(string) method.
  • Loop through array to access each contact.

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

Get a Free API 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

  • Create and Send Outlook Emails using C#

This entry was posted in Aspose.Email Product Family and tagged Dotnet API to Import Contacts from Gmail, Import Contacts from Email Group in Csharp, Import Contacts from Gmail in Csharp. .

← Read Outlook MSG Files in C#
Create Flowchart Programmatically in C# →
  • Search

  • Follow Us

    RSS Facebook Twitter YouTube LinkedIn

  • Categories

    • Aspose.Total Product Family
      (455)
    • Aspose.Words Product Family
      (812)
    • Aspose.PDF Product Family
      (505)
    • Aspose.Cells Product Family
      (602)
    • Aspose.Email Product Family
      (370)
    • Aspose.Slides Product Family
      (533)
    • Aspose.Imaging Product Family
      (137)
    • Aspose.BarCode Product Family
      (271)
    • Aspose.Diagram Product Family
      (154)
    • Aspose.Tasks Product Family
      (174)
    • Aspose.OCR Product Family
      (93)
    • Aspose.Note Product Family
      (71)
    • Aspose.CAD Product Family
      (67)
    • Aspose.3D Product Family
      (79)
    • Aspose.HTML Product Family
      (70)
    • Aspose.GIS Product Family
      (30)
    • Aspose.ZIP Product Family
      (20)
    • Aspose.Page Product Family
      (32)
    • Aspose.PSD Product Family
      (33)
    • Aspose.OMR Product Family
      (11)
    • Aspose.PUB Product Family
      (15)
    • Aspose.SVG Product Family
      (12)
    • Aspose.Finance Product Family
      (7)
    • Aspose.Drawing Product Family
      (9)
    • Aspose.Font Product Family
      (13)
    • Aspose.TeX Product Family
      (11)
    • Success Stories
      (157)
  • Archives

    • ►2022 (258)
      • June
      • May
      • April
      • March
      • February
      • January
    • ►2021 (479)
      • December
      • November
      • October
      • September
      • August
      • July
      • June
      • May
      • April
      • March
      • February
      • January
    • ►2020 (202)
      • December
      • November
      • October
      • September
      • August
      • July
      • June
      • May
      • April
      • March
      • February
      • January
    • ►2019 (211)
      • December
      • November
      • October
      • September
      • August
      • July
      • June
      • May
      • April
      • March
      • February
      • January
    • ►2018 (389)
      • December
      • November
      • October
      • September
      • August
      • July
      • June
      • May
      • April
      • March
      • February
      • January
    • ►2017 (398)
      • December
      • November
      • October
      • September
      • August
      • July
      • June
      • May
      • April
      • March
      • February
      • January
    • ►2016 (368)
      • December
      • November
      • October
      • September
      • August
      • July
      • June
      • May
      • April
      • March
      • February
      • January
    • ►2015 (383)
      • December
      • November
      • October
      • September
      • August
      • July
      • June
      • May
      • April
      • March
      • February
      • January
    • ►2014 (305)
      • December
      • November
      • October
      • September
      • August
      • July
      • June
      • May
      • April
      • March
      • February
      • January
    • ►2013 (226)
      • December
      • November
      • October
      • September
      • August
      • July
      • June
      • May
      • April
      • March
      • February
      • January
    • ►2012 (210)
      • December
      • November
      • October
      • September
      • August
      • July
      • June
      • May
      • April
      • March
      • February
      • January
    • ►2011 (229)
      • December
      • November
      • October
      • September
      • August
      • July
      • June
      • May
      • April
      • March
      • February
      • January
    • ►2010 (280)
      • December
      • November
      • October
      • September
      • August
      • July
      • June
      • May
      • April
      • March
      • February
      • January
    • ►2009 (244)
      • December
      • November
      • October
      • September
      • August
      • July
      • June
      • May
      • April
      • March
      • February
      • January
    • ►2008 (153)
      • December
      • November
      • October
      • September
      • August
      • July
      • June
      • May
      • April
      • March
      • February
      • January
    • ►2007 (244)
      • December
      • November
      • October
      • September
      • August
      • July
      • June
      • May
      • April
      • March
      • February
      • January
    • ►2006 (294)
      • December
      • November
      • October
      • September
      • August
      • July
      • June
      • May
      • April
      • March
      • February
      • January
    • ►2005 (55)
      • December
      • November
      • October
      • September
      • August
      • July
      • June
      • May
      • April
      • March
      • February
      • January
    • ►2004 (25)
      • December
      • November
      • October
      • September
      • August
      • July
      • March
    • ►2003 (4)
      • December
      • August
      • March
      • January
    • ►2002 (1)
      • March