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

标签 c# gmail

我正在尝试将 Gmail 的联系人获取到 C# 桌面表单应用程序中,我已使用 google APi 来实现此目的,我想在按下按钮时在 gridview 中显示 Gmail 的联系人。但当按下按钮时, GridView 中不会显示任何内容。

代码粘贴在下面。

请告诉我并帮助我解决此问题。

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("", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e39096919a828184d1d3d3d3a3848e828a8fcd808c8e" rel="noreferrer noopener nofollow">[email protected]</a>", "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 = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7d19181b3d1a101c1411531e1210" rel="noreferrer noopener nofollow">[email protected]</a>" });
        //lstContacts.Add(new MyClass() { Id = 2, Email = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8de9e8ebcdeae0ece4e1a3eee2e0" rel="noreferrer noopener nofollow">[email protected]</a>" });
        //lstContacts.Add(new MyClass() { Id = 3, Email = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ccaba4a58caba1ada5a0e2afa3a1" rel="noreferrer noopener nofollow">[email protected]</a>" });

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

    }

}

最佳答案

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 = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="680c0d0e280f05090104460b0705" rel="noreferrer noopener nofollow">[email protected]</a>" }); 
    lstContacts.Add(new MyClass() { Id = 2, Email = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="791d1c1f391e14181015571a1614" rel="noreferrer noopener nofollow">[email protected]</a>" });
    lstContacts.Add(new MyClass() { Id = 3, Email = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="36515e5f76515b575f5a1855595b" rel="noreferrer noopener nofollow">[email protected]</a>" });

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

 }

我建议您首先单步执行代码,这应该可以让您更好地了解到底是什么导致 GridView 为空结果。调用 Google Contacts API 或将数据绑定(bind)到 gridview 控件可能会出现问题。

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

另外,我会使用 dataGridView1.DataSource = lstContacts 来进行数据绑定(bind)。

编辑: 该代码当然对我有用。这里显示了 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 dt = new DataTable();
dt.Columns.Add("Email Address");

dt.Rows.Add(new object[] { "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c7a3a2a187a0aaa6aeabe9a4a8aa" rel="noreferrer noopener nofollow">[email protected]</a>" });
dt.Rows.Add(new object[] { "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="75111013351218141c195b161a18" rel="noreferrer noopener nofollow">[email protected]</a>" });
dt.Rows.Add(new object[] { "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f2969794b2959f939b9edc919d9f" rel="noreferrer noopener nofollow">[email protected]</a>" });
dataGridView1.DataSource = dt;
dataGridView1.Show();

关于c# - 从 C# 桌面表单中获取 Gmail 联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16883321/

相关文章:

c# - 一些 NGit 东西阻​​止 C# 应用程序正确关闭

c# - 是否可以通过反射调用私有(private)委托(delegate)?如果是那么如何?如果否,那么原因是什么?

c# - 找不到 System.Deployment 文件 Visual Studio 2017 OpenCV

php - PEAR 邮件连接拒绝 smtp.gmail.com

ipad - iPad 上的 Google gmail : how do they scroll just the right hand side of the page?

c# - 最后用缓动来回摇动一个物体?

c# - 如何隐藏导航栏中的后退按钮xamarin表单

node.js - 使用 Mandrill 发送时是否可以指定我自己的 Message-Id 电子邮件 header ?

c# - CF.NET SMTP - 使用 Gmail 发送电子邮件

javamail-1.4.5 解析通过 imap 收到的 gmail 消息时出错