Lấy danh sách liên hệ từ gmail bằng C# – Get Gmail contact list using C#

Lấy danh sách liên hệ từ gmail bằng C# – Get Gmail contact list using C#

Việc lấy danh sách liên hệ từ gmail có rất nhiều cách, đơn giản nhất là sử dụng tính năng export contacts được tích hợp sẵn trong gmail. Nhưng  với ngôn ngữ C# thì làm thể nào để chúng ta lấy ra danh sách liên hệ từ gmail. Điều này khá thú vị đúng không? Và Google đã cung cấp cho chúng ta các API để giúp chúng ta thực hiện những điều nói trên.

1. Download và cài đặt Google data APIs cho .NET

-  Đầu tiên chúng ta download Google Data APIs cho .NET tại đây. Sau đó tiến hành cài đặt bình thường.

image

-  Mặc định chương trình sẽ được cài vào C:\Program Files\Google\Google Data API SDK.

2. Sử dụng Google Data APIs trong .NET

-  Khi cài Google Data APIs thì theo mặc định đường dẫn chứa các file *.dll sử dụng khi lập trình sẽ nằm trong đường dẫn: C:\Program Files\Google\Google Data API SDK\Redist như: Google.GData.Client.dll, Google.GData.CodeSearch.dll, Google.GData.Contacts.dll…

-  Để sử dụng những APIs mà google đã cung cấp nói trên trong C#. Chúng ta tiến hành tạo project C# một cách bình thường (Lưu ý sủ dụng .NET 2.0 để tránh tình trạng báo lỗi vì Google APIs hình như chưa hỗ trợ cho .NET 4.0)

image

-  Sau đó add các file .dll cần thiết mà Google cung cấp để sử dụng trong chương trình này: Google.GData.Client.dll,  Google.GData.Contacts.dll, Google.Gdata.Extension.dll. Bằng cách: Click chuột phải vào project trong cửa sổ solution explorer chọn Add Reference

image

  Trong cửa sổ hiện ra chọn tab Browse rồi duyệt tới đường dẫn đã cài Google APIs (mặc định là C:\Program Files\Google\Google Data API SDK\Redist). Rồi chọn 3 file .dll nói trên sau đó Ok.

image

Sau khi thực hiện tao tác add reference trên thì trong thanh solution explorer sẽ xuất hiện các file .dll đã add vào reference như hình bên dưới.

image

-  Bây giờ chúng ta có thể sử dụng các APIs trong các thư viện vừa add vào để viết code cho chương trình.  Trong code để sử dụng các APIs trên chúng ta cần khai báo using:

using Google.GData.Client;
using Google.Contacts;


3. Source code chương trình lấy danh sách liên hệ từ Gmail

-  Chương trình được phân làm 2 lớp Form: 1 Form dùng để đăng nhập (class Form1.cs) và 1 Form để hiển thị danh sách liên hệ (class Contacts.cs).

image

image

- Source code của Class Form1.cs như sau:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using Google.GData.Client;
using Google.Contacts;
using System.Windows.Forms;

namespace Get_gmail_contacts
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            txtPass.PasswordChar = '*';
        }

        private Service service;
        private string authToken;

        public Form1(Service serviceToUse)
        {
            InitializeComponent();
            service = serviceToUse;
        }

        public Form1(Service serviceToUse, string username)
        {
            InitializeComponent();
            service = serviceToUse;
            txtUser.Text = username;
        }

        public string AuthenticationToken
        {
            get
            {
                return authToken;
            }
        }

        public bool RememberAuthentication
        {
            get
            {
                return cbToken.Visible;
            }
            set
            {
                cbToken.Visible = value;
            }
        }

        public string user
        {
            get
            {
                return txtUser.Text;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            authToken = null;
            this.Close();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            this.Hide();
            Contacts fC = new Contacts();
            fC.setText("GoogleTest", txtUser.Text,txtPass.Text);
            fC.Show();
        }

        private void txtUser_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnLogin_Click(null, null);
            }
        }

        private void txtPass_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnLogin_Click(null, null);
            }
        }
    }
}

-  Và class Contacts.cs như sau:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Google.GData.Client;
using Google.Contacts;
using Google.GData.Extensions;
using Google.GData.Contacts;

namespace Get_gmail_contacts
{
    public partial class Contacts : Form
    {
        public Contacts()
        {
            InitializeComponent();
        }

        public List<GoogleContacts> list;
        public List<GoogleContacts> GetGoogleContacts(string appName, string un, string pwd)
        {
            List<GoogleContacts> contactList = new List<GoogleContacts>();
            RequestSettings settings = new RequestSettings(appName, un, pwd);
            settings.AutoPaging = true;
            ContactsRequest request = new ContactsRequest(settings);
            Feed<Contact> feed = request.GetContacts();
            foreach (Contact contact in feed.Entries)
            {
                GoogleContacts c = new GoogleContacts();
                c.title = string.IsNullOrEmpty(contact.Title) ? "Chua dat ten" : contact.Title;
                c.im = contact.IMs.Count == 0 ? " " : contact.IMs[0].Address;
                c.email = contact.Emails.Count == 0 ? " " : contact.Emails[0].Address;
                contactList.Add(c);
            }
            return contactList;
        }

        public void addContactsToListBox(List<GoogleContacts> contactList)
        {
            lbName.Text = "Số liên hệ trong danh sách: " + contactList.Count.ToString();

            foreach (GoogleContacts contact in contactList)
            {
                lbxContacts.Items.Add(contact.title + " - " + contact.email + " - " + contact.im);
            }
        }

        public void setText(string a, string b, string c)
        {
            list = GetGoogleContacts(a, b, c);
        }

        private void Contacts_Load(object sender, EventArgs e)
        {
            addContactsToListBox(list);
        }
    }

    public class GoogleContacts
    {
        public string title { get; set; }
        public string email { get; set; }
        public string im { get; set; }
    }
}

-  Click vào đây để download source code demo chương trình lấy danh sách liên hệ gmail viết trên Visual studio 2010 bằng C#. (Click here to download demo source code Get Gmail Contact List Program using C#).

Chúc thành công!

(Tham khảo Google Data APIs)

Bạn thấy bài viết này như thế nào?: 
No votes yet
Ảnh của Binh Tran Thanh

Drupal Consultant

Started my career as a drupal8 developer in EM Solutions . I love learning Web technologies like HTML, CSS, PHP, Jquery Ajax and Drupal backend . Currently working as a drupal backend developer.

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Adobe

Tin tặc tấn công máy chủ của Adobe và nhúng mã độc

Hãng phần mềm Adobe vừa xác nhận và thông báo việc hệ thống cấp phát chữ kí điện tử của mình bị tin tặc tấn công với mục đích “hợp thức hóa” một số mã độc nguy hiểm.

Drupal tại sao chọn - Why choose Drupal?

Drupal tại sao chọn - Why choose Drupal?

Drupal is the leading open source content management system for developing sophisticated, flexible and robust websites, social media networks and applications.

Drupal 8 Module Development: Phần 2 - tạo Forms

Drupal 8 Module Development: Phần 2 - tạo Forms

Like all Drupal 8 module development, creating forms isn't quite as straight forward as it has been in the past using the hook system.

Công ty diệt chuột T&C

 

Diet con trung