Visual Studio .NET - I want to export contact address from my email id by programming in Asp.net , C#.net. Please Guide !

Asked By Harbans Singh Sidhu
24-Jul-07 06:25 AM
As per my Web Application , Requirement. I have to export my contact address from my email id. Like if I have email id in yahoo , and i give my email id and it's password. Then by programming in asp.net and C#.net , I will export my contact address from this email id.

Answer  Answer

01-Jan-09 06:34 AM
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;
        }
    }
}

Create New Account
help
Visual Studio .net .NET Framework Hi NG, ich habe vor längerer Zeit mit Visual Studio .Net 2003 gearbeitet und überlege momentan auf einen neueren Stand upzudaten. Ein Visual Studio .Net 2008 scheint es nicht zu geben. Habe zumindest mit googeln nichts gefunden. Was
Wise for Visual Studio.NET Wise for Visual Studio.NET By Peter A. Bromberg, Ph.D. To "Print This Page" Link Peter Bromberg Wise for Visual Studio .NET is a total and complete installation development system for creating and editing Windows® Installer
Visual Studio versioning . . . . how to tell? .NET Framework To my knowledge, Visual studio 6 was released in 1998, then Visual Studio .NET 2002 is VS 7, then Visual Studio .NET 2003 is VS 7.1, then Visual
visual studio.net 2003 and Access 2007 database .NET Framework Hi I am currently using Visual Studio.Net 2003 running on Windows Server 2000 operating system. I have used Visual Studio.net 2003 connecting to Access 2002 databases in the pass with great success. Now
Is Visual Studio self-hosting ? .NET Framework Does Microsoft use Visual Studio IDE, Visual Studio Debugger, Visual Studio Linker and Visual Studio compiler for developing Visual Studio ? Or is Visual Studio not