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