Click here to Skip to main content
Licence 
First Posted 16 Jun 2004
Views 59,474
Bookmarked 25 times

Connect the Gmail with IWebBrowser2

By | 16 Jun 2004 | Article
Use the IWebBrowser2 to connect Gmail

Introduction

When I got a Gmail account, I must do many complex steps to login the Gmail system.  I think if there is an interface like POP#, people will feel more free to use the GMail system.

I do not know of a GMail Client API, so I use the IE COM interface to manage IE to do what we do manually.

There are many articles about IE programming, but I cannot find one code to do a page that includes frames or IFrame.  In this article, I resolved this problem.

First, We can use get_frames to get the frame collection. If the page does not have the <body> tag, the frame collection will include all the framesets. Otherwise, the frame collection includes all the iframes. In the Gmail system login page,there is a iframe tag, and we can find it and get the form.

BOOL CGMailClass::lunchBrowser()
{
    HRESULT hr1 ;
    hr1 = CoInitialize(NULL);
    if(!SUCCEEDED(hr1))
        return FALSE;

    //Create an Instance of web browser
    hr1 = CoCreateInstance (CLSID_InternetExplorer, NULL, 
        CLSCTX_LOCAL_SERVER, 
        IID_IWebBrowser2, (LPVOID *)&m_pBrowser); 
    if(hr1==S_OK)
    {
        VARIANT_BOOL pBool=true; //The browser is invisible
        m_pBrowser->put_Visible( pBool ) ; 
        //Gmail login page
        COleVariant vaURL("https://gmail.google.com/gmail") ; 
        COleVariant null; 

        //Open the mail login page
        m_pBrowser->Navigate2(vaURL,null,null,null,null) ; 
        WaitForEnd();
    }
    return TRUE;
}
m_pBrowser is the member variable of this class, below is the declaration:
protected:
    IWebBrowser2* m_pBrowser;
The function below is to get the document from the IFrame, so that we can find the form element.
BOOL CGMailClass::getDoc(IHTMLDocument2** pIFrameDoc)
{
    IDispatch *spDispatch;
    CComQIPtr<IHTMLDOCUMENT2, &IID_IHTMLDocument2> pDoc2;
    if (m_pBrowser){
        spDispatch=NULL;
        if (SUCCEEDED(m_pBrowser->get_Document( &spDispatch)))
            pDoc2 = spDispatch;
        if(pDoc2!=NULL)
        {
            //because the Gmail login page hide in a Iframe
            //so we must find where is the IFrame object
            CComPtr<IHTMLFRAMESCOLLECTION2> pIFrameCol;
            if (SUCCEEDED(pDoc2->get_frames(&pIFrameCol))) 
            {
                //retreive all of the Frames
                long p=0;
                pIFrameCol->get_length(&p);
                if(p!=0)
                { //yes we find more than one Frames
                    for(long i=0;i<=(p-1);i++)
                    { 
                        VARIANT frameRequested;
                        VARIANT frameOut;
                        frameRequested.vt = VT_I4 ;
                        frameRequested.bstrVal = (BSTR)i;
                        pIFrameCol->item(&frameRequested, &frameOut);
                        IHTMLWindow2* pIFrameWindow;
                        frameOut.pdispVal->QueryInterface(IID_IHTMLWindow2, 
                                                      (void**)&pIFrameWindow);

                    pIFrameWindow->get_document(pIFrameDoc);
                        return TRUE;
                        //ParseDoc(pIFrameDoc);
                    }
                }
            }
        }
    }
    return FALSE;
}
The getForm function can get the Form element and you can use the form element to set the input value,visible and other properties.
BOOL CGMailClass::getForm(IHTMLDocument2 *pIFrameDoc,
                          IHTMLFormElement **pFormElement)
{
    IHTMLElementCollection* pElementCol=NULL;
    long p=0;
    if (SUCCEEDED(pIFrameDoc->get_forms(&pElementCol)))
    {

    if(SUCCEEDED(pElementCol->get_length(&p)))
            if(p!=0)
            { 
                for(long i=0;i<=(p-1);i++)
                {
                    VARIANT id, index;
                    IDispatch* spDispatch=NULL;
                    V_VT(&id) = VT_I4;
                    V_I4(&id) = i;
                    V_VT(&index) = VT_I4;
                    V_I4(&index) = 0;
                    spDispatch=NULL;
                    if(SUCCEEDED(pElementCol->item(id,index, &spDispatch)))
                    {
                        if(SUCCEEDED(spDispatch->QueryInterface(
                                 IID_IHTMLFormElement,(void**)pFormElement)))
                            return TRUE;
                    }
                }
            }
    }
    return FALSE;
}

In this demo, I just implented the login function, because the Gmail system uses too many and too complicated javascript. I cannot find a way to get the inbox property. If you had a good method,please let me know. You can reach me at mailto:ChineseDofu@gmail.com

I must say thanks to pratheesh because of his Yahoo Mail.

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

About the Author

Dofu



China China

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionsource code? Pinmemberyaron shani4:06 11 Oct '07  
GeneralSampleCode Required PinmemberTusharKavali7:05 12 Oct '06  
GeneralSource Code PinmemberAdeel6880:02 7 Aug '06  
Generalerror happens Pinmemberpearlite22:47 10 Aug '05  
Generalunable to connect to server PinsussAnonymous15:23 26 Mar '05  
GeneralSample code please... Pinsusssangsvar18:00 26 Nov '04  
Generalthanks Pinmemberobad82002@yahoo.com12:44 5 Oct '04  
Generalsign up gmail PinsussAnonymous1:20 13 Aug '04  
GeneralRe: sign up gmail PinmemberTaha Zayed18:27 25 Oct '04  
GeneralRe: sign up gmail Pinsusssanjay shiwakoti3:44 28 Nov '04  
GeneralRe: sign up gmail PinsussAnonymous8:42 1 May '05  
GeneralRe: sign up gmail PinsussAnonymous20:14 27 Jul '05  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 17 Jun 2004
Article Copyright 2004 by Dofu
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid