Read in email from inbox and store it on local disks

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • baygan
    New Member
    • Feb 2007
    • 5

    Read in email from inbox and store it on local disks

    can anyone plz help me to write a code to read mail from one's inbox and store the mails to the local disk??? i forgot to say it would be preferably in java..

    i would be very grateful if someone could help me out in this matter.

    tks in advance..

    Ronald.
  • baygan
    New Member
    • Feb 2007
    • 5

    #2
    Read in email from inbox and store it on local disks

    can anyone plz help me to write a code to read mail from one's inbox and store the mails to the local disk??? i forgot to say it would be preferably in java..

    i would be very grateful if someone could help me out in this matter.

    tks in advance..

    Ronald.

    Comment

    • horace1
      Recognized Expert Top Contributor
      • Nov 2006
      • 1510

      #3
      Originally posted by baygan
      can anyone plz help me to write a code to read mail from one's inbox and store the mails to the local disk??? i forgot to say it would be preferably in java..

      i would be very grateful if someone could help me out in this matter.

      tks in advance..

      Ronald.
      have a look at the java mail API
      http://java.sun.com/products/javamail/

      Comment

      • babaralibhayo
        New Member
        • Feb 2007
        • 1

        #4
        Originally posted by baygan
        can anyone plz help me to write a code to read mail from one's inbox and store the mails to the local disk??? i forgot to say it would be preferably in java..

        i would be very grateful if someone could help me out in this matter.

        tks in advance..

        Ronald.


        hi i am boby and i am very week in two subjects (1)vb.net (2)java2 so please would you like you help me i will be your thank full

        Comment

        • abctech
          New Member
          • Dec 2006
          • 157

          #5
          Originally posted by babaralibhayo
          hi i am boby and i am very week in two subjects (1)vb.net (2)java2 so please would you like you help me i will be your thank full
          Have a read at the JavaMail API first and then make an attempt at your code, post the code here if you get stuck or are getting any exceptions.

          P.S : This is the Java forum, .Net posts are made in the .Net forum if you are trying to program in it.

          Comment

          • baygan
            New Member
            • Feb 2007
            • 5

            #6
            import java.util.*;
            import java.io.*;
            import javax.mail.*;
            import javax.mail.inte rnet.*;
            import javax.mail.sear ch.*;
            import javax.activatio n.*;


            public class FetchMailUsage {

            public static void main(String[] args) {

            // SUBSTITUTE YOUR ISP's POP3 SERVER HERE!!!
            String host = "pop.yourisp.ne t";
            // SUBSTITUTE YOUR USERNAME AND PASSWORD TO ACCESS E-MAIL HERE!!!
            String user = "your_username" ;
            String password = "your_password" ;
            // SUBSTITUTE YOUR SUBJECT SUBSTRING TO SEARCH HERE!!!
            String subjectSubstrin gToSearch = "Test E-Mail through Java";

            // Get a session. Use a blank Properties object.
            Session session = Session.getInst ance(new Properties());

            try {

            // Get a Store object
            Store store = session.getStor e("pop3");
            store.connect(h ost, user, password);

            // Get "INBOX"
            Folder fldr = store.getFolder ("INBOX");
            fldr.open(Folde r.READ_WRITE);
            int count = fldr.getMessage Count();
            System.out.prin tln(count + " total messages");

            // Message numebers start at 1
            for(int i = 1; i <= count; i++) {
            // Get a message by its sequence number
            Message m = fldr.getMessage (i);

            // Get some headers
            Date date = m.getSentDate() ;
            Address [] from = m.getFrom();
            String subj = m.getSubject();
            String mimeType = m.getContentTyp e();
            System.out.prin tln(date + "\t" + from[0] + "\t" +
            subj + "\t" + mimeType);
            }

            // Search for e-mails by some subject substring
            String pattern = subjectSubstrin gToSearch;
            SubjectTerm st = new SubjectTerm(pat tern);
            // Get some message references
            Message [] found = fldr.search(st) ;

            System.out.prin tln(found.lengt h +
            " messages matched Subject pattern \"" +
            pattern + "\"");

            for (int i = 0; i < found.length; i++) {
            Message m = found[i];
            // Get some headers
            Date date = m.getSentDate() ;
            Address [] from = m.getFrom();
            String subj = m.getSubject();
            String mimeType = m.getContentTyp e();
            System.out.prin tln(date + "\t" + from[0] + "\t" +
            subj + "\t" + mimeType);

            Object o = m.getContent();
            if (o instanceof String) {
            System.out.prin tln("**This is a String Message**");
            System.out.prin tln((String)o);
            }
            else if (o instanceof Multipart) {
            System.out.prin t("**This is a Multipart Message. ");
            Multipart mp = (Multipart)o;
            int count3 = mp.getCount();
            System.out.prin tln("It has " + count3 +
            " BodyParts in it**");
            for (int j = 0; j < count3; j++) {
            // Part are numbered starting at 0
            BodyPart b = mp.getBodyPart( j);
            String mimeType2 = b.getContentTyp e();
            System.out.prin tln( "BodyPart " + (j + 1) +
            " is of MimeType " + mimeType);

            Object o2 = b.getContent();
            if (o2 instanceof String) {
            System.out.prin tln("**This is a String BodyPart**");
            System.out.prin tln((String)o2) ;
            }
            else if (o2 instanceof Multipart) {
            System.out.prin t(
            "**This BodyPart is a nested Multipart. ");
            Multipart mp2 = (Multipart)o2;
            int count2 = mp2.getCount();
            System.out.prin tln("It has " + count2 +
            "further BodyParts in it**");
            }
            else if (o2 instanceof InputStream) {
            System.out.prin tln(
            "**This is an InputStream BodyPart**");
            }
            } //End of for
            }
            else if (o instanceof InputStream) {
            System.out.prin tln("**This is an InputStream message**");
            InputStream is = (InputStream)o;
            // Assumes character content (not binary images)
            int c;
            while ((c = is.read()) != -1) {
            System.out.writ e(c);
            }
            }

            // Uncomment to set "delete" flag on the message
            //m.setFlag(Flags .Flag.DELETED,t rue);

            } //End of for

            // "true" actually deletes flagged messages from folder
            fldr.close(true );
            store.close();

            }
            catch (MessagingExcep tion mex) {
            // Prints all nested (chained) exceptions as well
            mex.printStackT race();
            }
            catch (IOException ioex) {
            ioex.printStack Trace();
            }

            }


            } //End of class

            can anyone help me out by executin this code an dtellin me if it works???

            plz give me a quick repl..

            tks

            Comment

            • horace1
              Recognized Expert Top Contributor
              • Nov 2006
              • 1510

              #7
              does it work when you run it? if not what happens?

              Comment

              • hirak1984
                Contributor
                • Jan 2007
                • 316

                #8
                LOOK AT THIS!!

                please dont double post.

                Originally posted by baygan
                can anyone plz help me to write a code to read mail from one's inbox and store the mails to the local disk??? i forgot to say it would be preferably in java..

                i would be very grateful if someone could help me out in this matter.

                tks in advance..

                Ronald.

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by baygan
                  import java.util.*;
                  import java.io.*;
                  import javax.mail.*;
                  import javax.mail.inte rnet.*;
                  import javax.mail.sear ch.*;
                  import javax.activatio n.*;


                  public class FetchMailUsage {

                  public static void main(String[] args) {

                  // SUBSTITUTE YOUR ISP's POP3 SERVER HERE!!!
                  String host = "pop.yourisp.ne t";
                  // SUBSTITUTE YOUR USERNAME AND PASSWORD TO ACCESS E-MAIL HERE!!!
                  String user = "your_username" ;
                  String password = "your_password" ;
                  // SUBSTITUTE YOUR SUBJECT SUBSTRING TO SEARCH HERE!!!
                  String subjectSubstrin gToSearch = "Test E-Mail through Java";

                  // Get a session. Use a blank Properties object.
                  Session session = Session.getInst ance(new Properties());

                  try {

                  // Get a Store object
                  Store store = session.getStor e("pop3");
                  store.connect(h ost, user, password);

                  // Get "INBOX"
                  Folder fldr = store.getFolder ("INBOX");
                  fldr.open(Folde r.READ_WRITE);
                  int count = fldr.getMessage Count();
                  System.out.prin tln(count + " total messages");

                  // Message numebers start at 1
                  for(int i = 1; i <= count; i++) {
                  // Get a message by its sequence number
                  Message m = fldr.getMessage (i);

                  // Get some headers
                  Date date = m.getSentDate() ;
                  Address [] from = m.getFrom();
                  String subj = m.getSubject();
                  String mimeType = m.getContentTyp e();
                  System.out.prin tln(date + "\t" + from[0] + "\t" +
                  subj + "\t" + mimeType);
                  }

                  // Search for e-mails by some subject substring
                  String pattern = subjectSubstrin gToSearch;
                  SubjectTerm st = new SubjectTerm(pat tern);
                  // Get some message references
                  Message [] found = fldr.search(st) ;

                  System.out.prin tln(found.lengt h +
                  " messages matched Subject pattern \"" +
                  pattern + "\"");

                  for (int i = 0; i < found.length; i++) {
                  Message m = found[i];
                  // Get some headers
                  Date date = m.getSentDate() ;
                  Address [] from = m.getFrom();
                  String subj = m.getSubject();
                  String mimeType = m.getContentTyp e();
                  System.out.prin tln(date + "\t" + from[0] + "\t" +
                  subj + "\t" + mimeType);

                  Object o = m.getContent();
                  if (o instanceof String) {
                  System.out.prin tln("**This is a String Message**");
                  System.out.prin tln((String)o);
                  }
                  else if (o instanceof Multipart) {
                  System.out.prin t("**This is a Multipart Message. ");
                  Multipart mp = (Multipart)o;
                  int count3 = mp.getCount();
                  System.out.prin tln("It has " + count3 +
                  " BodyParts in it**");
                  for (int j = 0; j < count3; j++) {
                  // Part are numbered starting at 0
                  BodyPart b = mp.getBodyPart( j);
                  String mimeType2 = b.getContentTyp e();
                  System.out.prin tln( "BodyPart " + (j + 1) +
                  " is of MimeType " + mimeType);

                  Object o2 = b.getContent();
                  if (o2 instanceof String) {
                  System.out.prin tln("**This is a String BodyPart**");
                  System.out.prin tln((String)o2) ;
                  }
                  else if (o2 instanceof Multipart) {
                  System.out.prin t(
                  "**This BodyPart is a nested Multipart. ");
                  Multipart mp2 = (Multipart)o2;
                  int count2 = mp2.getCount();
                  System.out.prin tln("It has " + count2 +
                  "further BodyParts in it**");
                  }
                  else if (o2 instanceof InputStream) {
                  System.out.prin tln(
                  "**This is an InputStream BodyPart**");
                  }
                  } //End of for
                  }
                  else if (o instanceof InputStream) {
                  System.out.prin tln("**This is an InputStream message**");
                  InputStream is = (InputStream)o;
                  // Assumes character content (not binary images)
                  int c;
                  while ((c = is.read()) != -1) {
                  System.out.writ e(c);
                  }
                  }

                  // Uncomment to set "delete" flag on the message
                  //m.setFlag(Flags .Flag.DELETED,t rue);

                  } //End of for

                  // "true" actually deletes flagged messages from folder
                  fldr.close(true );
                  store.close();

                  }
                  catch (MessagingExcep tion mex) {
                  // Prints all nested (chained) exceptions as well
                  mex.printStackT race();
                  }
                  catch (IOException ioex) {
                  ioex.printStack Trace();
                  }

                  }


                  } //End of class

                  can anyone help me out by executin this code an dtellin me if it works???

                  plz give me a quick repl..

                  tks
                  Please use code tags when posting code.

                  Execute the code yourself and tell us if you have any problems.

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by hirak1984
                    LOOK AT THIS!!

                    please dont double post.
                    Threads merged.

                    Comment

                    Working...