Click here to Skip to main content
15,908,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using this code to Retrieve Contact of yahoo mail id if user has given UserId or password of yahoo mail, but this is not working

If you have any idea or suggestion about this code please forward me.
C#
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;  
        }  
    }  
}
Posted
Updated 30-Nov-12 1:22am
v2
Comments
bbirajdar 30-Nov-12 7:33am    
"but this is not working" is not a proper technical description of the problem . I cant help you unless you help me to understand your problem....
ZurdoDev 30-Nov-12 8:15am    
What's the error? Have you checked with yahoo's documentation?
sonu Ranjan 30-Nov-12 8:27am    
there are no any bugs.but MailContact class have set same properties Name or Email this properties have not contain a wright value..this class properties have display value Like this Email= pv$1 and Name Like a Name=sp$150827032.. so i can't understand which type properties value is set in this code...
ZurdoDev 30-Nov-12 8:28am    
I don't understand what you are saying, but I still recommend checking with yahoo.
sonu Ranjan 30-Nov-12 8:36am    
sir Please check my post code Line this..
if ( !email.Equals( "Email" ) && !name.Equals( "Nickname" ) )
{
MailContact mailContact = new MailContact();
mailContact.Name = name;
mailContact.Email = email;
list.Add( mailContact );
}
This code Have set properties Name or Email but this is not Given a any Email or Name...
thank you.

1 solution

This code goes around for some time...
The problem is that Yahoo changed contract and now before exporting contact to csv they ask guptcha - you have to enter manually word from the picture they provide.
So in your code you simulate user actions:
1. login into Yahoo,
2. go to contact section,
3. export contacts,
4. read exported contact list

Except now between steps 2 and 3 you have to type in some phrase, which cannot be automated.

You can check/debug your code if you add lines [to save web output and see it in browser]:
right after getting
C#
string thirdRes = Encoding.UTF8.GetString( thirdResponse );

System.IO.File.WriteAllText ("c:/y3.html", thirdRes);
System.Diagnostics.Process.Start("c:/y3.html");


Original idea, i think, was to automate user actions, - you can try to export yahoo contacts from your yahoo page and now you can see that user interaction is needed.
Yahoo provides some open api, but it requires temp session/token created by Yahoo, etc - see http://developer.yahoo.com/oauth/guide/oauth-auth-flow.html[^] , http://developer.yahoo.com/social/rest_api_guide/contacts-resource.html[^]. I wasted a lot of time on this, cannot make it work. There are some posts like http://gnillydev.blogspot.com/2007/10/yahoo-contact-import-class-in-c.html[^]
and others, but none code from them is working right now...
 
Share this answer
 
v2

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