2

I am using the javamail api to make a Search option which Searches the Emails in the Gmail Folder on the Basis of a Keyword present in the Subject of the Messages Here is the Code that i am Using

public class EGMail7 {
public static void main(String args[])
{
    Scanner sc = new Scanner(System.in);
    final String m10 = "abc@gmail.com";   
    final String n10 = "12345";
    string host = "smtp.gmail.com"; 
    try
    {
    Properties pro1 = new Properties();   
    pro1.put("mail.smtp.host", "smtp.gmail.com");    
    pro1.put("mail.smtp.socketFactory.port", "465");
    pro1.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");  
    pro1.put("mail.smtp.auth", "true");   
    pro1.put("mail.smtp.port", "465");  
Session session = Session.getDefaultInstance(pro1, 
        new javax.mail.Authenticator()                  
{ 
    protected PasswordAuthentication getPasswordAuthentication()
{
        return new PasswordAuthentication(m10,n10);
        }
    }); 
    Store store = session.getStore("imaps");  
    store.connect(host, m10, n10); 
    Folder folderbox = store.getFolder("INBOX");
    folderbox.open(Folder.READ_ONLY);   
    SearchTerm search = new SearchTerm(){

        @Override
        public boolean match(Message message) {
            try
            {
                if(message.getSubject().contains([Keyword]))  
                {
                    return true;
                }
            }
            catch(Exception e)
            {
                System.err.println(e.getMessage());
            }
            return false;
        }

    };

    Message[] found = folderbox.search(search);

    int length = found.length;
    for(int i = 0;i<found.length;i++)
    {
        Message mess1 = found[i];
        System.out.println("->Message Number > "+i);
        System.out.println("->Message Subject >"+mess1.getSubject());
    }
    folderbox.close(true);
    store.close();
}
    catch(Exception e)
    {
        System.err.println(e.getMessage());
    }
   }
  }

I am using the Eclipse IDE and the problem i am facing is that whatever keyword I pass in the SearchTerm Method I always end up getting null Exception and an Message[] Found Array . I used Stack Trace to find out the Problem of this NullPointer Exception and it is at the line

Message[] found = folderbox.search(search);

I cannot Seem to understand what is the Problem Here ?

Regards, Thank you

PS - and if someone could also please post the corrected code it would be great Thanks

Also When i Am Just Adding the Keyword Directly in the SearchTerm it give an Error like

SearchTerm searchCon = new SearchTerm([Keyword]);

There are 2 Errors 1.Cannot Instantiate the SearchTerm 2.The Serializable class does not Declare a static final Serial Verison UID field of Type Long

So I Cannot Understand what is the Mistake here

Radhesh Khanna
  • 141
  • 3
  • 11
  • And inside `.match()`, `message.getSubject()` does indeed return non-null for every message? – dhke Jul 05 '17 at 06:44
  • The Stack Trace at the inner Try catch inside the `.match()` is giving the NullPointer Exception and Pointing to the line `Message[] found = folderbox.search(search);` – Radhesh Khanna Jul 05 '17 at 07:01
  • But i do not seem to understand whatever Keywords that i am passing inside the `.match()` and `message.getSubject()` are present in my INBOX in gmail and there are mails which actually contain the Keyword in their Subject – Radhesh Khanna Jul 05 '17 at 07:02
  • Can you please correct '(i+1)' . Your 'i' is increasing two by two. – newuserua_ext Jul 05 '17 at 07:11
  • Yes,Thank you for Pointing that out It has been Corrected – Radhesh Khanna Jul 05 '17 at 07:14
  • I even Tried Using the pop3 server instead of the imaps server which only has the Inbox folder The result was still the same NullPointer Exception at the same line and i still couldnt understand why – Radhesh Khanna Jul 05 '17 at 07:31

2 Answers2

0

You got exception because you have mail without subject. Statement should be corrected with ;

