将Gmail联系人从C#桌面表单中获取

时间:2013-06-02 13:43:11

标签: c# gmail

我正在尝试将Gmail的联系人转移到C#桌面应用程序,我已经使用谷歌APi为此目的,我想在按下按钮时在gridview中显示gmail的联系人。但是按下按钮时,网格视图中不会显示任何内容。

代码粘贴在下面。

请让我知道并帮助我解决此问题。

public class MyClass
{
    public int Id { get; set; }
    public string Email { get; set; }
}

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void FetchContactList()
    {
        // Define string of list
        List<string> lstContacts = new List<string>();

        // Below requestsetting class take 3 parameters applicationname, gmail username, gmail password. Provide appropriate Gmail account details
        RequestSettings rsLoginInfo = new RequestSettings("", "suryabg2000@gmail.com", "XXXXXX");
        rsLoginInfo.AutoPaging = true;
        ContactsRequest cRequest = new ContactsRequest(rsLoginInfo);

        // fetch contacts list
        Feed<Contact> feedContacts = cRequest.GetContacts();

        // looping the feedcontact entries
        try
        {
            foreach (Contact gmailAddresses in feedContacts.Entries)
            {
                // Looping to read email addresses
                foreach (EMail emailId in gmailAddresses.Emails)
                {
                    lstContacts.Add(emailId.Address);
                }
            }
            // finally binding the list to gridview defined in above step

           // dataGridView1.DataSource = lstContacts;
           ////dataGridView1.DataBind();
           ////dataGridView1.DataSource = dataGridView1;
           // dataGridView1.Show();
        }
        catch (Exception)
        {
            MessageBox.Show("Error Please enter the correct credentials","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
            //throw;
        }

    }

    private void button1_Click(object sender, EventArgs e)
    {
        List<MyClass> lstContacts = new List<MyClass>();
        //lstContacts.Add(new MyClass() { Id = 1, Email = "def@gmail.com" });
        //lstContacts.Add(new MyClass() { Id = 2, Email = "def@gmail.com" });
        //lstContacts.Add(new MyClass() { Id = 3, Email = "ghi@gmail.com" });

        dataGridView1.DataSource = new BindingSource(lstContacts, null);
        dataGridView1.Show();

    }

}

2 个答案:

答案 0 :(得分:1)

public class MyClass
{
    public int Id { get; set; }
    public string Email { get; set; }
}
private void button1_Click(object sender, EventArgs e)
{
    List<MyClass> lstContacts = new List<MyClass>();
    //need to add items to list
    lstContacts.Add(new MyClass() { Id = 1, Email = "def@gmail.com" }); 
    lstContacts.Add(new MyClass() { Id = 2, Email = "def@gmail.com" });
    lstContacts.Add(new MyClass() { Id = 3, Email = "ghi@gmail.com" });

    dataGridView1.DataSource = lstContacts;
    dataGridView1.Show();

 }

我建议您首先单步执行代码,这样可以更好地了解导致网格视图空结果的确切原因。调用Google Contacts API或将数据绑定到gridview控件可能会出现问题。

但只是通过查看你的点击事件处理程序,我不禁要问你为什么注释掉lstContacts.Add行。列表lstContacts仍然是空的,不是吗?

另外,我会使用dataGridView1.DataSource = lstContacts代替数据绑定。

编辑: 代码肯定对我有用。这里显示了3行。

示例:

使用包装类

public class StringValue
{
    public StringValue(string s)
    {
        _value = s;
    }
    public string Value { get { return _value; } set { _value = value; } }
    string _value;
}
List<StringValue> lstContacts = new List<StringValue>();
lstContacts.Add("your email address");
dataGridView1.DataSource = lstContacts;
dataGridView1.Show();

使用DataTable

DataTable dt = new DataTable();
dt.Columns.Add("Email Address");

dt.Rows.Add(new object[] { "def@gmail.com" });
dt.Rows.Add(new object[] { "def@gmail.com" });
dt.Rows.Add(new object[] { "def@gmail.com" });
dataGridView1.DataSource = dt;
dataGridView1.Show();

答案 1 :(得分:1)

我已经借助下面提到的代码将gmail的联系人提取到C#桌面应用程序。使用GOOGLE API为目的!!! 下面粘贴了带有数据集的正确和可用的代码。

public class MyClass
{
    public int Id { get; set; }
    public string Email { get; set; }
}

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void fetchContactList()
    {
        // Define string of list
        List<string> lstContacts = new List<string>();



        // Below requestsetting class take 3 parameters applicationname, gmail username, gmail password. Provide appropriate Gmail account details
        RequestSettings rsLoginInfo = new RequestSettings("", textBox1.Text, textBox2.Text);
        rsLoginInfo.AutoPaging = true;
        ContactsRequest cRequest = new ContactsRequest(rsLoginInfo);

        // fetch contacts list
        Feed<Contact> feedContacts = cRequest.GetContacts();


        //dataGridView1.ColumnCount = 1;
        //dataGridView1.Columns[0].Name = "Product ID";



        // looping the feedcontact entries
        try
        {
           // dataGridView1.Columns.Add("Name", "Name");
            RichTextBox rtb = new RichTextBox();
            string email = "";
            DataTable dt = new DataTable();
            dt.Columns.Add("Email Address");
            foreach (Contact gmailAddresses in feedContacts.Entries)
            {
                // Looping to read email addresses
                foreach (EMail emailId in gmailAddresses.Emails)
                {
                   dt.Rows.Add(new object[] {email=emailId.Address});
                   dataGridView1.DataSource = dt;  
                }

                dataGridView1.Show();
            }

        }
        catch (Exception)
        {
            MessageBox.Show("Error Please enter the correct credentials","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
            //throw;
        }

    }

    private void button1_Click(object sender, EventArgs e)
    {

        fetchContactList();

    }

}

}