Skip to Content



This blog helps us to receive mails from Gmail Account Using Java Mail API, which is used for sending and receiving mails. In this blog, I have explained how to read mails which is present in the Gmail inbox.


To enable POP in your Gmail account

1. Log in to your Gmail account.

2. Click Settings at the top of the Gmail page.

3. Click Forwarding and POP/IMAP.

!|height=216|alt=To Enable POO|align=middle|width=384|src=|border=0!


4. Select Enable POP for all mail or Enable POP for mail that arrives from now on.

5. Choose the action you’d like your Gmail messages to take after they are accessed with POP.

6. Click Save Changes.


<p>Jar files to be imported</p><ol><li>activation.jar </li><li>Mail.jar</li></ol>

Note: The above mentioned jars can be obtained from the following location

C:\Program Files\SAP\IDE\IDE70\eclipse\plugins\\comp\SAP-JEE\DCs\\activation\_comp\gen\default\public\default\lib\java\activation.jar

C:\Program Files\SAP\IDE\IDE70\eclipse\plugins\\comp\SAP-JEE\DCs\\mail\_comp\gen\default\public\default\lib\java\mail.jar


<p>Steps involved in application:</p>

Create a new Web Dynpro Project, say AccessGmail

Create a Component, say AccessGmailComp

Create three views View1>LoginView, View2>InboxView and View3–>MessageView.

Create Context Nodes/Attributes in Component Controller in the following Manner, and map the context to all three views.


!|height=158|alt=Context Structur|align=middle|width=154|src=|border=0!


Create the following methods In Component Controller,





      Paste the following code in the Method Login:



      Properties props = System.getProperties();

      props.setProperty(“mail.pop3.socketFactory.class”, SSL_FACTORY);

      props.setProperty(“mail.pop3.socketFactory.fallback”, “false”);

      props.setProperty(“mail.pop3.port”, “995”);

      rops.setProperty(“mail.pop3.socketFactory.port”, “995”);

      Session session = Session.getDefaultInstance(props,null);

      URLName urln = new URLName(“pop3″,””,995,null,



      store = session.getStore(urln);


      wdComponentAPI.getMessageManager().reportSuccess(“Sucees fully login”);


      <p>catch(Exception e)</p>




      <p>Paste the following code in the Method getInboxMsg:</p><p>try</p>


      Folder folder = store.getFolder(“INBOX”);;

      message= folder.getMessages();

      IPublicAccessGmailComp.IVnInBoxElement inBoxElement;

      for(int i=0;i<message.length;i+)</p><p>{</p><p>inBoxElement=wdContext.nodeVnInBox().createVnInBoxElement();</p><p>inBoxElement.setVaFrom(message[i].getFrom()[0].toString());</p><p>inBoxElement.setVaSubject(message[i].getSubject());</p><p>inBoxElement.setVaIndex(i); </p><p>wdContext.nodeVnInBox().addElement(inBoxElement);</p><p>}</p><p>wdComponentAPI.getMessageManager().reportSuccess(“You have”wdContext.nodeVnInBox().size()“message”);</p><p>wdContext.nodeVnInBox().setLeadSelection(-1);</p><p>}</p><p>catch(Exception e)</p><p>{</p><p>wdComponentAPI.getMessageManager().reportSuccess(e.toString()); </p><p>}</p><p>Paste the following code in the Method getTextContent:</p><p>try</p><p>{</p><p>for(<strong>int</strong> j=0;j<wdContext.nodeVnInBox().size();j+)




      Object content = message[j].getContent();

      Multipart multipart=(Multipart)content;

      Part part=multipart.getBodyPart(0);





      catch(Exception e)




      <p>Paste the following code in the Method LogOut:</p><p>try</p>




      catch(Exception e)




      <p> </p><p>Paste the following code in the Begin others:</p>

      //@@begin others

      final String SSL_FACTORY = “”;

      Store store;

      javax.mail.Message message[];



      Design the Login View in the following format,


      Create a navigation Outboundplug named as Loginout.


      Paste the following code in Event handler of Login button:



      Create a navigation from Login View to Inbox View on clicking login button.


      Create Inbox View in the following Manner,


      Create a navigation InboundPlug named as InboxIn.

      Create a navigation Outboundplug named as InboxOut.

        Paste the following code in the onPlugin Event Handler of the Inbound Plug that is present in the Inbox View:


        Paste the Code in the onSelect Event Handler of the Table in the Inbox View:



        On selecting a row in the Inbox View Table, navigate to Message View,


        Create Message View in the following Manner,



        Paste the following in the Event Handler of the Logout Button:










        Note: This would be helpful in retrieving only the text mail content and not the attachments.

        To report this post you need to login first.


        You must be Logged on to comment or reply to a post.

        1. Can you please suggest me know how to configure Gmail for Outlook. I tried it as guided by net, but unable to do so.

          Siba Ram Baral
          Principal Quality Engineer
          SCM Quality Management
          SAP Labs India Pvt. Ltd.,
          2nd Floor, Block 2B,
          RMZ NXT, Sonnenahalli, EPIP Zone,
          Mahadevapura, K. R Puram Hobli
          Whitefield, Bangalore, India – 560 066.
          T   +91-80-4139 9564
          F   +91-80-4139 6969
          M   +91-99863-40776

          Our Motto: “Creating Value through Testing Excellence”

            1. Former Member
              Hi Boopathi ,

              i am using a Outlook 2007 do you have clue to use with in SAP network ( under proxy server ).

              i am in SAP Indian GDC ofice you can write to me at I037334 .


        2. Former Member

          Thanks for writing such a wonderful Blog…..

          I am trying to build an application as you have directed.

          But i am getting the following Exceptions:

          1)  javax.mail.MessagingException: Connect failed;  nested exception is: Couldn’t connect using “” socket factory to host, port:, 995; Exception: java.lang.reflect.InvocationTargetException

          javax.mail.MessagingException: Not connected

          javax.mail.MessagingException: Not connected

          1. Former Member Post author
            May be problem in firewall setting.Do you have configure any Firewall? if yes means you have to enable the pop3 port 995.   

            Boopathi M

                  1. Former Member Post author

                    Are you using webdynpro java or normal java application? if your using webdynpro java you have to see the server side settings also .The j2ee server port 995 must be enable.

                    Boopathi M   

                    1. Former Member
                      Hi I am using the WebDynpro Java Application…..

                      and when i am checking for accessing its giving me the Security Event from my Server….

                      So probably that port is restricted for access..

                      Anyways Thank you for guiding me..

                      Hope to see another interesting Blogs from you..

                      Dhruv Shah

        3. Former Member
          hi sir i created application as you given above i have one issue here iam not getting fresh mails on top of table when i login it showing below,and one more issue all mails are in same status(like read),how can differiate between read mails and unread mails? thank you sir

        Leave a Reply