0
public class testemail {
java.lang.Comparable c[];
Properties properties = null;
private Session session = null;
private Store store = null;
private Folder inbox = null;
private String userName = "xxx@gmail.com";  //
private String password = "xxx";
public testemail() {

}

public void readMails() throws Exception {
    properties = new Properties();
    properties.setProperty("mail.host", "imap.gmail.com");
    properties.setProperty("mail.port", "995");
    properties.setProperty("mail.transport.protocol", "imaps");
    session = Session.getInstance(properties,
            new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(userName, password);
        }
    });


        store = session.getStore("imaps");
        store.connect();
        inbox = store.getFolder("INBOX");
        inbox.open(Folder.READ_ONLY);

        //Message messages[] = inbox.search(new FlagTerm(new Flags(Flag.SEEN), false));
        Message messages[]=inbox.getMessages();
    //  System.out.println("Number of mails = " + messages.length);




        for ( Message message : messages ) {
            System.out.println("Subject: "+ message.getSubject());
            if(message.getSubject().toString()=="Suppliers that match your search: suto in future mode")
            {
            Address[] from = message.getFrom();
            System.out.println("-------------------------------");
            System.out.println("Date : " + message.getSentDate());
            //System.out.println("From : " + from[0]);
            //System.out.println("Subject: " + message.getSubject());
            //System.out.println("Content :");

            Object content = message.getContent();
            Multipart multiPart = (Multipart) content;
            //procesMultiPart(multiPart);

            System.out.println("--------------------------------");
            }
            else
            {
                System.out.println("not found");
            }
        }
        inbox.close(true);
        store.close();


}

Output:

   Subject: Suppliers that match your search: Test 13 mar 2012 Automotive
   not found
   Subject: Suppliers that match your search: suto in future mode
   not found
   Subject: Suppliers that match your search: ttt
   not found
   Subject: Suppliers that match your search:
   not found
   Subject: Suppliers that match your search: 123
   not found
   Subject: Suppliers that match your search: Search1
   not found
   Subject: Suppliers that match your search: Jan 28 US by vani
   not found
   Subject: Sign-in attempt prevented
   not found
   Subject: Stay more organised with Gmail's inbox
   not found

I am trying to filter out email with the subject line :"Suppliers that match your search: suto in future mode" the above program is unable to locate it even thought the email is available. kIndly help :) thanks

Suraj Prasad
  • 241
  • 3
  • 24

2 Answers2

1

Try to replace == to .equals() method in if(message.getSubject().toString()=="Suppliers that match your search: suto in future mode")

FYI look for this answer, shortly it's about this: == tests for reference equality (whether they are the same object) while .equals() compares for value equality (whether they are logically "equal") and that's your case. So, if you want to test whether two strings have the same value you should use .equals().

Community
  • 1
  • 1
solar
  • 1,344
  • 1
  • 8
  • 14
0

I noticed while skimming over your code that you wrote

if(message.getSubject().toString()=="Suppliers that match your search: suto in future mode")

When programming in Java, comparing strings should be done with the ".equals()" method rather than "==".

Dylan Meeus
  • 5,696
  • 2
  • 30
  • 49