karastojko/mailio

[Question] Is there any way to get list of only unread messages via IMAP?

Closed this issue · 6 comments

Does it possible to get only "unread" messages from mailbox/folder via imap classes, cause for now the only way i see is to store sid/uid of the latest readed message and add it to search_condition before executing next "search" request, which is not reliable at all.

Not currently, but it's an option easy to add. Check the newly created branch, where I have experimented with this option. Then, it can be used with the following snippet:

conn.select(list<string>({"Inbox"}));
list<unsigned long> messages;
conn.search(list<imap::search_condition_t>({imap::search_condition_t(imap::search_condition_t::SID_LIST,
    list<imap::messages_range_t>{make_pair(1700, 1800)}), imap::search_condition_t::UNSEEN}), messages, false);

Option like this and few others can be added in the version 0.23, now I have to publish the version 0.22 which mostly deals with encodings. So, as the quick fix you can use the new branch until I add it to the master.

I've get an error "Search mailbox failure." during the execution of the following code:

std::string imap_addr = ..., login = ..., password = ...;
try {
   mailio::imaps conn(imap_addr, 143);
   auto result = conn.authenticate(login, password, mailio::imaps::auth_method_t::START_TLS);
   auto stat = conn.select(std::list<std::string>({ "Inbox" }));
   std::list<unsigned long> messages;
   std::list<mailio::imaps::search_condition_t> conds;
   conds.push_back(mailio::imaps::search_condition_t(mailio::imaps::search_condition_t::SUBJECT, "Test"));
   conds.push_back(mailio::imaps::search_condition_t::UNSEEN);
   conn.search(conds, messages, false);
   for (auto sid : messages) { std::cout << sid << " "; }
   std::cout << "Search ended" << std::endl;
}
catch (mailio::imap_error& exc) {std::cout << "Imap error: " << exc.what() << std::endl;}
catch (mailio::dialog_error& exc) {std::cout << "Imap dialog error: " << exc.what() << std::endl;}

Do you also get it? According to debugging, problem is that mailio::dialog::receive returns "BAD Command Argument Error".

I tried with Dovecot and works fine. Which IMAP server are you using?

For example i get this error from yandex mail. To save your time: imap server - imap.yandex.com, auth method - login, port - 993, do not forget to enable application access setting in mail options (options -> email clients)

Take the latest code from the branch and try again.

Yes, now it's working, thank you!