Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

java.io.IOException: java.io.EOFException: EOF on socket

902411Nov 28 2011 — edited Nov 30 2011
Hi All,
I always get the following exception when I get the body of a message :
------------Body------

	at javax.mail.internet.MimePartDataSource.getInputStream(MimePartDataSource.java:109)
	at com.sun.mail.handlers.text_plain.getContent(text_plain.java:107)
	at javax.activation.DataSourceDataContentHandler.getContent(DataHandler.java:790)
	at javax.activation.DataHandler.getContent(DataHandler.java:537)
	at javax.mail.internet.MimeMessage.getContent(MimeMessage.java:1419)
	at edu.coeia.onlinemail.OnlineEmailDownloader.getText(OnlineEmailDownloader.java:681)
	at edu.coeia.onlinemail.OnlineEmailDownloader.doInBackground(OnlineEmailDownloader.java:340)
	at edu.coeia.onlinemail.OnlineEmailDownloader.doInBackground(OnlineEmailDownloader.java:116)
	at javax.swing.SwingWorker$1.call(SwingWorker.java:296)
	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
	at java.util.concurrent.FutureTask.run(FutureTask.java:166)
	at javax.swing.SwingWorker.run(SwingWorker.java:335)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
	at java.lang.Thread.run(Thread.java:722)
java.io.IOException: java.io.EOFException: EOF on socket
	at javax.mail.internet.MimePartDataSource.getInputStream(MimePartDataSource.java:109)
	at com.sun.mail.handlers.text_plain.getContent(text_plain.java:107)
	at javax.activation.DataSourceDataContentHandler.getContent(DataHandler.java:790)
	at javax.activation.DataHandler.getContent(DataHandler.java:537)
	at javax.mail.internet.MimeMessage.getContent(MimeMessage.java:1419)
	at edu.coeia.onlinemail.OnlineEmailDownloader.getAttachments(OnlineEmailDownloader.java:614)
	at edu.coeia.onlinemail.OnlineEmailDownloader.doInBackground(OnlineEmailDownloader.java:382)
	at edu.coeia.onlinemail.OnlineEmailDownloader.doInBackground(OnlineEmailDownloader.java:116)
	at javax.swing.SwingWorker$1.call(SwingWorker.java:296)
	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
	at java.util.concurrent.FutureTask.run(FutureTask.java:166)
	at javax.swing.SwingWorker.run(SwingWorker.java:335)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
	at java.lang.Thread.run(Thread.java:722)
The function getText()
 private String getText(Part p) throws
                MessagingException, IOException, Exception {
        if (p.isMimeType("text/*")) {
            String s = null;
            try {
            s = (String)p.getContent();
            }
            catch ( UnsupportedEncodingException ex )
            {
             InputStream is = p.getInputStream();
	    /*
	     * Read the input stream into a byte array.
	     * Choose a charset in some heuristic manner, use
	     * that charset in the java.lang.String constructor
	     * to convert the byte array into a String.
	     */    
             s =  convertStreamToString(is);
            }
            catch ( Exception ex)
            {
                ex.printStackTrace();
            }
            
            textIsHtml = p.isMimeType("text/html");
            return s;
        }

        if (p.isMimeType("multipart/alternative")) {
            // prefer html text over plain text
            Multipart mp = (Multipart)p.getContent();
            String text = null;
            for (int i = 0; i < mp.getCount(); i++) {
                Part bp = mp.getBodyPart(i);
                if (bp.isMimeType("text/plain")) {
                    if (text == null)
                        text = getText(bp);
                    continue;
                } else if (bp.isMimeType("text/html")) {
                    String s = getText(bp);
                    if (s != null)
                        return s;
                } else {
                    return getText(bp);
                }
            }
            return text;
        } else if (p.isMimeType("multipart/*")) {
            Multipart mp = (Multipart)p.getContent();
            for (int i = 0; i < mp.getCount(); i++) {
                String s = getText(mp.getBodyPart(i));
                if (s != null)
                    return s;
            }
        }

        return null;
    }

 private List<String> getAttachments(Message temp) throws IOException, MessagingException {

        attachments = new ArrayList<String>();
        Object objRef = null;
        try {
            objRef = temp.getContent();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        if (!(objRef instanceof Multipart)) {
            return Collections.emptyList();
        }

        Multipart multipart = (Multipart) temp.getContent();
        System.out.println("Number of Attachments: " + multipart.getCount());

        for (int i = 0; i < multipart.getCount(); i++) {
            BodyPart bodyPart = multipart.getBodyPart(i);
            if (!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) {
                continue; // dealing with attachments only
            }

            String decoded = MimeUtility.decodeText(bodyPart.getFileName());

            String filename = Normalizer.normalize(decoded, Normalizer.Form.NFC);
            InputStream is = bodyPart.getInputStream();

            if (is == null) {
                continue;
            }

            System.out.println("file name" + filename);
            
            try {
            FileUtil.saveObject(bodyPart.getInputStream(), filename, attachmentsPath);
            }
            catch (Exception ex) {ex.printStackTrace();}
            
            attachments.add(filename);

        }

        return attachments;
    }
Edited by: EJP on 28/11/2011 18:52: added code tags. Please use them.

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Dec 28 2011
Added on Nov 28 2011
18 comments
5,717 views