如何获取gmail联系人阿
高手给个代码。。
谢了。

解决方案 »

  1.   

    http://www.pudn.com/downloads139/sourcecode/windows/csharp/detail599885.html
      

  2.   

    里面的代码现在  获取不了  gmail联系人 了
    不知道为什么 ,原来确实可以的。
    代码如下public class GmailExtract : IMailContactExtract
        {
            private const string ContinueUrl = "http://mail.google.com/mail?ui=html&zy=l";
            private const string ExportUrl = "https://mail.google.com/mail/contacts/data/export?exportType=ALL&groupToExport=&out=GMAIL_CSV";
            private const string LoginRefererUrl = "https://www.google.com/accounts/ServiceLogin?service=mail&passive=true&rm=false&continue=http%3A%2F%2Fmail.google.com%2Fmail%2F%3Fui%3Dhtml%26zy%3Dl&ltmpl=default&ltmplcache=2";                private const string LoginUrl = "https://www.google.com/accounts/ServiceLoginAuth?service=mail";
            private const string UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1; .NET CLR 1.0.3705; .NET CLR 3.0.04506.30)";
            
            #region IMailContactExtract Members
            
            public bool Extract( NetworkCredential credential, out MailContactList list )
            {
                bool result = false;
                list = new MailContactList();            try
                {
                    CookieCollection cookies = new CookieCollection();                // Prepare login form data
                    HttpValueCollection loginFormValues = new HttpValueCollection();
                    loginFormValues["ltmpl"] = "default";
                    loginFormValues["ltmplcache"] = "2";
                    loginFormValues["continue"] = ContinueUrl;
                    loginFormValues["service"] = "mail";
                    loginFormValues["rm"] = "false";
                    loginFormValues["hl"] = "en";
                    loginFormValues["Email"] = credential.UserName;
                    loginFormValues["Passwd"] = credential.Password;
                    loginFormValues["PersistentCookie"] = "true";
                    loginFormValues["rmShown"] = "1";
                    loginFormValues["null"] = "Sign In";                // Convert to bytes
                    byte[] loginPostData = Encoding.UTF8.GetBytes(loginFormValues.ToString(true));                HttpWebRequest loginRequest = (HttpWebRequest)WebRequest.Create(LoginUrl);
                    loginRequest.Method = "POST";
                    loginRequest.UserAgent = UserAgent;
                    loginRequest.Referer = LoginRefererUrl;
                    loginRequest.ContentType = "application/x-www-form-urlencoded";
                    loginRequest.ContentLength = loginPostData.Length;
                    loginRequest.AllowAutoRedirect = false;                // Create cookie container
                    loginRequest.CookieContainer = new CookieContainer();                // Add post data to request
                    Stream stream;
                    using (stream = loginRequest.GetRequestStream())
                    {
                        stream.Write(loginPostData, 0, loginPostData.Length);
                    }                HttpWebResponse loginResponse = (HttpWebResponse)loginRequest.GetResponse();                cookies.Add(loginResponse.Cookies);                // Create request to export Google CSV page
                    HttpWebRequest contactsRequest = ( HttpWebRequest ) WebRequest.Create( ExportUrl );
                    contactsRequest.Method = "GET";
                    contactsRequest.UserAgent = UserAgent;
                    contactsRequest.Referer = loginResponse.ResponseUri.ToString();                // use cookie gotten from login page
                    contactsRequest.CookieContainer = new CookieContainer();                foreach (Cookie cookie in cookies)
                    {
                        contactsRequest.CookieContainer.Add(cookie);
                    }                HttpWebResponse exportResponse = (HttpWebResponse)contactsRequest.GetResponse();//上句为问题语句,好像 是https://mail.google.com/mail/contacts/data/export?exportType=ALL&groupToExport=&out=GMAIL_CSV  访问时出现问题,是不是近期gmail邮箱服务器 做什么改变了。???                // Read data from response stream
                    string csvData;
                    using ( Stream responseStream = exportResponse.GetResponseStream() )
                    {
                        using ( StreamReader streamRead = new StreamReader( responseStream ) )
                        {
                            csvData = streamRead.ReadToEnd();
                        }
                    }                // parse google csv
                    int cnt = 0;
                    string[] lines = csvData.Split( '\n' );
                    foreach ( string line in lines )
                    {
                        if (cnt == 0)
                        {
                            cnt += 1;
                            continue;
                        }
                        string[] values = line.Split( ',' );
                        if ( values.Length < 29 )
                        {
                            continue;
                        }  
                        MailContact mailContact = new MailContact();
                        mailContact.Email = values[ 28 ];
                        mailContact.Name = values[ 0 ];
                        list.Add( mailContact );
                    }
                    return true;
                }
                catch (System.Net.WebException ex)
                {
                    Console.WriteLine(ex.Message);
                }
                //return result;
                return true;
            }
      

  3.   

    Google开放了相关的API
    楼主看这里:
    Google Contacts Data APIhttp://code.google.com/apis/contacts/docs/2.0/developers_guide_dotnet.html#retrieving_without_query
      

  4.   

    项目添加dll引用:
    Google.GData.Client.dll
    Google.GData.Contacts.dll
    Google.GData.Extensions.dll using System;
    using Google.Contacts;
    using Google.GData.Client;
    using Google.GData.Contacts;
    using Google.GData.Extensions;namespace ConsoleApplication9 {
        class Program {
            static void Main() {
                RequestSettings rs = new RequestSettings("", "[email protected]", "yourpassword");
                // AutoPaging results in automatic paging in order to retrieve all contacts
                rs.AutoPaging = true;
                ContactsRequest cr = new ContactsRequest(rs);            Feed<Contact> f = cr.GetContacts();
                foreach (Contact contact in f.Entries) {
                    Console.WriteLine("\t" + contact.Title);
                    foreach (EMail email in contact.Emails) {
                        Console.WriteLine("\t" + email.Address);
                    }
                    foreach (GroupMembership g in contact.GroupMembership) {
                        Console.WriteLine("\t" + g.HRef);
                    }
                    foreach (IMAddress im in contact.IMs) {
                        Console.WriteLine("\t" + im.Address);
                    }
                }
                Console.ReadLine();
            }
        }
    }
      

  5.   

    我有个获取msn联系人的帖子,不知道对你有没有用。你自己看哈
      

  6.   

    cppfaq
     
    (风吹过) 太谢谢你了
    给你磕头了