I am trying to fetch mails for a one particular date from IBM domino, but Java mails Search Term is not working. I am able to fetch all the mails in the inbox, but I want to fetch only for one particular date.
here's my code
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import javax.mail.*;
import javax.mail.search.*;
import java.text.*;
public class MyClass {
public static void FetchMails(String date) {
String host = "<IP of Host>";
String username = "<username>";
String password = "<password>";
Properties properties = new Properties();
properties.setProperty("mail.store.protocol", "imaps");
properties.setProperty("mail.imaps.host", host);
properties.setProperty("mail.imaps.port", "143");
try {
Session emailSession = Session.getInstance(properties,null);
Store emailStore = emailSession.getStore("imap");
emailStore.connect(host, username, password);
//Getting Inbox folder
Folder emailFolder = emailStore.getFolder("INBOX");
emailFolder.open(Folder.READ_ONLY);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date targetDateObj = sdf.parse(date);
SearchTerm searchTerm =
new SentDateTerm(ComparisonTerm.EQ, targetDateObj);
Message[] messages = emailFolder.search(searchTerm);
System.out.println("No Of Mails Fetched : "+messages.length);
//closing
emailFolder.close(false);
emailStore.close();
}
catch(NoSuchProviderException nspe) {
System.out.println(nspe.getMessage());
nspe.printStackTrace();
}
catch(MessagingException me){
System.out.println(me.getMessage());
System.out.println(me.toString());
me.printStackTrace();
}
catch (ParseException pe) {
System.out.println(pe.getMessage());
pe.printStackTrace();
}
catch(Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
FetchMails("31-08-2022");
}
}
I am expecting that instead of fetching all the mails in the inbox, The code only fetches the mails received on one particular date.
I have tried using properties.setProperty("mail.debug", "true");
but it seems like the code runs through all the mails but doesn't fetch any mail. I am getting zero fetched mails