GmailExtract.cs.svn-base
资源名称:GetFriend.rar [点击查看]
上传用户:asz878
上传日期:2020-04-01
资源大小:189k
文件大小:6k
源码类别:
WEB邮件程序
开发平台:
Visual C++
- //Code by Gnilly (http://gnillydev.blogspot.com)
- //Offical site: http://opencontacts.sourceforge.net
- //This program is free software: you can redistribute it and/or modify
- //it under the terms of the GNU Lesser General Public License as published by
- //the Free Software Foundation, either version 3 of the License, or
- //(at your option) any later version.
- //This program is distributed in the hope that it will be useful,
- //but WITHOUT ANY WARRANTY; without even the implied warranty of
- //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- //GNU Lesser General Public License for more details.
- //You should have received a copy of the GNU Lesser General Public License
- //along with this program. If not, see <http://www.gnu.org/licenses/>.
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Text;
- using Utilities.Web;
- namespace OpenContactsNet
- {
- 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<mpl=default<mplcache=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();
- // Read data from response stream
- string csvData;
- using ( Stream responseStream = exportResponse.GetResponseStream() )
- {
- using ( StreamReader streamRead = new StreamReader( responseStream ) )
- {
- csvData = streamRead.ReadToEnd();
- }
- }
- // parse google csv
- string[] lines = csvData.Split( 'n' );
- foreach ( string line in lines )
- {
- string[] values = line.Split( ',' );
- if ( values.Length < 2 )
- {
- continue;
- }
- MailContact mailContact = new MailContact();
- mailContact.Email = values[ 1 ];
- mailContact.Name = values[ 0 ];
- list.Add( mailContact );
- }
- result = true;
- }
- catch
- {
- }
- return result;
- }
- #endregion
- }
- }