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

WEB邮件程序

开发平台:

Visual C++

  1. //Code by Gnilly (http://gnillydev.blogspot.com)
  2. //Offical site: http://opencontacts.sourceforge.net
  3. //This program is free software: you can redistribute it and/or modify
  4. //it under the terms of the GNU Lesser General Public License as published by
  5. //the Free Software Foundation, either version 3 of the License, or
  6. //(at your option) any later version.
  7. //This program is distributed in the hope that it will be useful,
  8. //but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10. //GNU Lesser General Public License for more details.
  11. //You should have received a copy of the GNU Lesser General Public License
  12. //along with this program.  If not, see <http://www.gnu.org/licenses/>.
  13. using System;
  14. using System.Collections.Generic;
  15. using System.IO;
  16. using System.Net;
  17. using System.Text;
  18. using Utilities.Web;
  19. namespace OpenContactsNet
  20. {
  21.     public class GmailExtract : IMailContactExtract
  22.     {
  23.         private const string ContinueUrl = "http://mail.google.com/mail?ui=html&amp;zy=l";
  24.         private const string ExportUrl = "https://mail.google.com/mail/contacts/data/export?exportType=ALL&groupToExport=&out=GMAIL_CSV";
  25.         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";
  26.         private const string LoginUrl = "https://www.google.com/accounts/ServiceLoginAuth?service=mail";
  27.         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)";
  28.         #region IMailContactExtract Members
  29.         public bool Extract( NetworkCredential credential, out MailContactList list )
  30.         {
  31.             bool result = false;
  32.             list = new MailContactList();
  33.             try
  34.             {
  35.                 CookieCollection cookies = new CookieCollection();
  36.                 // Prepare login form data
  37.                 HttpValueCollection loginFormValues = new HttpValueCollection();
  38.                 loginFormValues[ "ltmpl" ] = "default";
  39.                 loginFormValues[ "ltmplcache" ] = "2";
  40.                 loginFormValues[ "continue" ] = ContinueUrl;
  41.                 loginFormValues[ "service" ] = "mail";
  42.                 loginFormValues[ "rm" ] = "false";
  43.                 loginFormValues[ "hl" ] = "en";
  44.                 loginFormValues[ "Email" ] = credential.UserName;
  45.                 loginFormValues[ "Passwd" ] = credential.Password;
  46.                 loginFormValues[ "PersistentCookie" ] = "true";
  47.                 loginFormValues[ "rmShown" ] = "1";
  48.                 loginFormValues[ "null" ] = "Sign In";
  49.                 // Convert to bytes
  50.                 byte[] loginPostData = Encoding.UTF8.GetBytes( loginFormValues.ToString( true ) );
  51.                 HttpWebRequest loginRequest = ( HttpWebRequest ) WebRequest.Create( LoginUrl );
  52.                 loginRequest.Method = "POST";
  53.                 loginRequest.UserAgent = UserAgent;
  54.                 loginRequest.Referer = LoginRefererUrl;
  55.                 loginRequest.ContentType = "application/x-www-form-urlencoded";
  56.                 loginRequest.ContentLength = loginPostData.Length;
  57.                 loginRequest.AllowAutoRedirect = false;
  58.                 // Create cookie container
  59.                 loginRequest.CookieContainer = new CookieContainer();
  60.                 // Add post data to request
  61.                 Stream stream;
  62.                 using ( stream = loginRequest.GetRequestStream() )
  63.                 {
  64.                     stream.Write( loginPostData, 0, loginPostData.Length );
  65.                 }
  66.                 HttpWebResponse loginResponse = ( HttpWebResponse ) loginRequest.GetResponse();
  67.                 cookies.Add( loginResponse.Cookies );
  68.                 // Create request to export Google CSV page
  69.                 HttpWebRequest contactsRequest = ( HttpWebRequest ) WebRequest.Create( ExportUrl );
  70.                 contactsRequest.Method = "GET";
  71.                 contactsRequest.UserAgent = UserAgent;
  72.                 contactsRequest.Referer = loginResponse.ResponseUri.ToString();
  73.                 // use cookie gotten from login page
  74.                 contactsRequest.CookieContainer = new CookieContainer();
  75.                 foreach ( Cookie cookie in cookies )
  76.                 {
  77.                     contactsRequest.CookieContainer.Add( cookie );
  78.                 }
  79.                 HttpWebResponse exportResponse = ( HttpWebResponse ) contactsRequest.GetResponse();
  80.                 // Read data from response stream
  81.                 string csvData;
  82.                 using ( Stream responseStream = exportResponse.GetResponseStream() )
  83.                 {
  84.                     using ( StreamReader streamRead = new StreamReader( responseStream ) )
  85.                     {
  86.                         csvData = streamRead.ReadToEnd();
  87.                     }
  88.                 }
  89.                 // parse google csv
  90.                 string[] lines = csvData.Split( 'n' );
  91.                 foreach ( string line in lines )
  92.                 {
  93.                     string[] values = line.Split( ',' );
  94.                     if ( values.Length < 2 )
  95.                     {
  96.                         continue;
  97.                     }
  98.                     MailContact mailContact = new MailContact();
  99.                     mailContact.Email = values[ 1 ];
  100.                     mailContact.Name = values[ 0 ];
  101.                     list.Add( mailContact );
  102.                 }
  103.                 result = true;
  104.             }
  105.             catch
  106.             {
  107.             }
  108.             return result;
  109.         }
  110.         #endregion
  111.     }
  112. }