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

Connect the Gmail with IWebBrowser2

0.00/5 (No votes)
16 Jun 2004 1  
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 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 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