Results 1 to 4 of 4
  1. #1
    vidya is offline Member
    Join Date
    Jan 2010
    Posts
    9
    Rep Power
    0

    Default Query in JavaMail

    HI i am working on receiving a mail in java console. wen i receive mails based on the search confined to the string "Status" in the subject line, i get all the mails, which were previously read. How to mark the mails as unread and read only the unread mails the next time i run the program...? here is my code.

    import java.util.*;
    import java.io.*;
    import javax.mail.*;
    import javax.mail.search.*;


    public class MessageRead {

    public static void receive(String smtpHost, String user,
    String password, String subjectSubstringToSearch){
    Session session = Session.getInstance(new Properties());

    try {
    Store store = session.getStore("pop3");
    store.connect(smtpHost, user, password);
    // Get "INBOX"
    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_WRITE);
    int count = folder.getMessageCount();
    System.out.println(count + " total messages");

    // Search for e-mails by some subject substring
    String pattern = subjectSubstringToSearch;
    SubjectTerm subterm = new SubjectTerm(pattern);
    Message [] found = folder.search(subterm);

    System.out.println(found.length +
    " messages matched Subject pattern \"" +
    pattern + "\"");

    for (int integer = 0; integer < found.length; integer++) {
    Message message = found[integer];
    Date date = message.getSentDate();
    Address [] from = message.getFrom();
    String subj = message.getSubject();
    String mimeType = message.getContentType();
    System.out.println(date + "\t" + from[0] + "\t" +
    subj + "\t" + mimeType);
    //here add the code for flagging


    /*if (message[i].isSet(Flags.Flag.SEEN)) {
    System.out.println("Read");
    } */
    Object o = message.getContent();
    if (o instanceof String) {
    System.out.println("--This is a String Body part--");
    System.out.println((String)o);
    }
    else if (o instanceof Multipart) {
    System.out.println("--This is a Multipart Message--");
    Multipart multipart = (Multipart)o;
    int count3 = multipart.getCount();
    System.out.println("It has " + count3 +
    " BodyParts in it!");
    for (int j = 0; j < count3; j++) {
    BodyPart b = multipart.getBodyPart(j);
    /*System.out.println( "BodyPart " + (j + 1) +
    " is of MimeType " + mimeType); */

    Object o2 = b.getContent();
    if (o2 instanceof String) {
    System.out.println("--This the mail's Body--");
    System.out.println((String)o2);
    }

    }
    }


    }

    folder.close(true);
    store.close();

    }
    catch (MessagingException messageexception) {
    messageexception.printStackTrace();
    }
    catch (IOException ioexception) {
    ioexception.printStackTrace();
    }

    }

    public static void main(String[] args) {
    receive("mail.emailsrvr.com", "vidya_n@xyz.net",
    "passwrd", "Status");
    }
    }

  2. #2
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    364
    Rep Power
    5

    Default

    Hmh seems like there are a lot of limitations on flags issues,
    so you will have to do many things manually.

    Study this:

    com.sun.mail.pop3 (JavaMail API documentation)

    As you can see:
    "...many of the JavaMail API capabilities like event notification, folder management, flag management, etc. are not allowed..."

    "...POP3 supports no permanent flags...It's up to the application to determine which messages in a POP3 mailbox are "new"...

    ".. There are several strategies to accomplish this, depending on the needs of the application and the environment: "

    Here is some code to get you starting if you choose 2nd approach - using UID:

    Java Code:
    import java.util.Properties;
    
    import javax.mail.Folder;
    import javax.mail.Message;
    import javax.mail.Session;
    import javax.mail.Store;
    
    /**
     * POP3 client 
     * Read messages from INBOX
     * reads messages's UID
     * @author dren
     *
     */
    public class Pop3Client {
    
    	
    	 public static void main(String[] args) throws Exception {
    
    		    Properties props = new Properties();
    
    		    //please provide your settings here:
    		    String host = "pop3.someisp.com";
    		    String username = "username@isp.com";
    		    String password = "mypass";
    		    String provider = "pop3";
    
    		    
    		    
    
    		    Session session = Session.getDefaultInstance(props, null);
    		    
    		    //verbose - set this to true see communication between server and client !!!
    		    session.setDebug(true);
    		    
    		    Store store = session.getStore(provider);
    		    store.connect(host, username, password);
    
    		    
    		    Folder inbox = store.getFolder("INBOX");
    		    if (inbox == null) {
    		      System.out.println("No INBOX");
    		      System.exit(1);
    		    }
    		    inbox.open(Folder.READ_ONLY);
    
    		    Message[] messages = inbox.getMessages();
    		    for (int i = 0; i < messages.length; i++) {
    		      System.out.println("Message " + (i + 1));
    		      messages[i].writeTo(System.out);
    		      
    		      /**/
    		      if (inbox instanceof com.sun.mail.pop3.POP3Folder) {
    			        com.sun.mail.pop3.POP3Folder pf =
    			            (com.sun.mail.pop3.POP3Folder)inbox;
    			        String uid = pf.getUID( messages[i]);
    			        if (uid != null){
    			        	System.out.println("*****************"); // use it
    			        	System.out.println(uid); // use it
    			        	System.out.println("*****************"); // use it
    			        }
    			            
    			    }
    		      
    		      /**/
    		    }
    		    inbox.close(false);
    		    store.close();
    		  }
    }

    From what I see main point is:
    "...All approaches will require some permanent storage associated with the client...."

    So maybe this is scenario that could work:
    1. connect to pop3 sever initially
    2. get all messages and their UID's
    3. put UID in some permanent list and store them somewhere

    4. connect to pop3 for 2nd, 3th...nth time
    5. load all previous UIDS
    6. get all messages, iterate, and check if messages UID is your permanent UID list, if it is skip, else take it

    Conclusion:
    I don't see anything that API offers you to get this automatically.
    Code and scenario i posted are far from optimal, but i really hope
    that will help you in your further research

    good luck :)

  3. #3
    vidya is offline Member
    Join Date
    Jan 2010
    Posts
    9
    Rep Power
    0

    Default

    Thanks a lot for ur kind response. It was very helpful and m getting a few ideas also regarding this. Thanks again. :)

  4. #4
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    364
    Rep Power
    5

    Default

    Please share your experience with rest of us on forum
    once you solve this

    thanx in advance :)

Similar Threads

  1. Javamail
    By johniem in forum New To Java
    Replies: 1
    Last Post: 01-29-2010, 03:24 PM
  2. JavaMail Jar
    By rummy in forum Advanced Java
    Replies: 1
    Last Post: 01-21-2010, 03:51 PM
  3. Javamail
    By v_mallikarjun in forum Advanced Java
    Replies: 14
    Last Post: 04-18-2008, 07:32 PM
  4. Using JavaMail API
    By consult4u in forum Networking
    Replies: 0
    Last Post: 08-09-2007, 10:15 AM
  5. This is Regarding JSP with javamail
    By venkatkomalli in forum Advanced Java
    Replies: 3
    Last Post: 07-19-2007, 02:07 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •