【问题标题】:I tried to connect and fetch mails using mailcore2 in iOS. But I got error我尝试在 iOS 中使用 mailcore2 连接和获取邮件。但我得到了错误
【发布时间】:2021-03-21 18:12:52
【问题描述】:

Error Domain=MCOErrorDomain Code=5 "无法使用当前会话的凭据进行身份验证。" UserInfo={NSLocalizedDescription=无法使用当前会话的凭据进行身份验证。}

我把这段代码放在我的项目中。

class ViewController: UIViewController {
  
  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    prepareImapSession()
  }
  
  var imapsession:MCOIMAPSession = MCOIMAPSession()
  var error : Error? = nil
  
  func prepareImapSession()
  {
    // CONFIGURE THAT DEPENDING OF YOUR NEEDS
    imapsession.hostname = "imap.gmail.com" // String
    imapsession.username = my email // String
    imapsession.password = password // String
    imapsession.port = 993 // UInt32 number
    imapsession.authType = MCOAuthType.saslLogin
    imapsession.connectionType = MCOConnectionType.TLS
    DispatchQueue.main.asyncAfter(deadline: .now() + 8) { [self] in
      self.useImapWithUIDS()
    }
    imapsession.connectOperation()
  }
  
  
  func useImapWithUIDS() {
    // There is more than one option here, explore depending of your needs
//    let kind = MCOIMAPMessagesRequestKind()
//    let headers = kind.union(MCOIMAPMessagesRequestKind.headers)
//    let request = headers.union(MCOIMAPMessagesRequestKind.flags)
    
    let requestKind: MCOIMAPMessagesRequestKind = [.headers, .flags]
    let folder : String = "INBOX"
    
    // HERE ALSO EXPLORE DEPENDING OF YOUR NEEDS, RANGE IT IS THE RANGE OF THE UIDS THAT YOU WANT TO FETCH, I SUGGEST TO YOU TO CHANGE THE  // NUMBER ONE IF YOU HAVE A LOWER BOUND TO FETCH EMAIL
    let uids : MCOIndexSet = MCOIndexSet(range: MCORangeMake(1, UINT64_MAX))
    
    let fetchOperation  = imapsession.fetchMessagesOperation(withFolder: folder, requestKind: requestKind, uids: uids)
    
    fetchOperation?.start
    { [self] (err, msg, vanished) -> Void in
      
      if (err != nil)
      {
        self.error = err
        NSLog((err?.localizedDescription)!)
      }
      else
      {
        
        guard let msgs = msg as? [MCOIMAPMessage]
        else
        {
          print("ERROR GETTING THE MAILS")
          return
        }
        
        for i in 0..<msgs.count
        {
          // THE SUBJECT
          let subject = msgs[i].header.subject
          
          // THE uid for this email. The uid is unique for one email
          let uid = msgs[i].uid
          
          self.useImapFetchContent(uidToFetch: uid)
          // The sequenceNumber like the nomber say it is the sequence for the emails in the INBOX from the first one                                     // (sequenceNumber = 1) to the last one , it not represent always the same email. Because if you delete one email then                              //next one will get the sequence number of that email that was deleted
          let sequenceNumber = msgs[i].sequenceNumber
          
        }
      }
    }
  }
  
  // MARK: - EXTRACT THE CONTENT OF ONE EMAIL, IN THIS FUNCTION YOU NEED THE uid, THE UNIQUE NUMBER FOR ONE EMAIL
  func useImapFetchContent(uidToFetch uid: UInt32) {
    
    let operation: MCOIMAPFetchContentOperation = imapsession.fetchMessageOperation(withFolder: "INBOX", uid: uid)
    operation.start { (Error, data) in
      if (Error != nil)
      {
        NSLog("ERROR")
        return
      }
      
      let messageParser: MCOMessageParser = MCOMessageParser(data: data)
      
      // IF YOU HAVE ATTACHMENTS USE THIS
      let attachments = messageParser.attachments() as? [MCOAttachment]
      
      // THEN YOU NEED THIS PROPERTIE, IN THIS EXAMPLE I TAKE THI FIRST, USE WHAT EVER YOU WANT
      let attachData = attachments?.first?.data
      
      // FOR THE MESSAGEPARSER YOU CAN EPLORE MORE THAN ONE OPTION TO OBTAIN THE TEXT
      let msgPlainBody = messageParser.plainTextBodyRendering()
      
    }
    
  }
}

我使用mailcore2 框架。我收到错误描述无法使用当前会话的凭据进行身份验证

【问题讨论】:

标签: ios swift imap mailcore2


【解决方案1】:

它可能与您帐户上的第二因素身份验证有关。它在Apple forum 上进行了描述。

【讨论】:

  • 检查您可能需要在收件箱中启用 pop3/imap 访问,否则默认情况下禁用
猜你喜欢
  • 2021-08-03
  • 1970-01-01
  • 2019-10-04
  • 1970-01-01
  • 1970-01-01
  • 2022-12-11
  • 2020-06-16
  • 1970-01-01
  • 2022-01-27
相关资源
最近更新 更多