如何在swift中使用MCOIMAPSession mailcore

时间:2016-03-14 14:17:05

标签: swift imap mailcore2

大家好我想在我的swift项目中使用MCOImapSession。 我可以将#import <MailCore/MailCore.h>添加到头文件中,我希望像这样使用此代码快速question

请指导我。

        MCOIMAPSession *session = [[MCOIMAPSession alloc] init];
        [session setHostname:@"imap.gmail.com"];
        [session setPort:993];
        [session setUsername:@"ADDRESS@gmail.com"];
        [session setPassword:@"123456"];
        [session setConnectionType:MCOConnectionTypeTLS];

        MCOIMAPMessagesRequestKind requestKind = MCOIMAPMessagesRequestKindHeaders;
        NSString *folder = @"INBOX";
        MCOIndexSet *uids = [MCOIndexSet indexSetWithRange:MCORangeMake(1, UINT64_MAX)];

        MCOIMAPFetchMessagesOperation *fetchOperation = [session fetchMessagesOperationWithFolder:folder requestKind:requestKind uids:uids];

        [fetchOperation start:^(NSError * error, NSArray * fetchedMessages, MCOIndexSet * vanishedMessages) {
            //We've finished downloading the messages!

            //Let's check if there was an error:
            if(error) {
                NSLog(@"Error downloading message headers:%@", error);
            }

            //And, let's print out the messages...
            NSLog(@"The post man delivereth:%@", fetchedMessages);
        }];

我在谷歌搜索太多了,我得到了这段代码:

let imapsession = MCOIMAPSession()
        imapsession.hostname = ""
        imapsession.port = 123
        imapsession.username = ""
        imapsession.password = ""
        imapsession.connectionType = MCOConnectionType.TLS

        let requestKind : MCOIMAPMessagesRequestKind = MCOIMAPMessagesRequestKind.Headers

        let folder : String = "INBOX"
        let uids : MCOIndexSet = MCOIndexSet(range: MCORangeMake(1, UINT64_MAX))

        let fetchOperation : MCOIMAPFetchMessagesOperation = imapsession.fetchMessagesOperationWithFolder(folder, requestKind: requestKind, uids: uids)

        fetchOperation.start({ (err: NSError!, fetchedMessages: [AnyObject]!, indexSet: MCOIndexSet!) -> Void in

            if err != nil {
                println("Error downloading message headers: \(err)")
            }

            println("Inbox contains \(fetchedMessages.count) emails.")

            let msgs = fetchedMessages as [MCOIMAPMessage]


        })

但是在运行时我在这行中遇到了很多错误:

fetchOperation.start({ (err: NSError!, fetchedMessages: [AnyObject]!, indexSet: MCOIndexSet!) -> Void in

            if err != nil {
                println("Error downloading message headers: \(err)")
            }

            println("Inbox contains \(fetchedMessages.count) emails.")

            let msgs = fetchedMessages as [MCOIMAPMessage]


        })
请帮助我,我很困惑,太累了! enter image description here

1 个答案:

答案 0 :(得分:1)

let imapsession = MCOIMAPSession()
        imapsession.hostname = ""
        imapsession.port = 123
        imapsession.username = ""
        imapsession.password = ""
        imapsession.connectionType = MCOConnectionType.TLS

        let requestKind : MCOIMAPMessagesRequestKind = MCOIMAPMessagesRequestKind.Headers

        let folder : String = "INBOX"
        let uids : MCOIndexSet = MCOIndexSet(range: MCORangeMake(1, UINT64_MAX))

        let fetchOperation : MCOIMAPFetchMessagesOperation = imapsession.fetchMessagesOperationWithFolder(folder, requestKind: requestKind, uids: uids)

        fetchOperation.start { (err, msg, vanished) -> Void in
            print("error from server \(err)")
            print("fetched \(msg?.count) messages")
        }