5 Replies Latest reply: Jun 26, 2012 11:15 PM by Bill Shannon-Oracle RSS

    Problem in Retrieve email

    891682
      Hi,
      Actually I want to Retrieve email though java code but i m getting exception and plz check my code .

      javax.mail.NoSuchProviderException: No provider for POP
      at javax.mail.Session.getProvider(Session.java:460)
      at javax.mail.Session.getStore(Session.java:535)
      at javax.mail.Session.getStore(Session.java:515)
      at TestMail.EmailReceiveTest.receiveEmail(EmailReceiveTest.java:51)
      at TestMail.EmailReceiveTest.main(EmailReceiveTest.java:26)


      package TestMail;
      import java.io.IOException;
      import java.util.Properties;

      import javax.mail.Folder;
      import javax.mail.Message;
      import javax.mail.MessagingException;
      import javax.mail.NoSuchProviderException;
      import javax.mail.Session;

      import com.sun.mail.pop3.POP3Store;

      import javax.mail.PasswordAuthentication;

      public class EmailReceiveTest {

      public static void main(String[] args) {

      String mailPop3Host = "pop.gmail.com";
      String mailStoreType = "POP";
      String mailUser = "abc@gmail.com";
      String mailPassword = "xyzzzz";


      receiveEmail(mailPop3Host, mailStoreType, mailUser, mailPassword);
      }

      public static void receiveEmail(String pop3Host, String mailStoreType, String mailUser, String mailPassword) {

      try {
      Properties properties = new Properties();
      // sets POP3 properties

      properties.put("mail.pop3.host", pop3Host);
      properties.put("mail.pop3.port", "995");
      // sets POP3S properties
      properties.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
      properties.setProperty("mail.pop3.socketFactory.fallback", "false");
      properties.setProperty("mail.pop3.socketFactory.port", "995");
      properties.put("mail.pop3.ssl.trust", "*");
      //Session emailSession = Session.getDefaultInstance(properties);
      Session emailSession = Session.getDefaultInstance(properties,
      new javax.mail.Authenticator() {
      protected PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication("abc@gmail.com",
      "xyzzzz");
      }
      });

      POP3Store emailStore = (POP3Store) emailSession.getStore(mailStoreType);
      System.out.println("hello------------>"+emailStore);
      emailStore.connect(mailUser, mailPassword);

      Folder emailFolder = emailStore.getFolder("INBOX");
      emailFolder.open(Folder.READ_ONLY);

      Message[] messages = emailFolder.getMessages();
      for (int i = 0; i < messages.length; i++) {
      Message message = messages;
      System.out.println("==============================");
      System.out.println("Email #" + (i + 1));
      System.out.println("Subject: " + message.getSubject());
      System.out.println("From: " + message.getFrom()[0]);
      System.out.println("Text: " + message.getContent().toString());
      }

      emailFolder.close(false);
      emailStore.close();
      } catch (NoSuchProviderException e) {
      e.printStackTrace();
      } catch (MessagingException e) {
      e.printStackTrace();
      } catch (IOException e) {
      e.printStackTrace();
      }

      }

      }

      Thanks
      Anup
        • 1. Re: Problem in Retrieve email
          Bill Shannon-Oracle
          mailStoreType should be "pop3".

          Please see the JavaMail FAQ for some simpler example code.
          • 2. Re: Problem in Retrieve email
            891682
            Thanks for replying,
            Actually this error has been resolved but new error is coming.
            javax.mail.MessagingException: Connect failed;s
            nested exception is:
                 java.net.ConnectException: Connection timed out: connect
                 at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:176)
                 at javax.mail.Service.connect(Service.java:291)
                 at javax.mail.Service.connect(Service.java:172)
                 at javax.mail.Service.connect(Service.java:192)
                 at TestMail.EmailReceiveTest.receiveEmail(EmailReceiveTest.java:55)
                 at TestMail.EmailReceiveTest.main(EmailReceiveTest.java:27)
            Caused by: java.net.ConnectException: Connection timed out: connect
                 at java.net.PlainSocketImpl.socketConnect(Native Method)
                 at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
                 at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
                 at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
                 at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
                 at java.net.Socket.connect(Socket.java:519)
                 at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:550)
                 at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:141)
                 at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:284)
                 at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:201)
                 at com.sun.mail.pop3.Protocol.<init>(Protocol.java:98)
                 at com.sun.mail.pop3.POP3Store.getPort(POP3Store.java:227)
                 at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:172)
                 ... 5 more


            Thanks
            Anup
            • 3. Re: Problem in Retrieve email
              gimbal2
              Likely that is not a code problem but a networking problem. A firewall may be blocking the connection attempt for example.
              • 4. Re: Problem in Retrieve email
                891682
                Thanks,
                But my firewall has been Off.and new exception is comming.
                So can u tell me the Exact port number of gmaid id.Actually i m giving port number is 995.This is the right number of port number.

                Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
                     at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:285)
                     at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:191)
                     at sun.security.validator.Validator.validate(Validator.java:218)
                     at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:126)
                     at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:209)
                     at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:249)
                     at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1014)
                     ... 17 more
                Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
                     at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:174)
                     at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:238)
                     at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:280)

                Note-->I am finding so many days there is no any solution So plz help me and its urgent for me.
                If any example u have then plz give me the link or send me the source code in Running mode.

                Thanks
                Anup

                Edited by: 888679 on Jun 26, 2012 7:08 AM
                • 5. Re: Problem in Retrieve email
                  Bill Shannon-Oracle
                  The JavaMail FAQ has both example code for connecting to Gmail as well as tips for debugging your program when it doesn't work. Try that and if it's still not working post the results.