Guest User

Untitled

a guest
Nov 1st, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. var imapsession:MCOIMAPSession = MCOIMAPSession()
  2.  
  3. func prepareImapSession()
  4. {
  5. // CONFIGURE THAT DEPENDING OF YOUR NEEDS
  6. imapsession.hostname = imapHostname // String
  7. imapsession.username = userName // String
  8. imapsession.password = password // String
  9. imapsession.port = portIMAP // UInt32 number
  10. imapsession.authType = MCOAuthType.saslLogin
  11. imapsession.connectionType = MCOAuthType.saslLogin
  12. }
  13.  
  14.  
  15. func useImapWithUIDS(startPoint start: UInt64, whatProtocol wProtocol: String)
  16. {
  17. // There is more than one option here, explore depending of your needs
  18. let requestKind : MCOIMAPMessagesRequestKind = .headers
  19.  
  20. let folder : String = "INBOX"
  21.  
  22. // 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
  23. let uids : MCOIndexSet = MCOIndexSet(range: MCORangeMake(1, UINT64_MAX))
  24.  
  25. let fetchOperation = imapsession.fetchMessagesOperation(withFolder: folder, requestKind: requestKind, uids: uids)
  26.  
  27. fetchOperation?.start
  28. { (err, msg, vanished) -> Void in
  29.  
  30. if (err != nil)
  31. {
  32. error = err
  33. NSLog((err?.localizedDescription)!)
  34. }
  35. else
  36. {
  37.  
  38. guard let msgs = msg as? [MCOIMAPMessage]
  39. else
  40. {
  41. print("ERROR GETTING THE MAILS")
  42. return
  43. }
  44.  
  45. for i in 0..<msgs.count
  46. {
  47. // THE SUBJECT
  48. let subject = msgs[i].header.subject
  49.  
  50. // THE uid for this email. The uid is unique for one email
  51. let uid = msgs[i].uid
  52.  
  53. // 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
  54. let sequenceNumber = msgs[i].sequenceNumber
  55.  
  56. }
  57. }
  58. }
  59.  
  60.  
  61. // MARK: - EXTRACT THE CONTENT OF ONE EMAIL, IN THIS FUNCTION YOU NEED THE uid, THE UNIQUE NUMBER FOR ONE EMAIL
  62. func useImapFetchContent(uidToFetch uid: UInt32)
  63. {
  64.  
  65. let operation: MCOIMAPFetchContentOperation = imapsession.fetchMessageOperation(withFolder: "INBOX", uid: uid)
  66. operation.start { (Error, data) in
  67. if (Error != nil)
  68. {
  69. NSLog("ERROR")
  70. return
  71. }
  72.  
  73. let messageParser: MCOMessageParser = MCOMessageParser(data: data)
  74.  
  75. // IF YOU HAVE ATTACHMENTS USE THIS
  76. let attachments = messageParser.attachments() as? [MCOAttachment]
  77.  
  78. // THEN YOU NEED THIS PROPERTIE, IN THIS EXAMPLE I TAKE THI FIRST, USE WHAT EVER YOU WANT
  79. let attachData = attachments?.first?.data
  80.  
  81. // FOR THE MESSAGEPARSER YOU CAN EPLORE MORE THAN ONE OPTION TO OBTAIN THE TEXT
  82. let msgPlainBody = messageParser.plainTextBodyRendering()
  83.  
  84. }
Add Comment
Please, Sign In to add comment