Skip to Content

 

<p>Introduction:</p>

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.

Prerequisites:

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.

!https://weblogs.sdn.sap.com/weblogs/images/251808494/POPEnable.PNG|height=216|alt=To Enable POO|align=middle|width=384|src=https://weblogs.sdn.sap.com/weblogs/images/251808494/POPEnable.PNG|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\com.sap.tc.ap_2.0.0\comp\SAP-JEE\DCs\sap.com\activation\_comp\gen\default\public\default\lib\java\activation.jar

C:\Program Files\SAP\IDE\IDE70\eclipse\plugins\com.sap.tc.ap_2.0.0\comp\SAP-JEE\DCs\sap.com\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.

 

!https://weblogs.sdn.sap.com/weblogs/images/251808494/Context.PNG|height=158|alt=Context Structur|align=middle|width=154|src=https://weblogs.sdn.sap.com/weblogs/images/251808494/Context.PNG|border=0!

 

Create the following methods In Component Controller,

    Login()

    getInboxMsg()

    getTextContent()

    LogOut()

      Paste the following code in the Method Login:

      try

      {

      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″,”pop.gmail.com”,995,null,

      wdContext.nodeVnLogin().currentVnLoginElement().getVaUserName(),

      wdContext.nodeVnLogin().currentVnLoginElement().getVaPassword());

      store = session.getStore(urln);

      store.connect();

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

      }

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

      {

      wdComponentAPI.getMessageManager().reportSuccess(e.toString());

      }

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

      {

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

      folder.open(Folder.READ_ONLY);

      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+)

      {

      if(wdContext.nodeVnInBox().currentVnInBoxElement().getVaIndex()==j)

      {

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

      Multipart multipart=(Multipart)content;

      Part part=multipart.getBodyPart(0);

      wdContext.currentContextElement().setCaBodyText(part.getContent().toString());

      }

      }

      }

      catch(Exception e)

      {

      wdComponentAPI.getMessageManager().reportSuccess(e.toString());

      }

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

      {

      store.close();

      }

      catch(Exception e)

      {

      wdComponentAPI.getMessageManager().reportSuccess(e.toString());

      }

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

      //@@begin others

      final String SSL_FACTORY = “javax.net.ssl.SSLSocketFactory”;

      Store store;

      javax.mail.Message message[];

      //@@end

       

      Design the Login View in the following format,

      !https://weblogs.sdn.sap.com/weblogs/images/251808494/Loginview.PNG|height=99|alt=Loginpage|align=middle|width=266|src=https://weblogs.sdn.sap.com/weblogs/images/251808494/Loginview.PNG|border=0!

      Create a navigation Outboundplug named as Loginout.

       

      Paste the following code in Event handler of Login button:

      wdThis.wdGetAccessGmailCompController().Login();

      wdThis.wdFirePlugLoginOut();

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

       

      Create Inbox View in the following Manner,

      !https://weblogs.sdn.sap.com/weblogs/images/251808494/InboxView.PNG|height=188|alt=InboxView|align=middle|width=209|src=https://weblogs.sdn.sap.com/weblogs/images/251808494/InboxView.PNG|border=0

      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:

        wdThis.wdGetAccessGmailCompController().getInboxMsg();

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

        wdThis.wdGetAccessGmailCompController().getTextContent();

        wdThis.wdFirePlugInboxOut();

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

         

        Create Message View in the following Manner,

        !https://weblogs.sdn.sap.com/weblogs/images/251808494/MsgView.PNG|height=218|alt=MsgView|align=middle|width=308|src=https://weblogs.sdn.sap.com/weblogs/images/251808494/MsgView.PNG|border=0!

         

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

        wdThis.wdGetAccessGmailCompController().LogOut();

         

        OUTPUT:

        LoginViewOP

         

        InboxViewOP

         

        MsgViewOP

         

        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.

        14 Comments

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

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

          Regards
          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
          E   Siba.Ram.Baral@sap.com
          http://www.sap.com
          Our Motto: “Creating Value through Testing Excellence”

          (0) 
            1. Community User
              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 .

              Thanks
              Siva

              (0) 
        2. Dhruv Shah
          Hi,

          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: java.io.IOException: Couldn’t connect using “javax.net.ssl.SSLSocketFactory” socket factory to host, port: pop.gmail.com, 995; Exception: java.lang.reflect.InvocationTargetException

          javax.mail.MessagingException: Not connected

          2)
          javax.mail.MessagingException: Not connected

          (0) 
          1. Boopathi M 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.   

            Regards,
            Boopathi M

            (0) 
                  1. Boopathi M Post author
                    Hi,

                    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.

                    Regards,
                    Boopathi M   

                    (0) 
                    1. Dhruv Shah
                      Hi I am using the WebDynpro Java Application…..

                      and when i am checking for accessing pop.gmail.com:995 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..

                      Regards,
                      Dhruv Shah

                      (0) 
        3. mohi baban
          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
          (0) 

        Leave a Reply