title: Open socket connection to SMTP server using Stream in Swift 3
body: <p>I need to connect to an SMTP server and failing open the connection. Failing handshake (missing authentication) is furthest I got here. I open the socket to a normal server but failing to do so in here to send an email.</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: [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()
      }
  </code></pre>
  
  <p>The commented part shows me trying to set some of the stuff but don't really know what is supposed to be there. Any ideas anyone please?</p>
  
  <p>Just to clarify, this is for a badly designed API that is using SMTP to receive data (not my idea but have to use it) so please don't suggest MessageUI or that the app will get rejected because of sending emails directly :)</p>
  
  <p>Full gist of the Playground is here if anyone is interested:
  <a href="https://gist.github.com/rafiki270/c004b92deca437934f702efd3508bd83" rel="nofollow noreferrer">https://gist.github.com/rafiki270/c004b92deca437934f702efd3508bd83</a></p>