在 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()
}

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

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

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

> @Ondrej 根据Apple文档"Cocoa流编程指南简介",其中提到NSStream(目标C(/流(Swift(类不支持连接到iOS上的远程主机。您的替代方法是利用 CFStream 和 NSStream 之间的免费网桥来使用 CFStream,将您的 CFStreams 投射到 NSStreams。

下面是如何使用 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];

希望这有帮助。

最新更新