GmailExtract.cs
上传用户:asz878
上传日期:2020-04-01
资源大小:189k
文件大小:5k
源码类别:

WEB邮件程序

开发平台:

Visual C++

  1. /*
  2.  * 作者:方宏俊
  3.  * 日期:2008-10-23
  4.  * 描述:获取 google 好友列表
  5.  * 51aspx
  6.  * *                  */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Net;
  11. using System.Text;
  12. using Utilities.Web;
  13. namespace Fhz.Msn.OpenContactsNet
  14. {
  15.     /// <summary>
  16.     /// google 好友列表
  17.     /// </summary>
  18.     public class GmailExtract : IMailContactExtract
  19.     {
  20.         // google
  21.         private const string ContinueUrl = "http://mail.google.com/mail?ui=html&amp;zy=l";
  22.         private const string ExportUrl = "https://mail.google.com/mail/contacts/data/export?exportType=ALL&groupToExport=&out=GMAIL_CSV";
  23.         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";
  24.         private const string LoginUrl = "https://www.google.com/accounts/ServiceLoginAuth?service=mail";
  25.         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)";
  26.         #region IMailContactExtract Members
  27.         public bool Extract( NetworkCredential credential, out MailContactList list )
  28.         {
  29.             bool result = false;
  30.             list = new MailContactList();
  31.             try
  32.             {
  33.                 CookieCollection cookies = new CookieCollection();
  34.                 // 准备登录表单数据
  35.                 HttpValueCollection loginFormValues = new HttpValueCollection();
  36.                 loginFormValues[ "ltmpl" ] = "default";
  37.                 loginFormValues[ "ltmplcache" ] = "2";
  38.                 loginFormValues[ "continue" ] = ContinueUrl;
  39.                 loginFormValues[ "service" ] = "mail";
  40.                 loginFormValues[ "rm" ] = "false";
  41.                 loginFormValues[ "hl" ] = "en";
  42.                 loginFormValues[ "Email" ] = credential.UserName;
  43.                 loginFormValues[ "Passwd" ] = credential.Password;
  44.                 loginFormValues[ "PersistentCookie" ] = "true";
  45.                 loginFormValues[ "rmShown" ] = "1";
  46.                 loginFormValues[ "null" ] = "Sign In";
  47.                 // 转换为字节
  48.                 byte[] loginPostData = Encoding.UTF8.GetBytes( loginFormValues.ToString( true ) );
  49.                 HttpWebRequest loginRequest = ( HttpWebRequest ) WebRequest.Create( LoginUrl );
  50.                 loginRequest.Method = "POST";
  51.                 loginRequest.UserAgent = UserAgent;
  52.                 loginRequest.Referer = LoginRefererUrl;
  53.                 loginRequest.ContentType = "application/x-www-form-urlencoded";
  54.                 loginRequest.ContentLength = loginPostData.Length;
  55.                 loginRequest.AllowAutoRedirect = false;
  56.                 // 创建的Cookie集
  57.                 loginRequest.CookieContainer = new CookieContainer();
  58.                 // 添加请求 post 数据
  59.                 Stream stream;
  60.                 using ( stream = loginRequest.GetRequestStream() )
  61.                 {
  62.                     stream.Write( loginPostData, 0, loginPostData.Length );
  63.                 }
  64.                 HttpWebResponse loginResponse = ( HttpWebResponse ) loginRequest.GetResponse();
  65.                 cookies.Add( loginResponse.Cookies );
  66.                 // 创建请求输出 Google CSV 页
  67.                 HttpWebRequest contactsRequest = ( HttpWebRequest ) WebRequest.Create( ExportUrl );
  68.                 contactsRequest.Method = "GET";
  69.                 contactsRequest.UserAgent = UserAgent;
  70.                 contactsRequest.Referer = loginResponse.ResponseUri.ToString();
  71.                 // 使用Cookie得到从登录页
  72.                 contactsRequest.CookieContainer = new CookieContainer();
  73.                 foreach ( Cookie cookie in cookies )
  74.                 {
  75.                     contactsRequest.CookieContainer.Add( cookie );
  76.                 }
  77.                 HttpWebResponse exportResponse = ( HttpWebResponse ) contactsRequest.GetResponse();
  78.                 // 读取数据流的反应
  79.                 string csvData;
  80.                 using ( Stream responseStream = exportResponse.GetResponseStream() )
  81.                 {
  82.                     using ( StreamReader streamRead = new StreamReader( responseStream ) )
  83.                     {
  84.                         csvData = streamRead.ReadToEnd();
  85.                     }
  86.                 }
  87.                 // 剖析 google csv
  88.                 string[] lines = csvData.Split( 'n' );
  89.                 foreach ( string line in lines )
  90.                 {
  91.                     string[] values = line.Split( ',' );
  92.                     if ( values.Length < 2 )
  93.                     {
  94.                         continue;
  95.                     }
  96.                     MailContact mailContact = new MailContact();
  97.                     mailContact.Email = values[ 1 ];
  98.                     mailContact.Name = values[ 0 ];
  99.                     list.Add( mailContact );
  100.                 }
  101.                 result = true;
  102.             }
  103.             catch
  104.             {
  105.             }
  106.             return result;
  107.         }
  108.         #endregion
  109.     }
  110. }