help
10 using System;
11 using System.Collections;
12 using System.Data;
13 using System.IO;
14 using System.Net;
15 using System.Net.Security;
16 using System.Security.Cryptography.X509Certificates;
17 using System.Text.RegularExpressions;
18
19
20 public class Gmail : Imail
21 {
22 public static bool MyCertValidation(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
23 {
24 return true;
25 }
26
27 public static void GetContacts(string strUserName, string strPassword, out bool boolIsOK, out DataTable dtContatct, out string strError)
28 {
29 //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(MyCertValidation);
30 //default values
31 dtContatct = new DataTable();
32 boolIsOK = true;
33 strError = string.Empty;
34
35 //WebProxy wp = new WebProxy("localhost", 8888);
36 //1=================================================================================================================
37 //http://www.gmail.com/ the AllowAutoRedirect will call subsequently...
38 //http://mail.google.com/mail/
39 //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
40 string strFirstPageRequestUrl = "http://gmail.com";
41 HttpWebRequest reqFirstPage = (HttpWebRequest)WebRequest.Create(strFirstPageRequestUrl);
42 reqFirstPage.Method = Utility.Get;
43 reqFirstPage.KeepAlive = true;
44 reqFirstPage.AllowAutoRedirect = true;
45 reqFirstPage.UseDefaultCredentials = true;
46 reqFirstPage.CookieContainer = new CookieContainer();
47 //reqFirstPage.Proxy = wp;
48 reqFirstPage.UserAgent = Utility.UserAgent;
49
50 HttpWebResponse resFirstPage = (HttpWebResponse)reqFirstPage.GetResponse();
51 CookieCollection ccFirstPage = new CookieCollection();
52 if (resFirstPage.Headers[Utility.SetCookie] != null)
53 {
54 ccFirstPage.Add(Utility.GetAllCookiesFromHeader(resFirstPage.Headers[Utility.SetCookie], resFirstPage.ResponseUri.Host));
55 }
56 StreamReader sr1 = new StreamReader(resFirstPage.GetResponseStream());
57 String strFirstPage = sr1.ReadToEnd();
58 sr1.Close();
59 string strLoginPageUrlReferer = resFirstPage.ResponseUri.ToString();
60 resFirstPage.Close();
61 reqFirstPage.Abort();
62 //the html tags are not closed perfectly..the SGMLReaderHelper convert to xhtml
63 //SGMLReaderHelper XHTMLConverterObj = new SGMLReaderHelper();
64 //string strXHTML = XHTMLConverterObj.ProcessString(strFirstPage);
65 //next url taken from action attribute of the FORM..
66 //string strLoginPostUrlTemp = Utility.GetRegExParsedValue("<form.*?id=\"gaia_loginform\"\\s*action=\"(?<RetVal>.*?)\"", strXHTML);
67 string strLoginPostUrlTemp = Utility.GetRegExParsedValue2(""gaia_loginform\".*?action=\"(?<RetVal>.*?)\".*?>", strFirstPage);
68 //collect FORM elements to post with next url..
69 ArrayList alName = new ArrayList();
70 ArrayList alValue = new ArrayList();
71 MatchCollection mc = Utility.GetRegExMatchCollection2("<\\s*input.*?>", strFirstPage);
72 foreach (Match m in mc)
73 {
74 string strName = Utility.GetRegExParsedValue2("<\\s*input\\s*.*?\\s*name=\"(?<RetVal>.*?)\"\\s*.*?>", m.Value);
75 string strValue = Utility.GetRegExParsedValue2("<\\s*input\\s*.*?\\s*value=\"(?<RetVal>.*?)\"\\s*.*?>", m.Value);
76 alName.Add(strName);
77 alValue.Add(strValue);
78 }
79
80
81 string strForm = string.Empty;
82 for (int i = 0; i < alName.Count; i++)
83 {
84 if (alName[i].ToString() == "PersistentCookie")
85 {
86 continue;
87 }
88 if (alName[i].ToString() == "continue")
89 {
90 strForm = strForm + alName[i].ToString() + "=" + Utility.GetRegExParsedValue("(?.*?\\?)", alValue[i].ToString()) + "&";
91 continue;
92 }
93
94 strForm = strForm + alName[i].ToString() + "=" + alValue[i].ToString() + "&";
95 }
96 //remove the last "&"
97 int n2 = strForm.Length;
98 strForm = strForm.Remove(n2 - 1);
99 //change the real username and password
100 string strFormUserIdName = "Email=";
101 string strFormPasswordName = "Passwd=";
102 if (strForm.IndexOf(strFormUserIdName) >= 0)
103 {
104 strForm = strForm.Replace(strFormUserIdName, strFormUserIdName + strUserName);
105 }
106 else
107 {
108 boolIsOK = false;
109 strError = Utility.ApplicationError;
110 return;
111 }
112 if (strForm.IndexOf(strFormPasswordName) >= 0)
113 {
114 strForm = strForm.Replace(strFormPasswordName, strFormPasswordName + strPassword);
115 }
116 else
117 {
118 boolIsOK = false;
119 strError = Utility.ApplicationError;
120 return;
121 }
122 //create cookies for post with next url
123 //this for broswer attributes ie/mac/mozilla..
124 Cookie cookTZ = new Cookie("TZ", "480", "/", ".google.com");
125 //create from javascript time (t1 - t2) difference
126 Cookie cookGMAIL_RTT = new Cookie("GMAIL_RTT", "324", "/", ".google.com");
127 //create from javascript date = "T" + (new Date()).getTime() + "/" + (new Date()).getTime() + "/" + (new Date()).getTime();
128 string strGMAIL_LOGINValue = "T" + Utility.GetJavaScriptTime() + "/" + Utility.GetJavaScriptTime();
129 strGMAIL_LOGINValue = strGMAIL_LOGINValue + "/" + Utility.GetJavaScriptTime();
130 Cookie cookGMAIL_LOGIN = new Cookie("GMAIL_LOGIN", strGMAIL_LOGINValue, "/", ".google.com");
131 ccFirstPage.Add(cookTZ);
132 ccFirstPage.Add(cookGMAIL_RTT);
133 ccFirstPage.Add(cookGMAIL_LOGIN);
134
135 if (strLoginPostUrlTemp == string.Empty)
136 {
137 boolIsOK = false;
138 strError = Utility.ApplicationError;
139 return;
140 }
141 //2=================================================================================================
142 //https://www.google.com/accounts/ServiceLoginAuth?service=mail
143 //https://www.google.com/accounts/CheckCookie?continue=http%3A%2F%2Fmail.google.com%2Fmail%2F%3F&service=mail&chtml=LoginDoneHtml
144 string strLoginUrl = strLoginPostUrlTemp;
145 string strLoginUrlPost = strForm;
146 HttpWebRequest reqLogin = (HttpWebRequest)WebRequest.Create(strLoginUrl);
147 reqLogin.KeepAlive = true;
148 reqLogin.Method = Utility.Post;
149 reqLogin.AllowAutoRedirect = true;
150 reqLogin.UseDefaultCredentials = true;
151 reqLogin.Referer = strLoginPageUrlReferer;
152 reqLogin.UserAgent = Utility.UserAgent;
153 reqLogin.ContentType = Utility.ContentTypeUrlEncoded;
154 //reqLogin.Proxy = wp;
155 reqLogin.CookieContainer = new CookieContainer();
156 reqLogin.CookieContainer.Add(ccFirstPage);
157 reqLogin.ContentLength = strLoginUrlPost.Length;
158 StreamWriter sw2 = new StreamWriter(reqLogin.GetRequestStream());
159 sw2.Write(strLoginUrlPost);
160 sw2.Close();
161
162 HttpWebResponse resLogin = (HttpWebResponse)reqLogin.GetResponse();
163 resLogin.Cookies = reqLogin.CookieContainer.GetCookies(reqLogin.RequestUri);
164 StreamReader sr2 = new StreamReader(resLogin.GetResponseStream());
165 String strAftLoginPage = sr2.ReadToEnd();
166 sr2.Close();
167 CookieCollection ccLogin = new CookieCollection();
168 ccLogin.Add(resLogin.Cookies);
169 //response url url
170 string strREsponseUrlTemp = resLogin.ResponseUri.ToString();
171 resLogin.Close();
172 reqLogin.Abort();
173
174 string strNextUrlTemp = Utility.GetRegExParsedValue("replace\\(\"(?<RetVal>.*?)\"\\)", strAftLoginPage);
175 // strNextUrlTemp = strNextUrlTemp.Replace("\\u003d","=");
176 if (strNextUrlTemp == string.Empty)
177 {
178 boolIsOK = false;
179 strError = Utility.UseridPassWrong;
180 return;
181 }
182 //3==============================================================================================
183 //http://mail.google.com/mail/?ui=1&view=fec
184 //gmail show different layouts for IE and FIREFOX..its taken from FIREFOX and easy too...
185 string strCSVRequest = @"http://mail.google.com/mail/contacts/data/export?exportType=ALL&groupToExport=&out=OUTLOOK_CSV";//GMAIL_CSV";
186 HttpWebRequest reqCSV = (HttpWebRequest)WebRequest.Create(strCSVRequest);
187 reqCSV.KeepAlive = true;
188 reqCSV.Method = Utility.Get;
189 reqCSV.AllowAutoRedirect = false;
190 reqCSV.UseDefaultCredentials = true;
191 reqCSV.Accept = Utility.AcceptAll;
192 //reqCSV.Proxy = wp;
193 reqCSV.UserAgent = Utility.UserAgent;
194 reqCSV.CookieContainer = new CookieContainer();
195 CookieCollection ccFresh = new CookieCollection();
196 ccFresh.Add(ccLogin);
197 ccFresh.Add(ccFirstPage);
198 CookieCollection ccFresh2 = Utility.GetDomainCorrectedCC(ccFresh,".google.com");
199 reqCSV.CookieContainer.Add(ccFresh2);
200 CookieCollection ccCSV = new CookieCollection();
201
202 HttpWebResponse resCSV = (HttpWebResponse)reqCSV.GetResponse();
203 StreamReader srCSV = new StreamReader(resCSV.GetResponseStream());
204 String strCSV = srCSV.ReadToEnd();
205 srCSV.Close();
206 resCSV.Close();
207 reqCSV.Abort();
208 //=====================================================
209 boolIsOK = true;
210 dtContatct = Utility.ConvertCSVIntoDataTable(strCSV);
211 strError = string.Empty;
212 }
213 }
214
215
216
cs
1
2 using System.Data;
3
4 public interface Imail
5 {
6 void GetContacts(string strUserName, string strPassword, out bool boolIsOK, out DataTable dtContatct, out string strError);
7 }
8
9
my desing
protected void Button1_Click(object sender, EventArgs e)
{
Gmail
.GetContacts("helloTR01@gmail.com","sasa",?,?,?
GetContacts(string strUserName, string strPassword, out bool boolIsOK, out DataTable dtContatct, out string strError)
}