0

My Mail receive programme in java i use mail.1.4.jar and activation jar in my java programme

There are 5 steps i followed to receive the email using JavaMail API. They are:
Get the session object
create the POP3 store object and connect with the pop server
create the folder object and open it
retrieve the messages from the folder in an array and print it
close the store and folder objects

here is my code :

public class receive_Email {

    public static void main(String[] args) {
        String host="pop.gmail.com";
        String mailStorType="pop3";
        String Username="***********@gmail.com";
        String Password="*******";

        receiveMail(host, mailStorType, Username, Password);
    }

    // method for Receive email.....!
    public static void receiveMail(String pop3Host,String sotreType,String user,String password){

        ///   1) get session object

        Properties props=new Properties();
            props.put("mail.pop3.host", pop3Host );
        Session sessEmail=Session.getDefaultInstance(props);

        // 2) create pop3 store object and connect with pop server
        try {
            POP3Store emailStore=(POP3Store)sessEmail.getStore(sotreType);
            emailStore.connect(user,password);

        // 3) create Folder object and open it
            Folder emailFolder=emailStore.getFolder("INBOX");
            emailFolder.open(Folder.READ_ONLY);

        //  4) Retrieve the messages in the folder and display it   
            Message[] messages=emailFolder.getMessages();
            int i=0;
            for(Message m : messages){
                System.out.println("------------------------------------------------");
                System.out.println("Email Number : "+m.getMessageNumber());
                System.out.println("Subject : "+m.getSubject());
                System.out.println("From : "+m.getFrom());
                try {
                    System.out.println("Subject : "+m.getContent().toString());
                } catch (IOException e) {
                    System.out.println("No messages are available.............!");
                        e.printStackTrace();
                }

            } // end for loop 

            // 5) Close the Folder and email store
            emailFolder.close(false);
            emailStore.close();

        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }

    }
}

Exception I got is :

javax.mail.MessagingException: Connect failed;
  nested exception is:
    java.net.ConnectException: Connection timed out: connect
    at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:148)
    at javax.mail.Service.connect(Service.java:275)
    at javax.mail.Service.connect(Service.java:156)
    at javax.mail.Service.connect(Service.java:176)
    at com.mwh.DBConnect.receive_Email.receiveMail(receive_Email.java:37)
    at com.mwh.DBConnect.receive_Email.main(receive_Email.java:22)
    Caused by: java.net.ConnectException: Connection timed out: connect
.....
..

any suggestions to get rid of this exception... thanks in advance....

