Home > Code > C# > How to Access, Add, Update or Delete Gmail Contacts inside .NET Apps

How to Access, Add, Update or Delete Gmail Contacts inside .NET Apps

by sher azam   on Dec 17, 2014   Category: C#   |  Views: 1375    |  Points: 25   |  Gold 


This technical tip shows how to .NET developers can access, add, update or Delete Gmail contacts inside .NET application. Aspose.Email supports working with Gmail contacts. Using the IGmailClient interface, users can retrieve contacts from a Gmail account, create new contacts, and update as well as delete existing contacts. Gmail allows developers to perform all these using its public developer's API. The following user information is required for working with Gmail contacts: User name, email address, password, client ID, client secret refresh token. This article shows how to access Gmail contacts, create new Gmail contacts, update existing contacts, delete a contact and save contact.

Following is a sample application which can be used to access the detail of contacts in all the groups.

// C# Code Sample

string username = "username";
string email = "email@gmail.com";
string password = "password";
string clientId = "clientid";
string clientSecret = "client secret";
string refresh_token = "refresh token";

//The refresh_token is to be used in below.
GoogleTestUser user = new GoogleTestUser(
username,
email,
password,
clientId, //client id
clientSecret, //client secret
refresh_token); //refresh token

using (IGmailClient client = Aspose.Email.Google.GmailClient.GetInstance(user.ClientId, user.ClientSecret, user.RefreshToken))
{
Contact[] contacts = client.GetAllContacts();
foreach (Contact contact in contacts)
Console.WriteLine(contact.DisplayName + ", " + contact.EmailAddresses[0]);

//Fetch contacts from a specific group
FeedEntryCollection groups = client.FetchAllGroups();
GmailContactGroup group = null;
foreach (GmailContactGroup g in groups)
switch (g.Title)
{
case "TestGroup":
group = g;
break;
}

//Retrieve contacts from the Group
if (group != null)
{
Contact[] contacts2 = client.GetContactsFromGroup(group);
foreach (Contact con in contacts2)
Console.WriteLine(con.DisplayName + "," + con.EmailAddresses[0].ToString());
}
}
}



//VB.NET Code Sample

Dim username As String = "username"
Dim email As String = "email@gmail.com"
Dim password As String = "password"
Dim clientId As String = "client id"
Dim clientSecret As String = "client secret"
Dim refresh_token As String = "refresh token"

'The refresh_token is to be used in below.
'client id
'client secret
Dim user As New GoogleTestUser(username, email, password, clientId, clientSecret, refresh_token)
'refresh token
Using client As IGmailClient = Aspose.Email.Google.GmailClient.GetInstance(user.ClientId, user.ClientSecret, user.RefreshToken)
Dim contacts As Contact() = client.GetAllContacts()
For Each contact As Contact In contacts
Console.WriteLine(contact.DisplayName + ", " + contact.EmailAddresses(0))
Next

'Fetch contacts from a specific group
Dim groups As FeedEntryCollection = client.FetchAllGroups()
Dim group As GmailContactGroup = Nothing
For Each g As GmailContactGroup In groups
Select Case g.Title
Case "TestGroup"
group = g
Exit Select
End Select
Next

'Retrieve contacts from the Group
If group IsNot Nothing Then
Dim contacts2 As Contact() = client.GetContactsFromGroup(group)
For Each con As Contact In contacts2
Console.WriteLine(con.DisplayName + "," + con.EmailAddresses(0).ToString())
Next
End If
End Using


Creating Gmail Contact

//C# Code Sample

string username = "newcustomeroffnet";
string email = "newcustomeroffnet@gmail.com";
string password = "visual2010";
string clientId = "633643385868-v64nsvkmh5ca2dlhkroo8941ne92onov.apps.googleusercontent.com";
string clientSecret = "UQ26EXvvo8L12TNhtLtYbHVO";
string refresh_token = "1/yord7s4aqV8t7S_vT9IqlBO60eIE-hY0WHLfqQ9zOCQ";

//The refresh_token is to be used in below.
GoogleTestUser user = new GoogleTestUser(
username,
email,
password,
clientId, //client id
clientSecret, //client secret
refresh_token); //refresh token

//Gmail Client
IGmailClient client = Aspose.Email.Google.GmailClient.GetInstance(user.ClientId, user.ClientSecret, user.RefreshToken, user.EMail);

