Mailcore2 IMAP空闲操作:如何确保操作在每次收到新电子邮件时触发其回调?

时间:2015-08-11 18:43:13

标签: windows visual-c++ mailcore2

我正在使用here提供的Mailcore 2 windows dll。我正在使用Visual Studio 2015并尝试构建可视化c ++控制台应用程序。

根据讨论here,我正在使用IMAPIdle操作来收听Gmail上的INBOX文件夹。但是,它仅在启动后触发回调一次。连接记录器的最后一个条目如下:

9 IDLE
+ idling

这似乎表明IDLE操作仍然存在,但是当新邮件到达INBOX时,它永远不会再次触发回调。有人可以建议如何解决这个问题吗?我错过了什么吗?示例代码如下:

------------头文件-----------

#pragma once
#include "MailCore/Mailcore.h"
class IdleIMAPManager : public mailcore::Object, public mailcore::OperationCallback
{
public:
    mailcore::IMAPAsyncSession * idleSession;
    mailcore::IMAPIdleOperation * idleOperation;
    int lastKnownUid = 1;

    IdleIMAPManager(mailcore::IMAPAsyncSession * session, int lastKnownUid);
    ~IdleIMAPManager();
    void operationFinished(mailcore::Operation * op);
    void startIMAPInboxListener();
};

------------ CPP档案-------------

#include "stdafx.h"

IdleIMAPManager::IdleIMAPManager(mailcore::IMAPAsyncSession * session, int lastKnownUid) {
    if (session != nullptr) {
        IdleIMAPManager::idleSession = session;
        IdleIMAPManager::lastKnownUid = lastKnownUid;
        IdleIMAPManager::idleOperation = IdleIMAPManager::idleSession->idleOperation(MCSTR("INBOX"), IdleIMAPManager::lastKnownUid);
        IdleIMAPManager::idleOperation->setCallback(this);
    }
    else {
        std::cout << "IdleIMAPManager Error: Session is invalid\n";
    }
}


IdleIMAPManager::~IdleIMAPManager()
{
}

void IdleIMAPManager::operationFinished(mailcore::Operation * op) {
    printf("\n============ IMAP IDLE OPERATION FIRED =========\n");
}

void IdleIMAPManager::startIMAPInboxListener() {
    if (IdleIMAPManager::idleOperation == nullptr) {
        std::cout << "\nError: Inbox listener not created\n";
        exit(1);
    }
    else {
        IdleIMAPManager::idleOperation->start();
    }
}

class IdleLogger : public mailcore::ConnectionLogger {
    void log(void * sender, mailcore::ConnectionLogType logType, mailcore::Data * buffer) {
        std::string logfile = "C:\\Users\\...\\idlelog";
        std::ofstream log;
        log.open(logfile, std::ios::binary | std::ios::app);
        if (log.is_open()) {
            if (buffer != nullptr) {
                log.write(buffer->bytes(), buffer->length());
            }
            log.close();
        }
        else {
            std::cout << "\nLogger error: Could not open log file" << std::endl;
        }
    }
};

static void win32MainLoop(void)
{
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

int main(int argc, _TCHAR* argv[])
{
    mailcore::AutoreleasePool * pool = new mailcore::AutoreleasePool();
    mailcore::IMAPAsyncSession * session = new mailcore::IMAPAsyncSession();
    //Create IDLE Session
    session->setHostname(MCSTR("imap.gmail.com"));
    session->setPort(993);
    session->setUsername(MCSTR("******@gmail.com"));
    session->setPassword(MCSTR("*******"));
    session->setConnectionType(mailcore::ConnectionTypeTLS);
    IdleLogger * connectionLog = new IdleLogger();
    session->setConnectionLogger(connectionLog); 

    IdleIMAPManager idler = IdleIMAPManager(session, (int)420);
    idler.startIMAPInboxListener();
    win32MainLoop();
    pool->release();
    return 0;
}

--------------------连接记录器输出----------------

* OK Gimap ready for requests from xx.xx.xxx.xxx i50mb118414577qgi
1 CAPABILITY
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH2 AUTH=PLAIN AUTH=PLAIN-CLIENTTOKEN AUTH=XOAUTH
1 OK Thats all she wrote! i50mb118414577qgi
2 LOGIN "*******@gmail.com" "*******"
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH UTF8=ACCEPT LIST-EXTENDED LIST-STATUS
2 OK **********@gmail.com authenticated (Success)
3 COMPRESS DEFLATE
3 OK Success
4 ENABLE CONDSTORE
* ENABLED CONDSTORE
4 OK Success
5 NAMESPACE
* NAMESPACE (("" "/")) NIL NIL
5 OK Success
6 ID NIL
* ID ("name" "GImap" "vendor" "Google, Inc." "support-url" "http://support.google.com/mail" "version" "gmail_imap_150804.04_p0" "remote-host" "xx.xx.xxx.xxx")
6 OK Success
7 SELECT INBOX
* FLAGS (\Answered \Flagged \Draft \Deleted \Seen $NotPhishing $Phishing)
* OK [PERMANENTFLAGS (\Answered \Flagged \Draft \Deleted \Seen $NotPhishing $Phishing \*)] Flags permitted.
* OK [UIDVALIDITY 1] UIDs valid.
* 106 EXISTS
* 0 RECENT
* OK [UIDNEXT 444] Predicted next UID.
* OK [HIGHESTMODSEQ 36560]
7 OK [READ-WRITE] INBOX selected. (Success)
8 UID FETCH 420:* (UID)
* 83 FETCH (UID 420 MODSEQ (35048))
* 84 FETCH (UID 421 MODSEQ (35052))
* 85 FETCH (UID 422 MODSEQ (35055))
* 86 FETCH (UID 423 MODSEQ (35054))
* 87 FETCH (UID 424 MODSEQ (35050))
* 88 FETCH (UID 425 MODSEQ (35103))
* 89 FETCH (UID 426 MODSEQ (35147))
* 90 FETCH (UID 427 MODSEQ (35191))
* 91 FETCH (UID 428 MODSEQ (35236))
* 92 FETCH (UID 429 MODSEQ (35517))
* 93 FETCH (UID 430 MODSEQ (35797))
* 94 FETCH (UID 431 MODSEQ (35771))
* 95 FETCH (UID 432 MODSEQ (35745))
* 96 FETCH (UID 433 MODSEQ (35557))
* 97 FETCH (UID 434 MODSEQ (36078))
* 98 FETCH (UID 435 MODSEQ (36226))
* 99 FETCH (UID 436 MODSEQ (36225))
* 100 FETCH (UID 437 MODSEQ (36227))
* 101 FETCH (UID 438 MODSEQ (36550))
* 102 FETCH (UID 439 MODSEQ (36548))
* 103 FETCH (UID 440 MODSEQ (36546))
* 104 FETCH (UID 441 MODSEQ (36547))
* 105 FETCH (UID 442 MODSEQ (36549))
* 106 FETCH (UID 443 MODSEQ (36551))
8 OK Success
9 IDLE
+ idling

0 个答案:

没有答案