Dear All,
Good Day..
I am getting Exception of type 'System.OutOfMemoryException' was
thrown. while fetching Gmail Contact Address Book.
I have used below code for Gmail access. Please help me!!!
using System;
using System.Collections;
using System.Data;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text.RegularExpressions;
namespace ideaBubbling.ContactsReader
{
public class Gmail_New : Imail
{
public static bool MyCertValidation(object sender,
X509Certificate certificate, X509Chain chain, SslPolicyErrors
sslPolicyErrors)
{
return true;
}
public void GetContacts(string strUserName, string
strPassword, out bool boolIsOK, out DataTable dtContatct, out string
strError)
{
//ServicePointManager.ServerCertificateValidationCallback
= new RemoteCertificateValidationCallback(MyCertValidation);
//default values
dtContatct = new DataTable();
boolIsOK = true;
strError = string.Empty;
//WebProxy wp = new WebProxy("localhost", 8888);
//
1=================================================================================================================
//
http://www.gmail.com/ the AllowAutoRedirect will call
subsequently...
//
http://mail.google.com/mail/
//
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
string strFirstPageRequestUrl = "
http://gmail.com";
HttpWebRequest reqFirstPage = (HttpWebRequest)
WebRequest.Create(strFirstPageRequestUrl);
reqFirstPage.Method = Utility.Get;
reqFirstPage.KeepAlive = true;
reqFirstPage.AllowAutoRedirect = true;
reqFirstPage.UseDefaultCredentials = true;
reqFirstPage.CookieContainer = new CookieContainer();
//reqFirstPage.Proxy = wp;
reqFirstPage.UserAgent = Utility.UserAgent;
HttpWebResponse resFirstPage = (HttpWebResponse)
reqFirstPage.GetResponse();
CookieCollection ccFirstPage = new CookieCollection();
if (resFirstPage.Headers[Utility.SetCookie] != null)
{
ccFirstPage.Add(Utility.GetAllCookiesFromHeader
(resFirstPage.Headers[Utility.SetCookie],
resFirstPage.ResponseUri.Host));
}
StreamReader sr1 = new StreamReader
(resFirstPage.GetResponseStream());
String strFirstPage = sr1.ReadToEnd();
sr1.Close();
string strLoginPageUrlReferer =
resFirstPage.ResponseUri.ToString();
resFirstPage.Close();
reqFirstPage.Abort();
//the html tags are not closed perfectly..the
SGMLReaderHelper convert to xhtml
//SGMLReaderHelper XHTMLConverterObj = new SGMLReaderHelper
();
//string strXHTML = XHTMLConverterObj.ProcessString
(strFirstPage);
//next url taken from action attribute of the FORM..
//string strLoginPostUrlTemp = Utility.GetRegExParsedValue
("<form.*?id=\"gaia_loginform\"\\s*action=\"(?<RetVal>.*?)\"",
strXHTML);
string strLoginPostUrlTemp = Utility.GetRegExParsedValue2
("<form.*?id=\"gaia_loginform\".*?action=\"(?<RetVal>.*?)\".*?>",
strFirstPage);
//collect FORM elements to post with next url..
ArrayList alName = new ArrayList();
ArrayList alValue = new ArrayList();
MatchCollection mc = Utility.GetRegExMatchCollection2("<\
\s*input.*?>", strFirstPage);
foreach (Match m in mc)
{
string strName = Utility.GetRegExParsedValue2("<\
\s*input\\s*.*?\\s*name=\"(?<RetVal>.*?)\"\\s*.*?>", m.Value);
string strValue = Utility.GetRegExParsedValue2("<\
\s*input\\s*.*?\\s*value=\"(?<RetVal>.*?)\"\\s*.*?>", m.Value);
alName.Add(strName);
alValue.Add(strValue);
}
string strForm = string.Empty;
for (int i = 0; i < alName.Count; i++)
{
if (alName[i].ToString() == "PersistentCookie")
{
continue;
}
if (alName[i].ToString() == "continue")
{
strForm = strForm + alName[i].ToString() + "=" +
Utility.GetRegExParsedValue("(?<RetVal>.*?\\?)", alValue[i].ToString
()) + "&";
continue;
}
strForm = strForm + alName[i].ToString() + "=" +
alValue[i].ToString() + "&";
}
//remove the last "&"
int n2 = strForm.Length;
strForm = strForm.Remove(n2 - 1);
//change the real username and password
string strFormUserIdName = "Email=";
string strFormPasswordName = "Passwd=";
if (strForm.IndexOf(strFormUserIdName) >= 0)
{
strForm = strForm.Replace(strFormUserIdName,
strFormUserIdName + strUserName);
}
else
{
boolIsOK = false;
strError = Utility.ApplicationError;
return;
}
if (strForm.IndexOf(strFormPasswordName) >= 0)
{
strForm = strForm.Replace(strFormPasswordName,
strFormPasswordName + strPassword);
}
else
{
boolIsOK = false;
strError = Utility.ApplicationError;
return;
}
//create cookies for post with next url
//this for broswer attributes ie/mac/mozilla..
Cookie cookTZ = new Cookie("TZ", "480", "/",
".
google.com");
//create from javascript time (t1 - t2)
difference
Cookie cookGMAIL_RTT = new Cookie("GMAIL_RTT", "324", "/",
".
google.com");
//create from javascript date = "T" + (new Date()).getTime
() + "/" + (new Date()).getTime() + "/" + (new Date()).getTime();
string strGMAIL_LOGINValue = "T" +
Utility.GetJavaScriptTime() + "/" + Utility.GetJavaScriptTime();
strGMAIL_LOGINValue = strGMAIL_LOGINValue + "/" +
Utility.GetJavaScriptTime();
Cookie cookGMAIL_LOGIN = new Cookie("GMAIL_LOGIN",
strGMAIL_LOGINValue, "/", ".
google.com");
ccFirstPage.Add(cookTZ);
ccFirstPage.Add(cookGMAIL_RTT);
ccFirstPage.Add(cookGMAIL_LOGIN);
if (strLoginPostUrlTemp == string.Empty)
{
boolIsOK = false;
strError = Utility.ApplicationError;
return;
}
//
2=================================================================================================
//
https://www.google.com/accounts/ServiceLoginAuth?
service=mail
//
https://www.google.com/accounts/CheckCookie?continue=http
%3A%2F%
2Fmail.google.com%2Fmail%2F%3F&service=mail&chtml=LoginDoneHtml
string strLoginUrl = strLoginPostUrlTemp;
string strLoginUrlPost = strForm;
HttpWebRequest reqLogin = (HttpWebRequest)WebRequest.Create
(strLoginUrl);
reqLogin.KeepAlive = true;
reqLogin.Method = Utility.Post;
reqLogin.AllowAutoRedirect = true;
reqLogin.UseDefaultCredentials = true;
reqLogin.Referer = strLoginPageUrlReferer;
reqLogin.UserAgent = Utility.UserAgent;
reqLogin.ContentType = Utility.ContentTypeUrlEncoded;
//reqLogin.Proxy = wp;
reqLogin.CookieContainer = new CookieContainer();
reqLogin.CookieContainer.Add(ccFirstPage);
reqLogin.ContentLength = strLoginUrlPost.Length;
StreamWriter sw2 = new StreamWriter
(reqLogin.GetRequestStream());
sw2.Write(strLoginUrlPost);
sw2.Close();
HttpWebResponse resLogin = (HttpWebResponse)
reqLogin.GetResponse();
resLogin.Cookies = reqLogin.CookieContainer.GetCookies
(reqLogin.RequestUri);
StreamReader sr2 = new StreamReader
(resLogin.GetResponseStream());
String strAftLoginPage = sr2.ReadToEnd();
sr2.Close();
CookieCollection ccLogin = new CookieCollection();
ccLogin.Add(resLogin.Cookies);
//response url url
string strREsponseUrlTemp = resLogin.ResponseUri.ToString
();
resLogin.Close();
reqLogin.Abort();
string strNextUrlTemp = Utility.GetRegExParsedValue
("replace\\(\"(?<RetVal>.*?)\"\\)", strAftLoginPage);
// strNextUrlTemp = strNextUrlTemp.Replace("\\u003d","=");
if (strNextUrlTemp == string.Empty)
{
boolIsOK = false;
strError = Utility.UseridPassWrong;
return;
}
//
3==============================================================================================
//
http://mail.google.com/mail/?ui=1&view=fec
//gmail show different layouts for IE and FIREFOX..its
taken from FIREFOX and easy too...
string strCSVRequest = @"
http://mail.google.com/mail/
contacts/data/export?exportType=ALL&groupToExport=&out=OUTLOOK_CSV";//
GMAIL_CSV";
HttpWebRequest reqCSV = (HttpWebRequest)WebRequest.Create
(strCSVRequest);
reqCSV.KeepAlive = true;
reqCSV.Method = Utility.Get;
reqCSV.AllowAutoRedirect = false;
reqCSV.UseDefaultCredentials = true;
reqCSV.Accept = Utility.AcceptAll;
//reqCSV.Proxy = wp;
reqCSV.UserAgent = Utility.UserAgent;
reqCSV.CookieContainer = new CookieContainer();
CookieCollection ccFresh = new CookieCollection();
ccFresh.Add(ccLogin);
ccFresh.Add(ccFirstPage);
CookieCollection ccFresh2 = Utility.GetDomainCorrectedCC
(ccFresh,".
google.com");
reqCSV.CookieContainer.Add(ccFresh2);
CookieCollection ccCSV = new CookieCollection();
HttpWebResponse resCSV = (HttpWebResponse)
reqCSV.GetResponse();
StreamReader srCSV = new StreamReader
(resCSV.GetResponseStream());
String strCSV = srCSV.ReadToEnd();
srCSV.Close();
resCSV.Close();
reqCSV.Abort();
//=====================================================
boolIsOK = true;
dtContatct = Utility.ConvertCSVIntoDataTable(strCSV);
strError = string.Empty;
}
}
}