Friday, July 31, 2009

Qt4 Imap API

Pushed in my GitHub repo the first draft of the Qt4 Imap (RFC 3501 Interne Message Access Protocol) Library.
I've implemented a sync communication because it's much easier to use, so if you want to use it in async way you need to implement a thread class that wrap the Imap class and throws the events that you need.


Imap imap;
if (!imap.connectToHost(IMAP_HOST, IMAP_PORT, IMAP_USE_SSL))
IMAP_MAIN_ABORT("connectToHost()", imap.errorString());

if (!imap.login(IMAP_USERNAME, IMAP_PASSWORD, IMAP_LOGIN_TYPE))
IMAP_MAIN_ABORT("login()", imap.errorString());

ImapMailbox *mailbox = imap.select("INBOX");
qDebug() << "INBOX";
qDebug() << " - Exists:" << mailbox->exists();
qDebug() << " - Unseen:" << mailbox->unseen();

QList<int> messageList = imap.searchRecentUnseen();
imap.fetch(mailbox, messageList);
foreach (int msgId, messageList) {
ImapMessage *message = mailbox->findById(msgId);
if (message == NULL) continue;

imap.fetchBodyStructure(message);

qDebug() << "FROM" << message->fromAddress().toString();
foreach (ImapAddress address, message->toAddresses())
qDebug() << " - TO" << address.toString();
qDebug() << "SUBJECT" << message->subject();

for (int i = 0; i < message->bodyPartCount(); ++i) {
ImapMessageBodyPart *bodyPart = message->bodyPartAt(i);
imap.fetchBodyPart(message, i);

qDebug() << bodyPart->isAttachment() << bodyPart->contentType();
qDebug() << bodyPart->data();
}
}

delete mailbox;

imap.logout();
imap.disconnectFromHost();

2 comments:

  1. Ciao Matteo,
    again from you here comes some really useful stuff, and with a nice API attached ;-)
    Take Care!

    ReplyDelete