Advertisement
Guest User

Untitled

a guest
Jun 15th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.50 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Properties;
  3. import javax.mail.*;
  4. import javax.mail.internet.*;
  5.  
  6. public class GetParts {
  7.   public static void main (String args[])
  8.       throws Exception {
  9.     String host = args[0];
  10.     String username = args[1];
  11.     String password = args[2];
  12.  
  13.     // Get session
  14.     Session session = Session.getInstance(
  15.       new Properties(), null);
  16.  
  17.     // Get the store
  18.     Store store = session.getStore("pop3");
  19.     store.connect(host, username, password);
  20.  
  21.     // Get folder
  22.     Folder folder = store.getFolder("INBOX");
  23.     folder.open(Folder.READ_ONLY);
  24.  
  25.     BufferedReader reader = new BufferedReader (
  26.       new InputStreamReader(System.in));
  27.  
  28.     // Get directory
  29.     Message message[] = folder.getMessages();
  30.     for (int i=0, n=message.length; i<n; i++) {
  31.        System.out.println(i + ": "
  32.          + message[i].getFrom()[0]
  33.          + "\t" + message[i].getSubject());
  34.  
  35.       System.out.println(
  36.         "Do you want to get the content?
  37.           [YES to read/QUIT to end]");
  38.       String line = reader.readLine();
  39.       if ("YES".equals(line)) {
  40.         Object content = message[i].getContent();
  41.         if (content instanceof Multipart) {
  42.           handleMultipart((Multipart)content);
  43.         } else {
  44.           handlePart(message[i]);
  45.         }
  46.       } else if ("QUIT".equals(line)) {
  47.         break;
  48.       }
  49.     }
  50.  
  51.     // Close connection
  52.     folder.close(false);
  53.     store.close();
  54.   }
  55.   public static void handleMultipart(Multipart multipart)
  56.       throws MessagingException, IOException {
  57.     for (int i=0, n=multipart.getCount(); i<n; i++) {
  58.       handlePart(multipart.getBodyPart(i));
  59.     }
  60.   }
  61.   public static void handlePart(Part part)
  62.       throws MessagingException, IOException {
  63.     String disposition = part.getDisposition();
  64.     String contentType = part.getContentType();
  65.     if (disposition == null) { // When just body
  66.       System.out.println("Null: "  + contentType);
  67.       // Check if plain
  68.       if ((contentType.length() >= 10) &&
  69.           (contentType.toLowerCase().substring(
  70.            0, 10).equals("text/plain"))) {
  71.         part.writeTo(System.out);
  72.       } else { // Don't think this will happen
  73.         System.out.println("Other body: " + contentType);
  74.         part.writeTo(System.out);
  75.       }
  76.     } else if (disposition.equalsIgnoreCase(Part.ATTACHMENT)) {
  77.       System.out.println("Attachment: " + part.getFileName() +
  78.         " : " + contentType);
  79.       saveFile(part.getFileName(), part.getInputStream());
  80.     } else if (disposition.equalsIgnoreCase(Part.INLINE)) {
  81.       System.out.println("Inline: " +
  82.         part.getFileName() +
  83.         " : " + contentType);
  84.       saveFile(part.getFileName(), part.getInputStream());
  85.     } else {  // Should never happen
  86.       System.out.println("Other: " + disposition);
  87.     }
  88.   }
  89.   public static void saveFile(String filename,
  90.       InputStream input) throws IOException {
  91.     if (filename == null) {
  92.       filename = File.createTempFile("xx", ".out").getName();
  93.     }
  94.     // Do no overwrite existing file
  95.     File file = new File(filename);
  96.     for (int i=0; file.exists(); i++) {
  97.       file = new File(filename+i);
  98.     }
  99.     FileOutputStream fos = new FileOutputStream(file);
  100.     BufferedOutputStream bos = new BufferedOutputStream(fos);
  101.  
  102.     BufferedInputStream bis = new BufferedInputStream(input);
  103.     int aByte;
  104.     while ((aByte = bis.read()) != -1) {
  105.       bos.write(aByte);
  106.     }
  107.     bos.flush();
  108.     bos.close();
  109.     bis.close();
  110.   }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement