1 Reply Latest reply on Nov 2, 2011 6:39 AM by Bill Shannon-Oracle

    Reading mail contents

    878460
      I am trying to read inbox using java mail prgm but it is showing some exception as

      DEBUG: setDebug: JavaMail version 1.3.1
      DEBUG: getProvider() returning javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]
      me
      DEBUG POP3: connecting to host "pop.gmail.com", port 995
      ---oops exception caught------javax.mail.MessagingException: Connect failed;
      nested exception is:
           java.net.SocketException: Connection reset

      my java prgms are
      ReadMail.java

      public class ReadMail {
           public static void main(String[] args) {
                
                String email = "das.kumar88@gmail.com";
                String toAddress = "das.kumar88@gmail.com";
                try {
                     String subject = "Cluster Software Solution";
                     String message = "Hi, This mail ur getting from java main class";
                     try {
                          FunctionReadClass mailClient = new FunctionReadClass();
                          mailClient.postMail(new String[] { toAddress }, subject, message,
                                    email);
                          System.out.println("Mail sent succesfully");
                     } catch (Exception e) {
                          System.out.println("---oops exception caught------"+e);
                     }
                } catch (Exception e) {
                     System.out.println("===========" + e);
                }
           }
      }
      and FuctionReadClass.java



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

      import javax.mail.Authenticator;
      import javax.mail.Folder;
      import javax.mail.Message;
      import javax.mail.MessagingException;
      import javax.mail.PasswordAuthentication;
      import javax.mail.Session;
      import javax.mail.Store;
      import javax.mail.Transport;
      import javax.mail.internet.InternetAddress;
      import javax.mail.internet.MimeMessage;

      public class FunctionReadClass extends Authenticator {
           private String gmailUserName = "praveenkhannur@gmail.com";
           private String gmailPassword = "password";
      /*public static void main(String []a) throws Exception, IOException
      {
           FunctionReadClass f=new FunctionReadClass();
           f.postMail(new String[]{},"2","3","4");
           
      }*/
           public void postMail(String recipients[], String subject, String message,
                     String from) throws MessagingException, IOException {
                boolean debug = true;
                String hostmailserver="pop.gmail.com";
                Properties props = new Properties();
                //props.setProperty("mail.transport.protocol", "smtp");
                props.setProperty("mail.pop3.host", "pop.gmail.com");
                props.put("mail.pop3.auth", "true");
                props.put("mail.pop3.port", "995");
                props.put("mail.pop3.socketFactory.port", "995");
                props.put("mail.pop3.socketFactory.class",
                          "javax.net.ssl.SSLSocketFactory");
                props.put("mail.pop3.socketFactory.fallback", "false");
                props.put("mail.pop3.host", hostmailserver);
                props.put("mail.store.protocol", "pop3");
                //props.setProperty("mail.smtp.quitwait", "false");
                
                Session session = Session.getDefaultInstance(props,this);
                
                session.setDebug(debug);
                
                //Message msg = new MimeMessage(session);
                //InternetAddress addressFrom = new InternetAddress(from);
                //msg.setFrom(addressFrom);
                /*InternetAddress[] addressTo = new InternetAddress[recipients.length];
                for (int i = 0; i < recipients.length; i++) {
                     addressTo[i] = new InternetAddress(recipients);
                }
                
                msg.setRecipients(Message.RecipientType.TO, addressTo);
                msg.setSubject(subject);
                msg.setContent(message, "text/html");
                Transport.send(msg);
           */
                Store store= session.getStore("pop3");
                System.out.println("me");
                store.connect();
                
                Folder inbox=store.getDefaultFolder().getFolder("INBOX");
                if(inbox==null)
                {
                     System.out.println("no inbox");
                     System.exit(1);
                }
                inbox.open(Folder.READ_ONLY);
                Message[] msg=inbox.getMessages();
                for(int i=0;i<msg.length;i++)
                {
                     System.out.println("Meassage"+(i+1));
                     msg[i].writeTo(System.out);
                }
                     inbox.close(false);
                     store.close();
                     
           }

           public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(gmailUserName, gmailPassword);
           }

      }

      I am running this prgm using eclipse...
      or if u have any better and easy code for reading from gmail, plz post the entire code......