if(message.getSubject() != null && message.getSubject().contains([Keyword]))  
newuserua_ext
  • 577
  • 6
  • 18
  • Thank you So much for the Code , I am Not getting any errors now but it still cant seem to find my Emails and it takes atleast 15 minutes to return the Result – Radhesh Khanna Jul 06 '17 at 06:06
  • Any Suggestions on how the Program can give Results for the Desired Keyword because Subject containing that Keyword exists in the Inbox , Thank you – Radhesh Khanna Jul 06 '17 at 06:07
  • Because your inbox size is too big. Maybe better way to create multi thread search with specific date interval. Hope this https://stackoverflow.com/questions/870045/java-imap-fetch-messages-since-a-date?lq=1 answer will help you. – newuserua_ext Jul 06 '17 at 06:14
  • I will try out and let you Know – Radhesh Khanna Jul 06 '17 at 08:36
  • Thank You So much for the Link the Search Worked Perfectly and it was also Fast compared to Defining a Custom Search Term – Radhesh Khanna Jul 20 '17 at 13:40
  • Gmail [imap](https://code.google.com/archive/p/java-gmail-imap/) search w.r.t SubjectTerm Ex:[1](https://www.javatips.net/api/javax.mail.search.subjectterm#Example_5) [2](https://www.programcreek.com/java-api-examples/index.php?api=javax.mail.search.SubjectTerm) or [range of Dates](https://stackoverflow.com/a/870658/5081877), [localized folder names](https://stackoverflow.com/a/53387370/5081877) – Yash May 22 '19 at 10:47
0

Your code is full of these common JavaMail mistakes, please correct them.

There's no need to create your own custom SearchTerm, just use SubjectTerm:

SearchTerm search = new SubjectTerm(keyword);
Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
  • Thank you for the Link it was really helpful but is the response Time Faster than the Previous Approach because it takes at least 10 mins to come up with an output ? – Radhesh Khanna Jul 07 '17 at 05:18
  • and currently this does not seem to work in the Program , It gives an error when i remove the custom SearchTerm and directly add the keyword , Please Advice ?? Also if you could Write the Code and give it would be great Thanks – Radhesh Khanna Jul 07 '17 at 11:29
  • Yes, it's much faster because the search is done on the IMAP server instead of the client. Exactly what error are you getting? – Bill Shannon Jul 07 '17 at 16:49
  • Sorry for the Late reply but the SearchTem is not directly Accepting the KeyWord and it is demanding for a custom Search term which returns a boolean value , I Tried to get it to work but it wont accept the Keyword , I am using Eclipse IDE if it makes any difference – Radhesh Khanna Jul 10 '17 at 05:49
  • What exactly does "is not directly accepting" mean? What exactly are you doing? Update your post with the new code and the exact error message. – Bill Shannon Jul 10 '17 at 06:56
  • The Question has been Edited with the Errors and also the Snippet that i am Using – Radhesh Khanna Jul 10 '17 at 13:24
  • First, `[Keyword]` is not valid Java syntax. You need to use a constant String `"Keyword"` or a variable String `keyword`. What version of JavaMail are you using? You should be using at least [version 1.5.6](https://javaee.github.io/javamail/#Download_JavaMail_Release). – Bill Shannon Jul 10 '17 at 18:09
  • Yes sir The Keyword is being used as the Way specified by you, but the Version is old. I have updated the latest Version of the javamail api – Radhesh Khanna Jul 11 '17 at 05:31
  • Did that fix your problems? – Bill Shannon Jul 11 '17 at 17:57
  • Sorry Sir, It is Still Not recognizing the SearchTerm and Gives the same 2 Errors that have been mentioned in the Question even after Updating the Javamail api – Radhesh Khanna Jul 20 '17 at 13:15
  • I was able to Execute the Search of the Emails on the Basis of the date and No other Field of the Email Thanks to newuserua_ext Link that he Provided – Radhesh Khanna Jul 20 '17 at 13:41
  • You're still doing something wrong, but unless you update the code in your original post, I can't tell what it is you're doing wrong. – Bill Shannon Jul 20 '17 at 19:03