Friday, 29 August 2014

Fetch the Inbox Mails with Attachments sepratly

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeMultipart;

public class AttachmentReader {

    public static void parsemessage(Message message) throws MessagingException, IOException {
          System.out.println( "<"+message.getFrom()[0] + "> " + message.getSubject());
          Multipart multipart = (Multipart)message.getContent();
          System.out.println("     > Message has "+multipart.getCount()+" multipart elements");
            for (int j = 0; j < multipart.getCount(); j++) {
                BodyPart bodyPart = multipart.getBodyPart(j);
                if(!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) {
                    if (bodyPart.getContent().getClass().equals(MimeMultipart.class)) {
                        MimeMultipart mimemultipart = (MimeMultipart)bodyPart.getContent();
                        System.out.println("Number of embedded multiparts "+mimemultipart.getCount());
                        for (int k=0;k<mimemultipart.getCount();k++) {
                            if (mimemultipart.getBodyPart(k).getFileName() != null) {
                                System.out.println("     > Creating file with name : "+mimemultipart.getBodyPart(k).getFileName());
                                savefile(mimemultipart.getBodyPart(k).getFileName(), mimemultipart.getBodyPart(k).getInputStream());
                            }
                        }
                    }
                  continue;
                }
                System.out.println("     > Creating file with name : "+bodyPart.getFileName());
                savefile(bodyPart.getFileName(), bodyPart.getInputStream());
            }
        }
   
   
    public static void savefile(String FileName, InputStream is) throws IOException {
        File f = new File("Attachment/" + FileName);
        FileOutputStream fos = new FileOutputStream(f);
        byte[] buf = new byte[4096];
        int bytesRead;
        while((bytesRead = is.read(buf))!=-1) {
            fos.write(buf, 0, bytesRead);
        }
        fos.close();
    }
   
    
   
public static void main(String[] args) {
 try {
final String user="userName1@gmail.com";
final    String password="Password1";
String host="pop.gmail.com";
Properties props = new Properties();
props.put("mail.pop3.host", "pop.gmail.com");

props.put("mail.pop3.user", user);

props.put("mail.pop3.socketFactory", 995);

props.put("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

props.put("mail.pop3.port", 995);
    Session session = Session.getInstance(props,
                 new javax.mail.Authenticator() {
                   
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,password);
}
});
   
 
Store store = session.getStore("pop3");
 store.connect("pop.gmail.com",user,password);
 System.out.println("Connected");
 Folder folder = store.getFolder("inbox"); 
        folder.open(Folder.READ_WRITE); 
    
        Message[] message = folder.getMessages(); 
       
        System.out.println("Connected");
        System.out.println(message.length);
      //  for (int a = message.length; a>0; a--) {
        for (int a = 0; a < message.length; a++) { 
            System.out.println("-------------" + (a + 1) + "-----------"); 
            System.out.println(message[a].getSentDate()); 
            System.out.println(message[a].getSubject()); 
           
           // String pp=message[a].getSentDate().toString();
         //   System.out.println(pp+"       Ye hai date ");
           //String tt=message[a].getSubject();
           //String det=pp.substring(0, 20);
            message[a].writeTo(new FileOutputStream("Inbox/"+message[a].getFrom().toString()+".txt",true));
           // message[a].getFrom().toString()
           parsemessage(message[a]);
      
       
        }
       

 } catch (Exception e) {

e.printStackTrace();

}    
}

}

1 comment: