Advertisement
Guest User

ahfjhadskfhasdlkf

a guest
Aug 26th, 2018
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.22 KB | None | 0 0
  1. package mail;
  2.  
  3. import java.io.BufferedOutputStream;
  4. import java.io.BufferedReader;
  5. import java.io.DataOutputStream;
  6. import java.io.File;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.InputStreamReader;
  11. import java.util.Date;
  12. import java.util.Properties;
  13.  
  14. import javax.mail.Address;
  15. import javax.mail.Folder;
  16. import javax.mail.Message;
  17. import javax.mail.MessagingException;
  18. import javax.mail.Multipart;
  19. import javax.mail.NoSuchProviderException;
  20. import javax.mail.Part;
  21. import javax.mail.Session;
  22. import javax.mail.Store;
  23.  
  24. public class FetchingEmail {
  25.  
  26.    public static void fetch(String pop3Host, String storeType, String user,
  27.       String password) {
  28.       try {
  29.          // create properties field
  30.          Properties properties = new Properties();
  31.          properties.put("mail.store.protocol", "pop3");
  32.          properties.put("mail.pop3.host", pop3Host);
  33.          properties.put("mail.pop3.port", "995");
  34.          properties.put("mail.pop3.starttls.enable", "true");
  35.          Session emailSession = Session.getDefaultInstance(properties);
  36.          // emailSession.setDebug(true);
  37.  
  38.          // create the POP3 store object and connect with the pop server
  39.          Store store = emailSession.getStore("pop3s");
  40.  
  41.          store.connect(pop3Host, user, password);
  42.  
  43.          // create the folder object and open it
  44.          Folder emailFolder = store.getFolder("INBOX");
  45.          emailFolder.open(Folder.READ_ONLY);
  46.  
  47.          BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  48.  
  49.          // retrieve the messages from the folder in an array and print it
  50.          Message[] messages = emailFolder.getMessages();
  51.          System.out.println("messages.length---" + messages.length);
  52.  
  53.          for (int i = 0; i < messages.length ; i++) {
  54.             Message message = messages[i];
  55.             System.out.println("---------------------------------");
  56.             writePart(message);
  57.             String line = reader.readLine();
  58.             if ("YES".equals(line)) {
  59.                message.writeTo(System.out);
  60.             } else if ("QUIT".equals(line)) {
  61.                break;
  62.             }
  63.          }
  64.  
  65.          // close the store and folder objects
  66.          emailFolder.close(false);
  67.          store.close();
  68.  
  69.       } catch (NoSuchProviderException e) {
  70.          e.printStackTrace();
  71.       } catch (MessagingException e) {
  72.          e.printStackTrace();
  73.       } catch (IOException e) {
  74.          e.printStackTrace();
  75.       } catch (Exception e) {
  76.          e.printStackTrace();
  77.       }
  78.    }
  79.    public static void main(String[] args) {
  80.  
  81.       String host = "pop.gmail.com";// change accordingly
  82.       String mailStoreType = "pop3";
  83.       String username ="olelavrov@gmail.com";// change accordingly
  84.       String password = "Mamasha166";// change accordingly
  85.  
  86.       //Call method fetch
  87.       fetch(host, mailStoreType, username, password);
  88.  
  89.    }
  90.  
  91.    /*
  92.    * This method checks for content-type
  93.    * based on which, it processes and
  94.    * fetches the content of the message
  95.    */
  96.    public static void writePart(Part p) throws Exception {
  97.       if (p instanceof Message)
  98.          //Call methods writeEnvelope
  99.          writeEnvelope((Message) p);
  100.  
  101.       System.out.println("----------------------------");
  102.       System.out.println("CONTENT-TYPE: " + p.getContentType());
  103.  
  104.       //check if the content is plain text
  105.       if (p.isMimeType("text/plain")) {
  106.          System.out.println("This is plain text");
  107.          System.out.println("---------------------------");
  108.          System.out.println((String) p.getContent());
  109.       }
  110.       //check if the content has attachment
  111.       else if (p.isMimeType("multipart/*")) {
  112.          System.out.println("This is a Multipart");
  113.          System.out.println("---------------------------");
  114.          Multipart mp = (Multipart) p.getContent();
  115.          int count = mp.getCount();
  116.          for (int i = 0; i < count; i++)
  117.             writePart(mp.getBodyPart(i));
  118.       }
  119.       //check if the content is a nested message
  120.       else if (p.isMimeType("message/rfc822")) {
  121.          System.out.println("This is a Nested Message");
  122.          System.out.println("---------------------------");
  123.          writePart((Part) p.getContent());
  124.       }
  125.       //check if the content is an inline image
  126.     //check if the content is an inline image
  127.       else if (p.isMimeType("image/jpeg")) {
  128.       System.out.println("--------> image/jpeg");
  129.       Object o = p.getContent();
  130.       InputStream x = (InputStream) o;
  131.       // Construct the required byte array
  132.       System.out.println("x.length = " + x.available());
  133.       int i = 0;
  134.       byte[] bArray = new byte[x.available()];
  135.       while ((i = (int) ((InputStream) x).available()) > 0) {
  136.       int result = (int) (((InputStream) x).read(bArray));
  137.       if (result == -1)
  138.       break;
  139.       }
  140.       FileOutputStream f2 = new FileOutputStream("/tmp/image.jpg");
  141.       f2.write(bArray);
  142.       }
  143.       else if (p.getContentType().contains("image/")) {
  144.          System.out.println("content type" + p.getContentType());
  145.          File f = new File("image" + new Date().getTime() + ".jpg");
  146.          DataOutputStream output = new DataOutputStream(
  147.             new BufferedOutputStream(new FileOutputStream(f)));
  148.             com.sun.mail.util.BASE64DecoderStream test =
  149.                  (com.sun.mail.util.BASE64DecoderStream) p
  150.                   .getContent();
  151.          byte[] buffer = new byte[1024];
  152.          int bytesRead;
  153.          while ((bytesRead = test.read(buffer)) != -1) {
  154.             output.write(buffer, 0, bytesRead);
  155.          }
  156.       }
  157.       else {
  158.          Object o = p.getContent();
  159.          if (o instanceof String) {
  160.             System.out.println("This is a string");
  161.             System.out.println("---------------------------");
  162.             System.out.println((String) o);
  163.          }
  164.          else if (o instanceof InputStream) {
  165.             System.out.println("This is just an input stream");
  166.             System.out.println("---------------------------");
  167.             InputStream is = (InputStream) o;
  168.             is = (InputStream) o;
  169.             int c;
  170.             while ((c = is.read()) != -1)
  171.                System.out.write(c);
  172.          }
  173.          else {
  174.             System.out.println("This is an unknown type");
  175.             System.out.println("---------------------------");
  176.             System.out.println(o.toString());
  177.          }
  178.       }
  179.  
  180.    }
  181.    /*
  182.    * This method would print FROM,TO and SUBJECT of the message
  183.    */
  184.    public static void writeEnvelope(Message m) throws Exception {
  185.       System.out.println("This is the message envelope");
  186.       System.out.println("---------------------------");
  187.       Address[] a;
  188.  
  189.       // FROM
  190.       if ((a = m.getFrom()) != null) {
  191.          for (int j = 0; j < a.length; j++)
  192.          System.out.println("FROM: " + a[j].toString());
  193.       }
  194.  
  195.       // TO
  196.       if ((a = m.getRecipients(Message.RecipientType.TO)) != null) {
  197.          for (int j = 0; j < a.length; j++)
  198.          System.out.println("TO: " + a[j].toString());
  199.       }
  200.  
  201.       // SUBJECT
  202.       if (m.getSubject() != null)
  203.          System.out.println("SUBJECT: " + m.getSubject());
  204.  
  205.    }
  206.  
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement