Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Read attachment message using Java Mail 
 

This Example shows you how to read a Attachment message using javamail api. Java Mail API provides classes to send multiple Mime body part with one Mime Message

 

Read attachment message using Java Mail

                         

This Example shows you how to read a Attachment message using javamail api. Java Mail API provides classes to send multiple Mime body part with one Mime Message. MulipltMimeBody parts can be text, file or image. All message are stored in Folder objects. folders can contain folders or messages. The Folder class declares methods that fetch, append, copy and delete messages, and message contain Attachment.

 

 

 

 

ReadAttachment.java

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

public class ReadAttachment {

    public static void main(String args[]) throws Exception {
        String host = "192.168.10.205";
        String user = "komal";
        String password = "komal";

        // Get system properties
        Properties properties = System.getProperties();

        // Get the default Session object.
        Session session = Session.getDefaultInstance(properties);

        // Get a Store object that implements the specified protocol.
        Store store = session.getStore("pop3");

        //Connect to the current host using the specified username and password.
        store.connect(host, user, password);

        //Create a Folder object corresponding to the given name.
        Folder folder = store.getFolder("inbox");

        // Open the Folder.
        folder.open(Folder.READ_WRITE);

        Message[] message = folder.getMessages();


        for (int a = 0; a < message.length; a++) {
            System.out.println("-------------" (a + 1"-----------");
            System.out.println(message[a].getSentDate());

            Multipart multipart = (Multipartmessage[a].getContent();
            //System.out.println(multipart.getCount());

            for (int i = 0; i < multipart.getCount(); i++) {
                //System.out.println(i);
                //System.out.println(multipart.getContentType());
                BodyPart bodyPart = multipart.getBodyPart(i);
                InputStream stream = bodyPart.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(stream));

                while (br.ready()) {
                    System.out.println(br.readLine());
                }
                System.out.println();
            }
            System.out.println();
        }
        folder.close(true);
        store.close();
    }
}
    

Output:

---------Message 1----------
Thu Jul 17 00:09:33 IST 2008
hi

hi this is komal


---------Message 2----------
Thu Jul 17 00:09:22 IST 2008
hi.....

hi this is komal


---------Message 3----------
Thu Jul 17 01:02:23 IST 2008
eml mail

Return-Path: <test@localhost>
Delivered-To: <test@localhost>
Received: from 192.168.10.111 ([192.168.10.111])
          by roseindi-iyo058 (JAMES SMTP Server 2.3.1with SMTP ID 18
          for <test@localhost>;
          Wed, 16 Jul 2008 14:33:32 +0000 (GMT)
Date: Wed, 16 Jul 2008 14:33:32 +0000 (GMT)
From: test@localhost
To: test@localhost
Message-ID: <6794265.0.1216195417823.JavaMail.komal@komal>
Subject: hi..!
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

hi.. this is komal choudhary

Download code

                         

» View all related tutorials
Related Tags: java c api class mail method ip new read using this message ai config show if ie example add javamail

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.