菜鸟教程小白 发表于 2022-12-11 18:23:50

ios - 在 Swift 3 中使用 Stream 打开到 SMTP 服务器的套接字连接


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

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

      Stream.getStreamsToHost(withName: server, port: port, inputStream: &amp;input, outputStream: &amp;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: = [
//             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()
    }
</code></pre>

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

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

<p>如果有人感兴趣,这里有 Playground 的完整要点:
<a href="https://gist.github.com/rafiki270/c004b92deca437934f702efd3508bd83" rel="noreferrer noopener nofollow">https://gist.github.com/rafiki270/c004b92deca437934f702efd3508bd83</a> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>@Ondrej 根据 Apple 文档“Cocoa 流编程指南简介”,提到 NSStream (Objective C)/Stream (Swift) 类不支持连接到 iOS 上的远程主机。您的替代方法是通过利用 CFStream 和 NSStream 之间的免费桥接器将 CFStreams 转换为 NSStreams 来使用 CFStream。</p>

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

<pre><code>CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef), 80, &amp;readStream, &amp;writeStream);

NSInputStream *inputStream = (__bridge_transfer NSInputStream *)readStream;
NSOutputStream *outputStream = (__bridge_transfer NSOutputStream *)writeStream;
;
;
forMode:NSDefaultRunLoopMode];
forMode:NSDefaultRunLoopMode];
;
;
</code></pre>

<p>希望这会有所帮助。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在 Swift 3 中使用 Stream 打开到 SMTP 服务器的套接字连接,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/45404050/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/45404050/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在 Swift 3 中使用 Stream 打开到 SMTP 服务器的套接字连接