Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 14th, 2005, 5:17 AM   #1
tsofras
Programmer
 
tsofras's Avatar
 
Join Date: Jul 2005
Location: Athens,Greece
Posts: 39
Rep Power: 0 tsofras is an unknown quantity at this point
EOF on Socket, exception..

Hello there,

i am just trying to implement a simple program that can connect to my gmail account and retrieve my e-mails, attachments etc.
Here is my code:
GetPic.java
package IMAIL;

import  javax.mail.*;
import  java.util.*;
import java.io.*;


public class GetPic {
     public static void main(String args[])
      {
          String username = "myusername@gmail.com";
          String password = "mypassword";
          String host = "pop.gmail.com";
          int port=995;

          // Create empty properties
          Properties props = new Properties();

           // Setup authentication, get session
 		  Authenticator auth = new PopupAuthenticator();
          Session session = Session.getDefaultInstance(props,auth);

          try {
            Store store = session.getStore("pop3");
            store.connect(host,port, username, password);

            // Get folder
            Folder folder = store.getFolder("INBOX");
            folder.open(Folder.READ_ONLY);
            // Get directory
            Message[] message = folder.getMessages();
            //Note: Trick for creating new image name. Erase it...
            for (int i=0, n=message.length; i<n; i++) {
                // Check out if it mime Message
                    System.out.println(i + ": " + message[i].getFrom()[0]
                    + "\t" + message[i].getSubject());
                    //Now get attachment (multiparts)
                    Multipart mp = (Multipart)message[i].getContent();
                    int numberOfnodyparts = mp.getCount();
                    //Count multipart occurences
                    for (int j=0; j<numberOfnodyparts; j++) {
                      Part part = mp.getBodyPart(j);
                      //
                      String disposition = part.getDisposition();
                      //if it's attachment save it
                      if ((disposition != null) &&
                         ((disposition.equals(Part.ATTACHMENT)))) {
                         //Print atatchment type
                         //System.out.println("Attachement type::" + part.getContentType());
                         //Save it only if attachment is an image
                         //NOTE: check for types: jpeg/png/gif

                         //NOTE: parse file's name and type and create a replica
                          System.out.println("Content-Type::\""+part.getContentType()+"END");
                          //savePic(part.getInputStream(), "C:\\aval\\inteliJ_Projects\\GetPicFromIMAIL\\"+i+".jpg");
                      }
                    }
                
            }
            folder.close(false);
            store.close();
        }
        catch (Exception e)
        {
            System.out.println("oops something went wrong!::"+e);

        }
    }

    private static void savePic(InputStream in, String s) throws IOException {
        File pic = new File(s);
        //NOTE: its better to use a buffer around the inputstream
        try {
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(pic));
            int b;
            while ((b = in.read()) != -1) {
                out.write(b);
            }
            in.close();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
	}
PopupAuthenticator.java
package IMAIL;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.swing.*;
import java.util.StringTokenizer;


public class PopupAuthenticator extends Authenticator
    {
      public PasswordAuthentication getPasswordAuthentication()
        {
            String username, password;
            String result = JOptionPane.showInputDialog("Enter 'username,password'");
            StringTokenizer st = new StringTokenizer(result, ",");
            username = st.nextToken();
            password = st.nextToken();
            return new PasswordAuthentication(username, password);
        }
    }

I just want to save my attachments from my e-mail if there is an image. I have implemented the getExtension method also and is working properly with a Filechooser that i found on the net. There is not the whole program in this code, but only with this i get this type of error:
oops something went wrong!::javax.mail.AuthenticationFailedException: EOF on socket

Any ideas what to do? i searched all over the internet and didn't find something to help me. Thx in advance , sorry for the long post
tsofras is offline   Reply With Quote
Old Dec 14th, 2005, 6:42 AM   #2
tsofras
Programmer
 
tsofras's Avatar
 
Join Date: Jul 2005
Location: Athens,Greece
Posts: 39
Rep Power: 0 tsofras is an unknown quantity at this point
Found it, thanks
tsofras is offline   Reply With Quote
Old Dec 14th, 2005, 6:57 AM   #3
b1g4L
Programmer
 
Join Date: Dec 2005
Location: SC
Posts: 38
Rep Power: 0 b1g4L is on a distinguished road
Send a message via AIM to b1g4L
What ended up being the problem? just curious.
b1g4L is offline   Reply With Quote
Old Dec 14th, 2005, 7:29 AM   #4
tsofras
Programmer
 
tsofras's Avatar
 
Join Date: Jul 2005
Location: Athens,Greece
Posts: 39
Rep Power: 0 tsofras is an unknown quantity at this point
I had to set up manual the properties :
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";


            Properties props = System.getProperties();
            //Set manual Properties
            props.setProperty( "mail.pop3.socketFactory.class", SSL_FACTORY);
            props.setProperty( "mail.pop3.socketFactory.fallback", "false");
            props.setProperty( "mail.pop3.port", "995");
            props.setProperty( "mail.pop3.socketFactory.port", "995");
            props.put("mail.pop3.host", "pop.gmail.com");

		    // Setup authentication, get session
 		    Authenticator auth = new PopupAuthenticator();
		    Session session = Session.getInstance(props,auth);
This was the "crutial part" in my opinion, and with some other changes finally is working :p
but i have to make a lot of things among this one
tsofras is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:22 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC