无法快速连接到 POP3 服务器

问题描述 投票:0回答:1

我正在尝试使用 Swift 连接到 POP3 服务器。这是我的代码,我使用了InputStream和OutputStream。

import Foundation


class Pop3Client{
    var inputStream: InputStream!
    var outputStream: OutputStream!
    
    init(host: String,port: Int){
        Stream.getStreamsToHost(withName: host, port: port, inputStream: &inputStream, outputStream: &outputStream)
         if inputStream == nil || outputStream == nil {
        // Handle error, e.g., print an error message and return
        print("Failed to initialize streams.")
        return
    }
    guard inputStream.streamStatus == .open else {
    print("Input stream is not open.")
    return
}
        inputStream.schedule(in: .current, forMode: .default)
        outputStream.schedule(in: .current, forMode: .default)
        inputStream.open()
        outputStream.open()
    
    }
    
     deinit {
        inputStream.close()
        outputStream.close()
    }
    
    func sendCommand(command: String){
          let data = "\(command)\r\n".data(using: .utf8)!
        let bytes = data.withUnsafeBytes { outputStream.write($0.baseAddress!.assumingMemoryBound(to: UInt8.self), maxLength: data.count) }
        print("WRITTEN \(bytes)")
         usleep(1000000)
    }
    
    
     func readResponse() -> String {
    var buffer = [UInt8](repeating: 0, count: 4096)
    var response = ""
    
    // Check if the input stream is open before reading
    guard inputStream.streamStatus == .open else {
        print("Input stream is not open.")
        return response
    }
    
    repeat {
        let bytesRead = inputStream.read(&buffer, maxLength: buffer.count)
        
        if bytesRead > 0 {
            if let str = String(bytes: buffer, encoding: .utf8) {
                response += str
            }
        } else if bytesRead == 0 {
            // No more data available to read
            break
        } else if bytesRead == -1 {
            // Handle error, e.g., print an error message
            if let error = inputStream.streamError {
                print("Error reading from stream: \(error)")
            } else {
                print("Unknown error reading from stream.")
            }
            break
        }
    } while inputStream.hasBytesAvailable
    
    return response
}

    
     func login(username: String, password: String) {
         sendCommand(command:"USER \(username)")
        print(readResponse())
        
         sendCommand(command:"PASS \(password)")
        print(readResponse())
    }
    
    func listMessages() {
        sendCommand(command:"LIST")
        print(readResponse())
    }
    
    func quit() {
        sendCommand(command:"QUIT")
        print(readResponse())
    }
}

let pop3Client = Pop3Client(host: "pop.mail.yahoo.com", port: 995)
pop3Client.login(username: "EMAIL_HERE", password: "PASSWORD_HERE")
pop3Client.listMessages()
pop3Client.quit()

这是我尝试连接到 yahoo POP3 服务器时的输出

输入流未打开。 书面-1 输入流未打开。

已写-1 输入流未打开。

已写-1 输入流未打开。

已写-1 输入流未打开。

ios swift foundation pop3
1个回答
0
投票

我认为这是因为没有“TLS”连接...

© www.soinside.com 2019 - 2024. All rights reserved.