如何在 swift 中使用 MCOIMAPSession 邮件核心

Posted

技术标签:

【中文标题】如何在 swift 中使用 MCOIMAPSession 邮件核心【英文标题】:how to use MCOIMAPSession mailcore in swift 【发布时间】:2016-06-29 15:12:12 【问题描述】:

大家好,我想在我的 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]


        )

请帮帮我,我很困惑,很累!!!

【问题讨论】:

【参考方案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")
        

【讨论】:

你是如何将库集成到你的swift项目中的,我找不到Podfile

以上是关于如何在 swift 中使用 MCOIMAPSession 邮件核心的主要内容,如果未能解决你的问题,请参考以下文章

如何在 kotlin native 中使用 swift 库?

如何在 Swift 中使用 Apple 地图 - 你必须使用 C 还是 Swift 可以?

如何在 Swift 中使用 MKPolylineView

如何在 Swift 中使用`syslog`

如何在 swift 项目中使用 TPKeyboardAvoiding?

如何在 Playground 中使用 Swift 包管理器