Dear All, I'm working on an application that require a basic functionality of email like fetch emails and send emails only so I used imap and smtp but I have a bit of issues that if I put a custom search criteria it's not working and return
[log] IMAP failed with BAD Could not parse command```
my method to do this
```Future<List<Email>> fetchEmails({
required String domain,
required String userName,
required String password,
String? fromAddress,
DateTime? sinceDate,
DateTime? beforeDate,
DateTime? specificTimeStart,
DateTime? specificTimeEnd,
int maxMessageCount = 2,
}) async {
final imapServerHost = 'imap.$domain';
const imapServerPort = 993;
const isImapServerSecure = true;
final client = ImapClient(isLogEnabled: false);
List<Email> emails = [];
try {
await _connectToServer(
client, imapServerHost, imapServerPort, isImapServerSecure);
await client.login(userName, password);
await client.selectInbox();
String searchCriteria = 'UNSEEN';
if (sinceDate != null) {
final sinceStr = sinceDate.toUtc().toIso8601String().split('T').first;
searchCriteria = '$searchCriteria SINCE "$sinceStr"';
}
if (beforeDate != null) {
final beforeStr = beforeDate.toUtc().toIso8601String().split('T').first;
searchCriteria = '$searchCriteria BEFORE "$beforeStr"';
}
if (specificTimeStart != null && specificTimeEnd != null) {
final startStr =
specificTimeStart.toUtc().toIso8601String().split('T').last;
final endStr =
specificTimeEnd.toUtc().toIso8601String().split('T').last;
searchCriteria =
'$searchCriteria SENTSINCE "$startStr" SENTBEFORE "$endStr"';
}
log('searchCriteria is: $searchCriteria');
final fetchResult =
await client.searchMessages(searchCriteria: searchCriteria);
log('fetchResult_1 is: $fetchResult');
var messageIds = fetchResult.matchingSequence?.toList() ?? [];
if (messageIds.isEmpty) {
log('No messages found');
return emails;
}
messageIds = messageIds.take(maxMessageCount).toList();
for (var messageId in fetchResult.matchingSequence!.toList()) {
final fetchResult = await client.fetchMessage(messageId, 'BODY[]');
log('fetchResult is: $fetchResult');
emails.add(_parseMessage(fetchResult));
}
await client.logout();
} on ImapException catch (e) {
log('IMAP failed with $e');
}
return emails;
}```
I tried many formats like
searchCriteria is: UNSEEN SINCE "2024-08-03" BEFORE "2024-08-04" SENTSINCE "12:00:00.000Z" SENTBEFORE "14:00:00.000Z"
searchCriteria is: UNSEEN SINCE "2024-08-03" BEFORE "2024-08-04" SENTSINCE "12:00:00.000Z" SENTBEFORE "14:00:00.000Z"
searchCriteria is: UNSEEN SINCE 2024-08-03 BEFORE 2024-08-04 SENTSINCE 12:00:00.000Z SENTBEFORE 14:00:00.000Z
searchCriteria is: UNSEEN SINCE 2024-08-03 BEFORE 2024-08-04 SENTSINCE 12:00 SENTBEFORE 22:00
searchCriteria is: UNSEEN SINCE "2024-08-03" BEFORE "2024-08-04" SENTSINCE "12:00" SENTBEFORE "22:00"
searchCriteria is: UNSEEN SINCE "2024-08-03"
searchCriteria is: SINCE "2024-08-03"
but all returns same error ? could anyone help me!
Dear All, I'm working on an application that require a basic functionality of email like fetch emails and send emails only so I used imap and smtp but I have a bit of issues that if I put a custom search criteria it's not working and return
searchCriteria is: UNSEEN SINCE "2024-08-03" BEFORE "2024-08-04" SENTSINCE "12:00:00.000Z" SENTBEFORE "14:00:00.000Z" searchCriteria is: UNSEEN SINCE "2024-08-03" BEFORE "2024-08-04" SENTSINCE "12:00:00.000Z" SENTBEFORE "14:00:00.000Z" searchCriteria is: UNSEEN SINCE 2024-08-03 BEFORE 2024-08-04 SENTSINCE 12:00:00.000Z SENTBEFORE 14:00:00.000Z searchCriteria is: UNSEEN SINCE 2024-08-03 BEFORE 2024-08-04 SENTSINCE 12:00 SENTBEFORE 22:00 searchCriteria is: UNSEEN SINCE "2024-08-03" BEFORE "2024-08-04" SENTSINCE "12:00" SENTBEFORE "22:00" searchCriteria is: UNSEEN SINCE "2024-08-03" searchCriteria is: SINCE "2024-08-03"