//Create a Contact
Contact contact = new Contact();
contact.Prefix = "Prefix";
contact.GivenName = "GivenName";
contact.Surname = "Surname";
contact.MiddleName = "MiddleName";
contact.DisplayName = "Test User 1";
contact.Suffix = "Suffix";

contact.JobTitle = "JobTitle";
contact.DepartmentName = "DepartmentName";
contact.CompanyName = "CompanyName";
contact.Profession = "Profession";

contact.Notes = "Notes";

PostalAddress address = new PostalAddress();
address.Category = PostalAddressCategory.Work;
address.Address = "Address";
address.Street = "Street";
address.PostOfficeBox = "PostOfficeBox";
address.City = "City";
address.StateOrProvince = "StateOrProvince";
address.PostalCode = "PostalCode";
address.Country = "Country";
contact.PhysicalAddresses.Add(address);

PhoneNumber pnWork = new PhoneNumber();
pnWork.Number = "323423423423";
pnWork.Category = PhoneNumberCategory.Work;
contact.PhoneNumbers.Add(pnWork);
PhoneNumber pnHome = new PhoneNumber();
pnHome.Number = "323423423423";
pnHome.Category = PhoneNumberCategory.Home;
contact.PhoneNumbers.Add(pnHome);
PhoneNumber pnMobile = new PhoneNumber();
pnMobile.Number = "323423423423";
pnMobile.Category = PhoneNumberCategory.Mobile;
contact.PhoneNumbers.Add(pnMobile);

contact.Urls.Blog = "Blog.ru";
contact.Urls.BusinessHomePage = "BusinessHomePage.ru";
contact.Urls.HomePage = "HomePage.ru";
contact.Urls.Profile = "Profile.ru";

contact.Events.Birthday = DateTime.Now.AddYears(-30);
contact.Events.Anniversary = DateTime.Now.AddYears(-10);

contact.InstantMessengers.AIM = "AIM";
contact.InstantMessengers.GoogleTalk = "GoogleTalk";
contact.InstantMessengers.ICQ = "ICQ";
contact.InstantMessengers.Jabber = "Jabber";
contact.InstantMessengers.MSN = "MSN";
contact.InstantMessengers.QQ = "QQ";
contact.InstantMessengers.Skype = "Skype";
contact.InstantMessengers.Yahoo = "Yahoo";

contact.AssociatedPersons.Spouse = "Spouse";
contact.AssociatedPersons.Sister = "Sister";
contact.AssociatedPersons.Relative = "Relative";
contact.AssociatedPersons.ReferredBy = "ReferredBy";
contact.AssociatedPersons.Partner = "Partner";
contact.AssociatedPersons.Parent = "Parent";
contact.AssociatedPersons.Mother = "Mother";
contact.AssociatedPersons.Manager = "Manager";

//Email Address
EmailAddress eAddress = new EmailAddress();
eAddress.Address = "email@gmail.com";
contact.EmailAddresses.Add(eAddress);

string contactUri = client.CreateContact(contact);

//VB.NET Code Sample

Dim username As String = "newcustomeroffnet"
Dim email As String = "newcustomeroffnet@gmail.com"
Dim password As String = "visual2010"
Dim clientId As String = "633643385868-v64nsvkmh5ca2dlhkroo8941ne92onov.apps.googleusercontent.com"
Dim clientSecret As String = "UQ26EXvvo8L12TNhtLtYbHVO"
Dim refresh_token As String = "1/yord7s4aqV8t7S_vT9IqlBO60eIE-hY0WHLfqQ9zOCQ"

'The refresh_token is to be used in below.
'client id
'client secret
Dim user As New GoogleTestUser(username, email, password, clientId, clientSecret, refresh_token)
'refresh token
'Gmail Client
Dim client As IGmailClient = Aspose.Email.Google.GmailClient.GetInstance(user.ClientId, user.ClientSecret, user.RefreshToken, user.EMail)

'Create a Contact
Dim contact As New Contact()
contact.Prefix = "Prefix"
contact.GivenName = "GivenName"
contact.Surname = "Surname"
contact.MiddleName = "MiddleName"
contact.DisplayName = "Test User 1"
contact.Suffix = "Suffix"

contact.JobTitle = "JobTitle"
contact.DepartmentName = "DepartmentName"
contact.CompanyName = "CompanyName"
contact.Profession = "Profession"

contact.Notes = "Notes"

