Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Checking Yahoo! Account via HTTP

0.00/5 (No votes)
20 May 2003 1  
How to check Yahoo! mail account via HTTP
Download Source (10 kb)

    Introduction

    As we all know Yahoo has stopped POP3 access to its free email service. This article will explain you how to check HTTP based email account from a Visual Basic Application. This article is just a head-start to developing a framework for a full-fledged application which can be customized for similar HTTP based email accounts. Being it HTTP your program is guaranteed to work regardless of the type of internet connection you have.

     

    The Concept and method

    The concept is fairly simple. Something that we do all the time, but never think of achieving in the manner I am about to show. How do all of us check our Yahoo! Accounts? These are the steps we follow:

     

    1. Connect to the mail.yahoo.com website.
    2. Key in our login and password.
    3. Hit the �login� button.

     

    After which, the traditional Yahoo! Mailbox page shows up.

     

    Now let us try doing the same programmatically:

     

    1. Load the mail.yahoo.com website in one of our nested Internet Explorer Controls.
    2. Set the Login name and Password fields to our login/password combination.
    3. Invoke the �Submit� method of the form.

     

    Simple! Isn�t it? Now, after following the above steps, the browser automatically loads up the Yahoo! Mailbox page. All that we have to do now is parse the HTML page correctly and extract the required information.

     

    The YahooMailChecker Application

    Let us call our application the YahooMailChecker Application. This is going to be a standard Windows Application (EXE) Project in Microsoft Visual Basic 6.0.

     

    Our application will require the following references:

     

    1. Microsoft HTML Object library.
    2. Microsoft Internet Explorer.

     

    Our applications will host the following controls:

     

    1. Timer control
    2. Status bar control.

     

    We will require the timer control because we will be checking our Yahoo! Account at a periodic interval. And obviously the status bar will keep telling us what exactly the Internet Explorer is up to.

     

     

    Connecting to the Yahoo! Mail site

    As you must have guessed by now, the entire code will go inside the Timer controls, Timer event. I have set the interval of the timer control to 15 seconds, you will have to adjust this interval based on the speed of the internet connection you have.

     

    Here is how our application will work:

     

    1. Create an instance of the Internet Explorer object and set it to our global variable.
    2. Start work inside the Timer controls event.

     

    The Internet Explorer Object which we will use has an event called DocumentComplete, we will be using this event to get notified of the browser�s completion status. Inside this event, we will check if we have already being authenticated on the yahoo mail site or not. If yes, go ahead and extract the required information. If not, try logging in.

     

    In order to connect to the Yahoo! Mail site you will be using the Navigate method of the Internet Explorer object.

     

    Here�s the code snippet from the Timer control�s Timer event:

    Private Sub tmrWorkerThread_Timer()
        Dim FirstPart As String
        Dim OpenBracket As Integer
        Dim CloseBracket As Integer
        Dim Actual As String

        tmrWorkerThread.Enabled = False

        sbYMC.Panels(1).Text = "Navigating"

        YahooMail.navigate "http://mail.yahoo.com"

        Do While YahooMail.readyState <> READYSTATE_COMPLETE
            DoEvents
            sbYMC.Panels(1).Text = YahooMail.StatusText
        Loop

        If InStr(YahooMail.document.body.innerHTML, "You must sign in to read or send mail") <> 0 Or InStr(YahooMail.document.body.innerHTML, "Invalid Password") <> 0 Then
            With YahooMail.document
                .All.Item("login").Value = "myloginid"
                .All.Item("passwd").Value = "mypassword"
                .Forms(0).submit
            End With

            Do While YahooMail.readyState <> READYSTATE_COMPLETE
                DoEvents
                sbYMC.Panels(1).Text = YahooMail.StatusText
            Loop
        Else
            lblMailBoxState = "Authenticated"
        End If

        tmrWorkerThread.Enabled = True
    End Sub
     
    Let us try and understand the above code now. The above code works in the following manner:

     

    1. Navigate to the mail.yahoo.com site by calling the Navigate method of the Internet Explorer object. I have called my Internet Explorer �YahooMail�.
    2. Wait till the page loading is complete. This is achieved by waiting for the Browser�s completion status (READYSTATE_COMPLETE).
    3. Once the page is loaded check the HTML content to see if you have already logged in using some different session i.d. (yahoo maintains this in the form of cookies on the client�s machine). This check is made by checking for the �You must sign in to read or send mail� text inside the HTML content (InnerHTML). If this text is not found that means the browser is using an existing cookie (which had worked in the past).

    That�s it!

     

    The DocumentComplete event

    Now that the page has loaded in our browser object, we need to extract the relevant information. This is done inside the DocumentComplete event of the Internet Explorer object.

     

    Here is how the code inside this event looks like:

     

    Private Sub YahooMail_DocumentComplete(ByVal pDisp As Object, URL As Variant)

        If InStr(YahooMail.document.body.innerHTML, ">Inbox") > 0 Then

            FirstPart = Mid(YahooMail.document.body.innerHTML, InStr(YahooMail.document.body.innerHTML, ">Inbox"), 20)

           

            OpenBracket = InStr(FirstPart, "(")

            CloseBracket = InStr(FirstPart, ")")

           

            If OpenBracket = 0 Or CloseBracket = 0 Then

                lblInboxCount = "No Unread Mails"

            Else

                Actual = Mid(FirstPart, OpenBracket + 1, CloseBracket - OpenBracket - 1)

               

                lblInboxCount = Actual

            End If

       

            FirstPart = Mid(YahooMail.document.body.innerHTML, InStr(YahooMail.document.body.innerHTML, ">Bulk"), 20)

           

            OpenBracket = InStr(FirstPart, "(")

            CloseBracket = InStr(FirstPart, ")")

           

            If OpenBracket = 0 Or CloseBracket = 0 Then

                lblInboxCount = "No Unread Bulk Mails"

            Else

                Actual = Mid(FirstPart, OpenBracket + 1, CloseBracket - OpenBracket - 1)

           

                lblBulkCount = Actual

            End If

        End If

    End Sub

     

    Let us now understand how this code works:

     

    1. If the target document contains the following text : �>Inbox� it means that you have logged into the account.
    2. Go ahead and hunt for the location of the above text.
    3. Extract the Unread mail count and then repeat the steps for the Bulk Mail folder.

    Simple wasn�t it? That�s how simple it is! We just think all these things are complicated and miss out the real fun.

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here