Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How use idleStart? #31

Closed
cjamcu opened this issue Apr 30, 2020 · 4 comments
Closed

How use idleStart? #31

cjamcu opened this issue Apr 30, 2020 · 4 comments
Assignees
Labels
documentation Improvements or additions to documentation imap question Further information is requested

Comments

@cjamcu
Copy link

cjamcu commented Apr 30, 2020

How can use listener when arrived new mails?. I see in log when arrived mails but haven't callback. Help me to implementation idle Start correctly.

@robert-virkus robert-virkus self-assigned this May 1, 2020
@robert-virkus robert-virkus added documentation Improvements or additions to documentation imap question Further information is requested labels May 1, 2020
@robert-virkus
Copy link
Member

robert-virkus commented May 2, 2020

Hey Carlos, thanks for testing enough_mail!

You need to listen for events using the ImapClient.eventBus. In the following I assume that you are interested in new messages.

imapClient.eventBus.on<ImapEvent>().listen(_onImapEvent);

When you start the IDLE mode, do not wait for its return.
I you are using the pedantic library (which I highly recommend), call idleStart() like this:

import 'package:pedantic/pedantic.dart';
[...]
unawaited(imapClient.idleStart());

If you are not using pedantic, then just do not wait for the future:

imapClient.idleStart();

Note that you need to have selected a folder before you can start the IDLE mode.

When you receive either an EXISTS or a FETCH event, you have to stop the idle mode, fetch your message and go back into the idle mode:

void _onImapEvent(ImapEvent event) async {
    int messageSequenceId;
    if (event is ImapFetchEvent) {
      messageSequenceId = event.messageSequenceId;
    } else if (event is ImapMessagesExistEvent) {
      messageSequenceId = event.newMessagesExists;
    }
    if (messageSequenceId != null) {
      imapClient.idleDone();
      var fetchContents =
          '(BODY.PEEK[HEADER.FIELDS (message-Id references subject from to cc date content-type content-transfer-encoding)])';
      var fetchHeaderResponse =
          await imapClient.fetchMessages(messageSequenceId, -1, fetchContents);
      if (fetchHeaderResponse.isOkStatus) {
        var allMessages = fetchHeaderResponse.result;
        for (var mimeMessage in allMessages) {
          print('got new message ${mimeMessage.decodeSubject()}');
        }
      }
      unawaited(imapClient.idleStart());
    }
  }

Hope this helps,
yours
Robert

@cjamcu
Copy link
Author

cjamcu commented May 2, 2020

Thank you very much for answering, I am trying to adjust this to my case but now every time I do the fetchMessage it gives an error. Can you help me see what is happening or help to add this to the example that is on github.

Again thank you very much.
2020-05-02 06 08 55

@robert-virkus
Copy link
Member

Hi Carlos, have you been able to fetch messages before? Please turn on verbose logging by creating an ImapClient with the following code:

var imapClient = ImapClient(isLogEnabled: true);

In the log you should now see all client and server messages in more detail.

It seems to be me that the imap server is not understanding the fetch command, maybe you need to play with the fetchContents definition.

If you can solve it by yourself, please post the solution - and if not please post the log output.

@robert-virkus
Copy link
Member

The high level MailClient API now also shows how to use IDLE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation imap question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants