Java Properties Save saveMailAtt(String host, String userName, String password, String from, String directory)

Here you can find the source of saveMailAtt(String host, String userName, String password, String from, String directory)

Description

This method will open a pop mail box and read through all messages.

License

Open Source License

Declaration


public static boolean saveMailAtt(String host, String userName, String password, String from, String directory)
        throws Exception 

Method Source Code


//package com.java2s;

import java.util.Properties;

import javax.mail.*;

import java.io.*;

public class Main {
    /** This method will open a pop mail box and read through all messages.  If the from e-mail on the message
     *   equals the from parameter (case-sensitive) the attachment of the message (if any) is downloaded to
     *  the directory indicated by the directory parameter.  The directory must be on the
     *   computer on which this class executes or to which the computer has a drive mapping.
     *  The file name of the attachment is kept.  The message is then deleted.
     *///w  w w  .  j  a  v  a2s .co m
    //GN 09/04/2001 for LancomePro
    public static boolean saveMailAtt(String host, String userName, String password, String from, String directory)
            throws Exception {
        //System.out.println("in save");
        //System.out.println("userName " + userName);
        //System.out.println("password " + password);
        //System.out.println("host " + host);
        int msgFound = -1;
        boolean mailRead = false;
        // Create empty properties
        Properties props = new Properties();

        // Get session
        Session session = Session.getInstance(props, null);

        // Get the store
        //System.out.println("get Store, connect");
        Store store = session.getStore("pop3");
        store.connect(host, userName, password);
        // Get folder
        //System.out.println("get Folder, INBOX");
        Folder folder = store.getFolder("INBOX");
        folder.open(Folder.READ_ONLY);

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        // Get directory
        Message message[] = folder.getMessages();

        Address[] address;
        //System.out.println("# of msg " + message.length);

        for (int i = 0, n = message.length; i < n; i++) {
            address = message[i].getFrom();
            //System.out.println("from: " + address[0].toString());
            //System.out.println("infrom: " + from);
            if (from.equals(address[0].toString())) {
                //System.out.println("msg found");
                msgFound = i;
            }
        }
        for (int i = 0, n = message.length; i < n; i++) {
            address = message[i].getFrom();
            if (from.equals(address[0].toString())) {
                if (msgFound != i) {
                    mailRead = true;
                    message[i].setFlag(Flags.Flag.DELETED, true);
                    //System.out.println("bad msg, i = " + i);
                } else {
                    //System.out.println("good msg" + i);
                    try {
                        Object obj = message[i].getContent();

                        if (message[i].isMimeType("multipart/mixed")) {
                            Multipart multi = (Multipart) obj;
                            Part part = multi.getBodyPart(1);
                            String disp = part.getDisposition();

                            if (disp == null || disp.equalsIgnoreCase(Part.ATTACHMENT)) {
                                File dir = new File(directory);
                                if (dir.exists()) {
                                    if (!dir.isDirectory()) {
                                        dir.mkdir();
                                    }
                                } else {
                                    dir.mkdir();
                                }

                                String att = directory + part.getFileName();
                                //System.out.println("Filename = " + att);
                                FileOutputStream out = new FileOutputStream(att);
                                InputStream in = part.getInputStream();
                                int c;
                                while ((c = in.read()) != -1) {
                                    out.write(c);
                                }
                                out.flush();
                                out.close();
                            }
                            /*else
                            {
                            System.out.println(obj.toString());
                            }*/
                        }
                        mailRead = true;
                        //message[i].setFlag(Flags.Flag.DELETED, true);
                        //System.out.println("Done");
                    } catch (MessagingException mex) {
                        mex.printStackTrace();
                        throw new Exception("Error with the message. " + mex);
                    } catch (java.io.IOException ioe) {
                        ioe.printStackTrace();
                        throw new Exception("Error with the IO. " + ioe);
                    } catch (Exception e) {
                        e.printStackTrace();
                        throw new Exception("Other error. " + e);
                    }
                }
            }
        }
        // Close connection
        folder.close(true);

        store.close();

        return mailRead;
    }
}

Related

  1. saveConfig()
  2. saveConfig(File settingsFile, Properties properties)
  3. saveDefaultScriptDirectory(String path)
  4. SaveDeployedObjects(String outputDirectory)
  5. saveInstalledChecksumCache(File dir, Map checksums)
  6. saveOccurrencesAsText(String fileName, TreeMap distribution, int frequency, char[] ignore)
  7. saveOutputFile(String prefix, String suffix, String data)
  8. saveParamsToFile(String fileName, String[] params)
  9. saveParamToFile(String fileName, String param, String value)