OGeek|极客世界-中国程序员成长平台

标题: ios - 在 Swift 3 中使用 Stream 打开到 SMTP 服务器的套接字连接 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:23
标题: ios - 在 Swift 3 中使用 Stream 打开到 SMTP 服务器的套接字连接

我需要连接到 SMTP 服务器并且无法打开连接。握手失败(缺少身份验证)是我到达这里的最远距离。我打开了一个普通服务器的套接字,但在这里没有这样做来发送电子邮件。

    private func connect() throws {
        var input: InputStream? = nil
        var output: OutputStream? = nil

        Stream.getStreamsToHost(withName: server, port: port, inputStream: &input, outputStream: &output)

        guard let inputSafe = input, let outputSafe = output else {
            throw FailerError.unableToConnectToHost
        }

        self.inputStream = inputSafe
        self.outputStream = outputSafe

        // TODO: Authentication using login

        // Enable SSL/TLS on the streams
//        inputStream!.setProperty(kCFStreamSocketSecurityLevelNegotiatedSSL, forKey: Stream.PropertyKey(rawValue: kCFStreamPropertySocketSecurityLevel as String))
//        outputStream!.setProperty(kCFStreamSocketSecurityLevelNegotiatedSSL, forKey: Stream.PropertyKey(rawValue: kCFStreamPropertySocketSecurityLevel as String))
//        
//        // Define custom SSL/TLS settings
//        let sslSettings: [NSString : Any] = [
//             NSStream automatically sets up the socket, the streams and creates a trust object and evaulates it before you even get a chance to check the trust yourself. Only proper SSL certificates will work with this method. If you have a self signed certificate like I do, you need to disable the trust check here and evaulate the trust against your custom root CA yourself.
//            NSString(format: kCFStreamSSLValidatesCertificateChain): kCFBooleanFalse,
//            //
//            NSString(format: kCFStreamSSLPeerName): kCFNull,
//            // We are an SSL/TLS client, not a server
//            NSString(format: kCFStreamSSLIsServer): kCFBooleanFalse,
//            
//            NSString(format: kCFStreamSocketSecurityLevelNegotiatedSSL): kCFBooleanTrue
//        ]
//        
//        // Set the SSL/TLS settingson the streams
//        inputStream!.setProperty(sslSettings, forKey: Stream.PropertyKey(rawValue: kCFStreamPropertySSLSettings as String))
//        outputStream!.setProperty(sslSettings, forKey: Stream.PropertyKey(rawValue: kCFStreamPropertySSLSettings as String))


        inputStream.delegate = self
        outputStream.delegate = self

        inputStream.schedule(in: .main, forMode: .commonModes)
        outputStream.schedule(in: .main, forMode: .commonModes)

        inputStream.open()
        outputStream.open()
    }

评论部分显示我试图设置一些东西,但不知道应该在那里。请问大家有什么想法吗?

澄清一下,这是针对使用 SMTP 接收数据的糟糕设计的 API(不是我的想法,但必须使用它),所以请不要建议 MessageUI,否则应用程序会因为直接发送电子邮件而被拒绝

如果有人感兴趣,这里有 Playground 的完整要点: https://gist.github.com/rafiki270/c004b92deca437934f702efd3508bd83



Best Answer-推荐答案


@Ondrej 根据 Apple 文档“Cocoa 流编程指南简介”,提到 NSStream (Objective C)/Stream (Swift) 类不支持连接到 iOS 上的远程主机。您的替代方法是通过利用 CFStream 和 NSStream 之间的免费桥接器将 CFStreams 转换为 NSStreams 来使用 CFStream。

以下是有关如何使用 CFStream 进行操作的示例。

CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)[website host], 80, &readStream, &writeStream);

NSInputStream *inputStream = (__bridge_transfer NSInputStream *)readStream;
NSOutputStream *outputStream = (__bridge_transfer NSOutputStream *)writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];

希望这会有所帮助。

关于ios - 在 Swift 3 中使用 Stream 打开到 SMTP 服务器的套接字连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45404050/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4