Ask Experts Questions for FREE Help !
Ask
    vasavi's Avatar
    vasavi Posts: 5, Reputation: 1
    New Member
     
    #1

    May 15, 2008, 07:22 PM
    How can I read Gmail or Yahoo mail messages using Javamail?
    Hi,

    I want to connect to gmail or Yahoo mail server to fetch the new messages. I am trying to connect to gmail using the code below, but I am getting this error:


    " javax.mail.MessagingException: Connect failed;
    nested exception is:
    java.net.ConnectException: Connection timed out: connect
    at com.sun.mail.pop3.POP3Store.protocolConnect(POP3St ore.java:161)"


    What would be the reason? Can you please suggest ways to handle it?


    Code:
       public class TestMail{
     
        public static void main(String[] args)
        {
        	String user="xxx";
                    String passwd="yyy";
        	new TestMail().receive(user,passwd);
        }
        
      
        public void receive(String username,String password) 
    
    {
            String host = "pop3.gmail.com";
            try {
                Properties prop = new Properties();
                prop.setProperty("mail.pop3.socketFactory.class", 
                                            "javax.net.ssl.SSLSocketFactory");
                prop.setProperty("mail.pop3.socketFactory.fallback", "false");
                prop.setProperty("mail.pop3.port", "995");
                prop.setProperty("mail.pop3.socketFactory.port", "995");
               
                prop.put("mail.pop3.host", host);
                prop.put("mail.store.protocol", "pop3");
                Session session = Session.getDefaultInstance(prop);
                Store store = session.getStore();
                System.out.println("your ID is : "+ username);
                System.out.println("Connecting...");
                store.connect(host, username, password);
                System.out.println("Connected...");
                Folder inbox = store.getDefaultFolder().getFolder("INBOX");
                inbox.open(Folder.READ_ONLY);
     
                Message[] msg = inbox.getMessages();
                
                BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
     
                for (int i = 0; i < msg.length; i++) {
                  System.out.println("Subject:" + msg[i].getSubject());
                   System.out.println("Read message? [Y to read / N to end]");
                  String line = reader.readLine();
                  if ("Y".equalsIgnoreCase(line))
                  {
                    handleMultipart(msg[i]);
                    System.out.println("****************************");
                  }
                  else if ("N".equalsIgnoreCase(line))
                   {
                    break;
                  }
                  else
                  {}
                }
                if (inbox != null) {
                    inbox.close(true);
                }
                if (store != null) {
                    store.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
          private void handle(Message msg) throws Exception {
            System.out.println("From:" + msg.getFrom()[0].toString());
            System.out.println("SentDate:" + msg.getSentDate());
        }
     
        private void handleText(Message msg) throws Exception {
            handle(msg);
            System.out.println("Content:"+msg.getContent());
        }
     
        private void handleMultipart(Message msg) throws Exception {
            String disposition;
            BodyPart part;
            Multipart mp = (Multipart) msg.getContent();
            int mpCount = mp.getCount();
            for (int m = 0; m < mpCount; m++) {
                handle(msg);
                part = mp.getBodyPart(m);
                disposition = part.getDisposition();
                System.out.println(disposition);
                if (disposition != null && disposition.equals(Part.ATTACHMENT))
                {
                  BufferedReader areader = new BufferedReader(new InputStreamReader                 (System.in));
                  System.out.println("you get an attachment : " + part.getFileName());
                  System.out.println("Save attachment? [Y to read / N to end]");
                  String line = areader.readLine();
                  if ("Y".equalsIgnoreCase(line))
                  {
                    saveAttach(part);
                  }
                  else{}
                } else if(disposition.equals(Part.INLINE)){
                    System.out.println(part.getContent());
                } else {}
            }
        }
    
    }
    Thank you,
    Vasavi
    CFZD's Avatar
    CFZD Posts: 385, Reputation: 49
    Full Member
     
    #2

    May 15, 2008, 07:26 PM
    I can't get on either! So I guess we are not the only ones.
    Scleros's Avatar
    Scleros Posts: 2,165, Reputation: 262
    Hardware Expert
     
    #3

    May 15, 2008, 09:44 PM
    See Configuring other mail clients.

    Blind stab: That document lists POP3 host as "pop.gmail.com" not "pop3.gmail.com" as in your code.
    vasavi's Avatar
    vasavi Posts: 5, Reputation: 1
    New Member
     
    #4

    May 15, 2008, 11:40 PM
    If speciy "pop3.gmail.com" as host

    The error I get is
    "java.net.UnknownHostException"
    Scleros's Avatar
    Scleros Posts: 2,165, Reputation: 262
    Hardware Expert
     
    #5

    May 16, 2008, 10:59 AM
    Quote Originally Posted by vasavi
    if speciy "pop3.gmail.com" as host...
    Yes, the correct host is "pop.gmail.com". Your original post's code had "pop3.gmail.com".
    willtrytoanswer's Avatar
    willtrytoanswer Posts: 1, Reputation: 1
    New Member
     
    #6

    Sep 19, 2008, 02:54 AM
    Just change this line

    Prop.put("mail.store.protocol", "pop3s");
    eee32's Avatar
    eee32 Posts: 1, Reputation: 1
    New Member
     
    #7

    Nov 21, 2011, 03:00 AM
    I think the error is due to invalid username and password.

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Gmail, outlook express, Yahoo mail, and my iPhone [ 2 Answers ]

Okay, I don't understand all the computer lingo so please be patient with me. Here is my scenario: I just got a new job. I have a personal email on yahoo.com. I also have an account set up on gmail.com. In my previous job, I had all accounts going into Gmail, a program which I like very...

Reading outlook express mail in Yahoo and gmail [ 1 Answers ]

I use YAHOO, OUTLOOK EXPRESS and Gmail. I set up Gmail and YAHOO so I can read both in OE6. Now I want to do the reverse, READ OE mail in YAHOO and Gmail. The respective help pages are useless. Anyone know how this is done? [email protected] Thank you.


View more questions Search