akcHitman
  • 117
  • 3
  • 16
  • 1
    Did you enable pop in your mail settings? Checkout this [link](https://support.google.com/mail/troubleshooter/1668960?hl=en#ts=1665119) – Keerthivasan Mar 07 '14 at 09:52
  • 1
    Use `Session.getInstance()` instead of `Session.getDefaultInstance()` http://www.oracle.com/technetwork/java/faq-135477.html#getdefaultinstance – user432 Mar 07 '14 at 09:57
  • Do we need to own a **PoP3** email account for this....? – akcHitman Mar 07 '14 at 15:15

2 Answers2

3

Im quite certain that GMail only accepts SSL Connections. Try following Code:

import com.sun.mail.pop3.POP3Store;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import java.io.IOException;
import java.util.Properties;

public class receive_Email {

  public static void main(String[] args) {
    String host="pop.gmail.com";
    int port=995;
    String mailStorType="pop3";
    String Username="***********@gmail.com";
    String Password="*******";

    receiveMail(host, port, mailStorType, Username, Password);
  }

  // method for Receive email.....!
  public static void receiveMail(String pop3Host, int port, String sotreType,String user,String password){

    ///   1) get session object

    Properties props = new Properties();
    props.put("mail.pop3.ssl.enable", "true"); // Use SSL
    Session sessEmail = Session.getInstance(props);

    // 2) create pop3 store object and connect with pop server
    try {
      POP3Store emailStore = (POP3Store) sessEmail.getStore(sotreType);
      emailStore.connect(pop3Host, port, user, password);

      // 3) create Folder object and open it
      Folder emailFolder=emailStore.getFolder("INBOX");
      emailFolder.open(Folder.READ_ONLY);

      //  4) Retrieve the messages in the folder and display it   
      Message[] messages=emailFolder.getMessages();

      for(Message m : messages){
        System.out.println("------------------------------------------------");
        System.out.println("Email Number : "+m.getMessageNumber());
        System.out.println("Subject : "+m.getSubject());
        System.out.println("From : "+m.getFrom());
        try {
          System.out.println("Subject : "+m.getContent().toString());
        } catch (IOException e) {
          System.out.println("No messages are available.............!");
          e.printStackTrace();
        }

      } // end for loop 

      // 5) Close the Folder and email store
      emailFolder.close(false);
      emailStore.close();

    } catch (NoSuchProviderException e) {
      e.printStackTrace();
    } catch (MessagingException e) {
      e.printStackTrace();
    }

  }
}
user432
  • 3,506
  • 17
  • 24
  • sorry man i got this exception : **java.net.ConnectException: Connection timed out: connect** , this line is suspecious : **emailStore.connect(pop3Host, user, password);** thanks... – akcHitman Mar 07 '14 at 10:14
  • 1
    Try to set the port, change `emailStore.connect(pop3Host, user, password);` to `emailStore.connect(pop3Host, 995, user, password);` The problem still is that no connection to the server can be established. – user432 Mar 07 '14 at 10:17
  • 1
    I edited my code and tested it. It works fine for me. When you still get connection errors there is possibly a problem with your network. Maybe a Firewall or Proxy that blocks POP3. – user432 Mar 07 '14 at 10:24
  • _I got this exception_ : javax.mail.AuthenticationFailedException: EOF on socket at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:146) at javax.mail.Service.connect(Service.java:275) atcom.mwh.DBConnect.receive_Email.receiveMail(receive_Email.java:40) at com.mwh.DBConnect.receive_Email.main(receive_Email.java:22) – akcHitman Mar 07 '14 at 10:26
  • The code works for me so I don't really know where the problem is. Maybe try to not use the POP3Store implementation and rather javax.mail.Store. So change `POP3Store emailStore = (POP3Store) sessEmail.getStore(sotreType);` to `Store emailStore = sessEmail.getStore(sotreType);` – user432 Mar 07 '14 at 10:36
0

I got Answer as i required thanks user user1232141 for ur kind replay.. I got the solution from this reference stactoverflow : reding all new mails
Here is the code that worked for me :

public static void receiveEmail(String pop3Host, String storeType,final String user, final String password) {  
              try {  
               //1) get the session object  
               Properties props = new Properties();  
               props.put("mail.pop3.host", "pop.gmail.com");
               
               props.put("mail.pop3.ssl.enable", "true"); // Use SSL
               
               props.put("mail.pop3.user", user);

               props.put("mail.pop3.socketFactory", 995);

               props.put("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

               props.put("mail.pop3.port", 995);
               
               Session session = Session.getDefaultInstance(props, new Authenticator() {
                   @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(user, password);

                    }
                });  
                 
               //2) create the POP3 store object and connect with the pop server  
               Store emailStore = (Store) session.getStore(storeType);  
               emailStore.connect("pop.gmail.com",995,user, password);  
              
               //3) create the folder object and open it  
               Folder emailFolder = emailStore.getFolder("INBOX");  
               emailFolder.open(Folder.READ_ONLY);  
              
               //4) retrieve the messages from the folder in an array and print it  
               Message[] messages = emailFolder.getMessages();  
               for (int i = 0; i < messages.length; i++) {  
                Message message = messages[i];  
                System.out.println("---------------------------------");  
                System.out.println("Email Number " + (i + 1));  
                System.out.println("Subject: " + message.getSubject());  
                System.out.println("From: " + message.getFrom()[0]);  
                System.out.println("Text: " + message.getContent().toString());  
               }  
              
               //5) close the store and folder objects  
               emailFolder.close(false);  
               emailStore.close();  
              
              } catch (MessagingException e) {e.printStackTrace();}  
              catch (IOException e) {e.printStackTrace();}  
             }  
    

as you can see i edited provided method receiveEmail(..){....} and got the answer,
Thank you srackoverflow user @wael your question helped me to find my required solution... Thank u all...

Community
  • 1
  • 1
akcHitman
  • 117
  • 3
  • 16