我需要连接到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
答案 0 :(得分:0)
@Ondrej根据Apple文档“Cocoa流编程指南简介”,提到NSStream(Objective C)/ Stream(Swift)类不支持连接到iOS上的远程主机。您的备用方法是利用CFStream利用CFStream和NSStream之间的免费网桥将CFStream转换为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];
希望这有帮助。