Dim address As New PostalAddress()
address.Category = PostalAddressCategory.Work
address.Address = "Address"
address.Street = "Street"
address.PostOfficeBox = "PostOfficeBox"
address.City = "City"
address.StateOrProvince = "StateOrProvince"
address.PostalCode = "PostalCode"
address.Country = "Country"
contact.PhysicalAddresses.Add(address)

Dim pnWork As New PhoneNumber()
pnWork.Number = "323423423423"
pnWork.Category = PhoneNumberCategory.Work
contact.PhoneNumbers.Add(pnWork)
Dim pnHome As New PhoneNumber()
pnHome.Number = "323423423423"
pnHome.Category = PhoneNumberCategory.Home
contact.PhoneNumbers.Add(pnHome)
Dim pnMobile As New PhoneNumber()
pnMobile.Number = "323423423423"
pnMobile.Category = PhoneNumberCategory.Mobile
contact.PhoneNumbers.Add(pnMobile)

contact.Urls.Blog = "Blog.ru"
contact.Urls.BusinessHomePage = "BusinessHomePage.ru"
contact.Urls.HomePage = "HomePage.ru"
contact.Urls.Profile = "Profile.ru"

contact.Events.Birthday = DateTime.Now.AddYears(-30)
contact.Events.Anniversary = DateTime.Now.AddYears(-10)

contact.InstantMessengers.AIM = "AIM"
contact.InstantMessengers.GoogleTalk = "GoogleTalk"
contact.InstantMessengers.ICQ = "ICQ"
contact.InstantMessengers.Jabber = "Jabber"
contact.InstantMessengers.MSN = "MSN"
contact.InstantMessengers.QQ = "QQ"
contact.InstantMessengers.Skype = "Skype"
contact.InstantMessengers.Yahoo = "Yahoo"

contact.AssociatedPersons.Spouse = "Spouse"
contact.AssociatedPersons.Sister = "Sister"
contact.AssociatedPersons.Relative = "Relative"
contact.AssociatedPersons.ReferredBy = "ReferredBy"
contact.AssociatedPersons.Partner = "Partner"
contact.AssociatedPersons.Parent = "Parent"
contact.AssociatedPersons.Mother = "Mother"
contact.AssociatedPersons.Manager = "Manager"

'Email Address
Dim eAddress As New EmailAddress()
eAddress.Address = "email@gmail.com"
contact.EmailAddresses.Add(eAddress)

Dim contactUri As String = client.CreateContact(contact)



Update Gmail Contact

//Once a contact is retrieved, its attributes can be updated and the contact can be saved back to the Gmail account. The sample code below retrieves contacts from a Gmail account and then modifies one of these which is then saved back.

//C# Code Sample

string username = "username";
string email = "email@gmail.com";
string password = "password";
string clientId = "clientid";
string clientSecret = "client secret";
string refresh_token = "refresh token";

//The refresh_token is to be used in below.
GoogleTestUser user = new GoogleTestUser(
username,
email,
password,
clientId, //client id
clientSecret, //client secret
refresh_token); //refresh token

using (IGmailClient client = Aspose.Email.Google.GmailClient.GetInstance(user.ClientId, user.ClientSecret, user.RefreshToken))
{
Contact[] contacts = client.GetAllContacts();
Contact contact = contacts[0];
contact.JobTitle = "Manager IT";
contact.DepartmentName = "Customer Support";
contact.CompanyName = "Aspose";
contact.Profession = "Software Developer";
client.UpdateContact(contact);
}



Deleting a Gmail Contact

C# Code Sample

client.DeleteContact(contact.Id.GoogleId);



//Saving a Contact

C# Code Sample

contact.Save("contact.msg", ContactSaveFormat.Msg);
contact.Save("contact.vcf", ContactSaveFormat.VCard);



Overview: Aspose.Email for .NET

Aspose.Email for .NET is a set of components allowing developers to easily implement email functionality within their ASP.NET web applications, web services & Windows applications. It Supports Outlook PST, EML, MSG & MHT formats. It allows developers to work with SMTP, POP3, FTP & MS Exchange servers. It supports mail merge, iCalendar, customized header & body, header information, embedded files, Twitter & many more. It makes it easy to work with HTML or plain text emails & their attachments.

- Homepage of Aspose.Email for .NET: http://www.aspose.com/.net/email-component.aspx

- Download Aspose.Email for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.email-for-.net/default.aspx



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts