Click here to Skip to main content
15,885,990 members
Articles / Programming Languages / VBScript
Article

Checking Yahoo! Account via HTTP

Rate me:
Please Sign up or sign in to vote.
1.61/5 (21 votes)
20 May 20035 min read 107.5K   2.3K   39   10
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.

    <o:p> 

    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:

    <o:p> 

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

    <o:p> 

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

    <o:p> 

    Now let us try doing the same programmatically:

    <o:p> 

    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.

    <o:p> 

    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.

    <o:p> 

    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.

    <o:p> 

    Our application will require the following references:

    <o:p> 

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

    <o:p> 

    Our applications will host the following controls:

    <o:p> 

    1. Timer control
    2. Status bar control.

    <o:p> 

    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.

    <o:p> 

    <o:p> 

    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.

    <o:p> 

    Here is how our application will work:

    <o:p> 

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

    <o:p> 

    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.

    <o:p> 

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

    <o:p> 

    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
        <st1:place>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
            <st1:place>Loop
        Else
            lblMailBoxState = "Authenticated"
        End If

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

    <o:p> 

    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!

    <o:p> 

    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.

    <o:p> 

    Here is how the code inside this event looks like:

    <o:p> 

    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

    <o:p> 

    Let us now understand how this code works:

    <o:p> 

    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


    Written By
    Japan Japan
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    GeneralMy vote of 1 Pin
    charles henington27-May-10 12:49
    charles henington27-May-10 12:49 
    GeneralRead a mail(Inbox) Pin
    AlexDa30-Mar-06 20:52
    AlexDa30-Mar-06 20:52 
    GeneralUpdate Pin
    Member 197177718-May-05 13:45
    Member 197177718-May-05 13:45 
    GeneralRe: Update Pin
    TaLamAk11-Sep-05 16:32
    TaLamAk11-Sep-05 16:32 
    Generalhello Pin
    scottbloodworth9-Jan-04 5:24
    scottbloodworth9-Jan-04 5:24 
    GeneralYahooPOPs Pin
    ijprest21-May-03 15:20
    ijprest21-May-03 15:20 
    GeneralRe: YahooPOPs Pin
    k_chandra_shekar15-Nov-03 1:35
    k_chandra_shekar15-Nov-03 1:35 
    GeneralRe: Ask this guy! Pin
    Anonymous22-May-03 10:51
    Anonymous22-May-03 10:51 
    GeneralC/C++ - MFC way Pin
    Peter Molnar27-May-03 7:39
    Peter Molnar27-May-03 7:39 
    QuestionHow is this a C++ program? Pin
    JonnoB21-May-03 8:27
    JonnoB21-May-03 8:27 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.