Guest User

Untitled

a guest
Mar 14th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.92 KB | None | 0 0
  1.  
  2. import java.io.*;
  3. import java.util.Properties;
  4. import javax.mail.*;
  5. import javax.mail.internet.*;
  6.  
  7. public class GmailMailAttach {
  8.     public static void main (String args[]) throws Exception {
  9.         String host = "pop.gmail.com";
  10.         String username = "amitt800"; //Put here Gmail Username without @ sign
  11.         String password = "123456"; // put here Gmail password
  12.         Session session = Session.getInstance(new Properties(), null);
  13.         Store store = session.getStore("pop3s");
  14.         store.connect(host, username, password);
  15.         Folder folder = store.getFolder("INBOX");
  16.         folder.open(Folder.READ_ONLY);
  17.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  18.         Message message[] = folder.getMessages();
  19.        Enumeration headers = message[i].getAllHeaders();
  20.        while (headers.hasMoreElements()) {
  21.              Header h = (Header) headers.nextElement();
  22.              System.out.println(h.getName() + ": " + h.getValue());
  23.        }
  24.  
  25.         for(int i=0, n=message.length; i++) {
  26.             System.out.println(i + ": " + message[i].getFrom()[0]+ "\t" + message[i].getSubject());
  27.             System.out.println("Want to get the content? [Y to read/Q to end]");
  28.             String ans = reader.readLine();
  29.             ans=ans.toLowerCase();
  30.             if ("y".equals(ans)) {
  31.                 Object content = message[i].getContent();
  32.                 if (content instanceof Multipart) {
  33.                     handleMultipart((Multipart)content);
  34.                 }
  35.                 else {
  36.                     handlePart(message[i]);
  37.                 }
  38.             }
  39.             else if ("q".equals(ans)) {
  40.                 break;
  41.             }
  42.         }
  43.         folder.close(false);
  44.         store.close();
  45.     }
  46.     public static void handleMultipart(Multipart multipart) throws MessagingException, IOException {
  47.         for (int i=0, n=multipart.getCount(); i
  48.             handlePart(multipart.getBodyPart(i));
  49.         }
  50.       }
  51.     public static void handlePart(Part part)  throws MessagingException, IOException {
  52.         String dposition = part.getDisposition();
  53.         String cType = part.getContentType();
  54.         if (dposition == null) {
  55.             System.out.println("Null: "  + cType);
  56.             if ((cType.length() >= 10) && (cType.toLowerCase().substring(0, 10).equals("text/plain"))) {
  57.                 part.writeTo(System.out);
  58.             }
  59.             else {
  60.                 System.out.println("Other body: " + cType);
  61.                 part.writeTo(System.out);
  62.             }
  63.         }
  64.         else if (dposition.equalsIgnoreCase(Part.ATTACHMENT)) {
  65.             System.out.println("Attachment: " + part.getFileName() + " : " + cType);
  66.             saveFile(part.getFileName(), part.getInputStream());
  67.         }
  68.         else if (dposition.equalsIgnoreCase(Part.INLINE)) {
  69.             System.out.println("Inline: " + part.getFileName() +  " : " + cType);
  70.             saveFile(part.getFileName(), part.getInputStream());
  71.         }
  72.         else {
  73.             System.out.println("Other: " + dposition);
  74.         }
  75.     }
  76.    
  77.     public static void saveFile(String filename,InputStream input) throws IOException {
  78.         if (filename == null) {
  79.             filename = File.createTempFile("MailAttacheFile", ".out").getName();
  80.         }
  81.         System.out.println("downloading attachment...");
  82.         File file = new File(filename);
  83.         for (int i=0; file.exists(); i++) {
  84.             file = new File(filename+i);
  85.         }
  86.         FileOutputStream fos = new FileOutputStream(file);
  87.         BufferedOutputStream bos = new BufferedOutputStream(fos);
  88.         BufferedInputStream bis = new BufferedInputStream(input);
  89.         int fByte;
  90.         while ((fByte = bis.read()) != -1) {
  91.             bos.write(fByte);
  92.             }
  93.         bos.flush();
  94.         bos.close();
  95.         bis.close();
  96.         System.out.println("done attachment...");
  97.     }
  98. }
Add Comment
Please, Sign In to add comment