Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using this code to import yahoo contacts in a web application..

but its not working..

any changes that can be made to this code??



using System;
using System.Collections.Specialized;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace Gnilly.Syndication.Mail
{
    public class YahooExtract
    {
        private const string _addressBookUrl = "http://address.yahoo.com/yab/us/Yahoo_ab.csv?loc=us&.rand=1671497644&A=H&Yahoo_ab.csv";
        private const string _authUrl = "https://login.yahoo.com/config/login?";
        private const string _loginPage = "https://login.yahoo.com/config/login";
        private const string _userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3";

        public bool Extract( NetworkCredential credential, out MailContactList list )
        {
            bool result = false;

            list = new MailContactList();

            try
            {
                WebClient webClient = new WebClient();
                webClient.Headers[ HttpRequestHeader.UserAgent ] = _userAgent;
                webClient.Encoding = Encoding.UTF8;

                byte[] firstResponse = webClient.DownloadData( _loginPage );
                string firstRes = Encoding.UTF8.GetString( firstResponse );


                NameValueCollection postToLogin = new NameValueCollection();
                Regex regex = new Regex( "type=\"hidden\" name=\"(.*?)\" value=\"(.*?)\"", RegexOptions.IgnoreCase );
                Match match = regex.Match( firstRes );
                while ( match.Success )
                {
                    if ( match.Groups[ 0 ].Value.Length > 0 )
                    {
                        postToLogin.Add( match.Groups[ 1 ].Value, match.Groups[ 2 ].Value );
                    }
                    match = regex.Match( firstRes, match.Index + match.Length );
                }


                postToLogin.Add( ".save", "Sign In" );
                postToLogin.Add( ".persistent", "y" );

                string login = credential.UserName.Split( '@' )[ 0 ];
                postToLogin.Add( "login", login );
                postToLogin.Add( "passwd", credential.Password );

                webClient.Headers[ HttpRequestHeader.UserAgent ] = _userAgent;
                webClient.Headers[ HttpRequestHeader.Referer ] = _loginPage;
                webClient.Encoding = Encoding.UTF8;
                webClient.Headers[ HttpRequestHeader.Cookie ] = webClient.ResponseHeaders[ HttpResponseHeader.SetCookie ];

                webClient.UploadValues( _authUrl, postToLogin );
                string cookie = webClient.ResponseHeaders[ HttpResponseHeader.SetCookie ];

                if ( string.IsNullOrEmpty( cookie ) )
                {
                    return false;
                }

                string newCookie = string.Empty;
                string[] tmp1 = cookie.Split( ',' );
                foreach ( string var in tmp1 )
                {
                    string[] tmp2 = var.Split( ';' );
                    newCookie = String.IsNullOrEmpty( newCookie ) ? tmp2[ 0 ] : newCookie + ";" + tmp2[ 0 ];
                }

                // set login cookie
                webClient.Headers[ HttpRequestHeader.Cookie ] = newCookie;
                byte[] thirdResponse = webClient.DownloadData( _addressBookUrl );
                string thirdRes = Encoding.UTF8.GetString( thirdResponse );

                string crumb = string.Empty;
                Regex regexCrumb = new Regex( "type=\"hidden\" name=\"\\.crumb\" id=\"crumb1\" value=\"(.*?)\"", RegexOptions.IgnoreCase );
                match = regexCrumb.Match( thirdRes );
                if ( match.Success && match.Groups[ 0 ].Value.Length > 0 )
                {
                    crumb = match.Groups[ 1 ].Value;
                }


                NameValueCollection postDataAB = new NameValueCollection();
                postDataAB.Add( ".crumb", crumb );
                postDataAB.Add( "vcp", "import_export" );
                postDataAB.Add( "submit[action_export_yahoo]", "Export Now" );

                webClient.Headers[ HttpRequestHeader.UserAgent ] = _userAgent;
                webClient.Headers[ HttpRequestHeader.Referer ] = _addressBookUrl;

                byte[] FourResponse = webClient.UploadValues( _addressBookUrl, postDataAB );
                string csvData = Encoding.UTF8.GetString( FourResponse );

                string[] lines = csvData.Split( '\n' );
                foreach ( string line in lines )
                {
                    string[] items = line.Split( ',' );
                    if ( items.Length < 5 )
                    {
                        continue;
                    }
                    string email = items[ 4 ];
                    string name = items[ 3 ];
                    if ( !string.IsNullOrEmpty( email ) && !string.IsNullOrEmpty( name ) )
                    {
                        email = email.Trim( '\"' );
                        name = name.Trim( '\"' );
                        if ( !email.Equals( "Email" ) && !name.Equals( "Nickname" ) )
                        {
                            MailContact mailContact = new MailContact();
                            mailContact.Name = name;
                            mailContact.Email = email;
                            list.Add( mailContact );
                        }
                    }
                }

                result = true;
            }
            catch
            {
            }
            return result;
        }
    }

    public class MailContact
    {
       private string _email = string.Empty;
       private string _name = string.Empty;

       public string Name
       {
          get { return _name; }
          set { _name = value; }
       }

       public string Email
       {
          get { return _email; }
          set { _email = value; }
       }

       public string FullEmail
       {
          get { return Email; }
       }
    }

    public class MailContactList : List<MailContact>
    {
    } 

}





Thanks and Ragards...
Posted

 
Share this answer
 
Comments
Arvinder.aneja 11-Jun-10 1:36am    
Thanks for the help brother..

I downloaded the source code but when i submit the email and password..

The output is..

(1) sans-serif;*font-size:small;*font:x-small;}




i.e (1) sans-serif;*font-size:small;*font:x-small;}
You have already asked this question. There is no point in repeating the exact same question again - in fact it annoys people so you are less likely to get a usefull answer.

Also, may I refer you to item 4 on the screen where you entered your questions, it says:-

4. Keep the question as concise as possible. If you have to include code, include the smallest snippet of code you can - do not dump your entire codebase.


It is a lot to expect busy people to go through all this code when all you say is it is not working - in what way is it 'not working'. If you cannot narrow the problem down to a smaller peice of code at least describe the problem in greater detail (preferaby do both).

If you can do that I am sure someone will be able to give you some help with this problem. :thumbsup:

Best of Luck :)
 
Share this answer
 
using System;
using System.Collections.Specialized;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace Gnilly.Syndication.Mail
{
public class YahooExtract
{
private const string _addressBookUrl = "http://address.yahoo.com/yab/us/Yahoo_ab.csv?loc=us&amp;.rand=1671497644&amp;A=H&amp;Yahoo_ab.csv";
private const string _authUrl = "https://login.yahoo.com/config/login?";
private const string _loginPage = "https://login.yahoo.com/config/login";
private const string _userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3";

public bool Extract( NetworkCredential credential, out MailContactList list )
{
bool result = false;

list = new MailContactList();

try
{
WebClient webClient = new WebClient();
webClient.Headers[ HttpRequestHeader.UserAgent ] = _userAgent;
webClient.Encoding = Encoding.UTF8;

byte[] firstResponse = webClient.DownloadData( _loginPage );
string firstRes = Encoding.UTF8.GetString( firstResponse );


NameValueCollection postToLogin = new NameValueCollection();
Regex regex = new Regex( "type=\"hidden\" name=\"(.*?)\" value=\"(.*?)\"", RegexOptions.IgnoreCase );
Match match = regex.Match( firstRes );
while ( match.Success )
{
if ( match.Groups[ 0 ].Value.Length &gt; 0 )
{
postToLogin.Add( match.Groups[ 1 ].Value, match.Groups[ 2 ].Value );
}
match = regex.Match( firstRes, match.Index + match.Length );
}


postToLogin.Add( ".save", "Sign In" );
postToLogin.Add( ".persistent", "y" );

string login = credential.UserName.Split( '@' )[ 0 ];
postToLogin.Add( "login", login );
postToLogin.Add( "passwd", credential.Password );

webClient.Headers[ HttpRequestHeader.UserAgent ] = _userAgent;
webClient.Headers[ HttpRequestHeader.Referer ] = _loginPage;
webClient.Encoding = Encoding.UTF8;
webClient.Headers[ HttpRequestHeader.Cookie ] = webClient.ResponseHeaders[ HttpResponseHeader.SetCookie ];

webClient.UploadValues( _authUrl, postToLogin );
string cookie = webClient.ResponseHeaders[ HttpResponseHeader.SetCookie ];

if ( string.IsNullOrEmpty( cookie ) )
{
return false;
}

string newCookie = string.Empty;
string[] tmp1 = cookie.Split( ',' );
foreach ( string var in tmp1 )
{
string[] tmp2 = var.Split( ';' );
newCookie = String.IsNullOrEmpty( newCookie ) ? tmp2[ 0 ] : newCookie + ";" + tmp2[ 0 ];
}

// set login cookie
webClient.Headers[ HttpRequestHeader.Cookie ] = newCookie;
byte[] thirdResponse = webClient.DownloadData( _addressBookUrl );
string thirdRes = Encoding.UTF8.GetString( thirdResponse );

string crumb = string.Empty;
Regex regexCrumb = new Regex( "type=\"hidden\" name=\"\\.crumb\" id=\"crumb1\" value=\"(.*?)\"", RegexOptions.IgnoreCase );
match = regexCrumb.Match( thirdRes );
if ( match.Success &amp;&amp; match.Groups[ 0 ].Value.Length &gt; 0 )
{
crumb = match.Groups[ 1 ].Value;
}


NameValueCollection postDataAB = new NameValueCollection();
postDataAB.Add( ".crumb", crumb );
postDataAB.Add( "vcp", "import_export" );
postDataAB.Add( "submit[action_export_yahoo]", "Export Now" );

webClient.Headers[ HttpRequestHeader.UserAgent ] = _userAgent;
webClient.Headers[ HttpRequestHeader.Referer ] = _addressBookUrl;

byte[] FourResponse = webClient.UploadValues( _addressBookUrl, postDataAB );
string csvData = Encoding.UTF8.GetString( FourResponse );

string[] lines = csvData.Split( '\n' );
foreach ( string line in lines )
{
string[] items = line.Split( ',' );
if ( items.Length &lt; 5 )
{
continue;
}
string email = items[ 4 ];
string name = items[ 3 ];
if ( !string.IsNullOrEmpty( email ) &amp;&amp; !string.IsNullOrEmpty( name ) )
{
email = email.Trim( '\"' );
name = name.Trim( '\"' );
if ( !email.Equals( "Email" ) &amp;&amp; !name.Equals( "Nickname" ) )
{
MailContact mailContact = new MailContact();
mailContact.Name = name;
mailContact.Email = email;
list.Add( mailContact );
}
}
}

result = true;
}
catch
{
}
return result;
}
}

public class MailContact
{
private string _email = string.Empty;
private string _name = string.Empty;

public string Name
{
get { return _name; }
set { _name = value; }
}

public string Email
{
get { return _email; }
set { _email = value; }
}

public string FullEmail
{
get { return Email; }
}
}

public class MailContactList : List&lt;MailContact&gt;
{
}

}ARUN PATIDAR SARVATMAN TECHNOLOGIES
 
Share this answer
 
v2
Comments
dnnclassified 29-Nov-10 7:57am    
hi,
is it working.for me its not working. i'm getting (1) sans-serif;*font-size:small;*font:x-small;}
only. Plz give me suggesstion.my mailid is dnnclassified@gmail.com
SureshNagalla 7-Apr-12 7:27am    
it shows the error as "the type or namespace List could not be found
sonu Ranjan 30-Nov-12 7:12am    
this code is not proper work..this code have not getting any contact of mail or user Name...if u have same Help code then please share me...thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900