如何使用Alamofire和Mailgun使用Swift发送电子邮件?

时间:2015-10-01 19:09:02

标签: ios swift email alamofire mailgun

我正在尝试使用Alamofire和Mailgun发送电子邮件,但却无法让它发挥作用。

我正在使用Mailgun沙箱,并且在运行此代码时已经添加了Alamofire:

POST https://api.mailgun.net/v3/sandbox3fecb2a1fd334a66bae85d3ed7d4ea9c.mailgun.org/messages
2015-10-01 15:58:57.520 Email[7001:912455] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)
Optional(<NSMutableURLRequest: 0x7f9551722230> { URL: https://api.mailgun.net/v3/sandbox3fecb2a1fd334a66bae85d3ed7d4ea9c.mailgun.org/messages })
nil
Optional(Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo={NSURLErrorFailingURLPeerTrustErrorKey=<SecTrustRef: 0x7f9551483e30>, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9802, NSErrorPeerCertificateChainKey=<CFArray 0x7f95514858d0 [0x10f0f17b0]>{type = immutable, count = 3, values = (
0 : <cert(0x7f9551481f00) s: *.mailgun.net i: GeoTrust SSL CA>
1 : <cert(0x7f9551482990) s: GeoTrust SSL CA i: GeoTrust Global CA>
2 : <cert(0x7f9551483140) s: GeoTrust Global CA i: Equifax Secure Certificate Authority>
)}, NSUnderlyingError=0x7f9551708d90 {Error Domain=kCFErrorDomainCFNetwork Code=-1200 "(null)" UserInfo={_kCFStreamPropertySSLClientCertificateState=0, kCFStreamPropertySSLPeerTrust=<SecTrustRef: 0x7f9551483e30>, _kCFNetworkCFStreamSSLErrorOriginalValue=-9802, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9802, kCFStreamPropertySSLPeerCertificates=<CFArray 0x7f95514858d0 [0x10f0f17b0]>{type = immutable, count = 3, values = (
0 : <cert(0x7f9551481f00) s: *.mailgun.net i: GeoTrust SSL CA>
1 : <cert(0x7f9551482990) s: GeoTrust SSL CA i: GeoTrust Global CA>
2 : <cert(0x7f9551483140) s: GeoTrust Global CA i: Equifax Secure Certificate Authority>
)}}}, NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made., NSErrorFailingURLKey=https://api.mailgun.net/v3/sandbox3fecb2a1fd334a66bae85d3ed7d4ea9c.mailgun.org/messages, NSErrorFailingURLStringKey=https://api.mailgun.net/v3/sandbox3fecb2a1fd334a66bae85d3ed7d4ea9c.mailgun.org/messages, NSErrorClientCertificateStateKey=0})

我收到此错误日志:

Factorial

1 个答案:

答案 0 :(得分:1)

问题在于Apple的新App Transport Security(ATS)功能旨在提高安全性。由于您的应用程序正在尝试连接到不支持最新SSL技术(TLSv1.2)的HTTP服务器(api.mailgun.net),因此ATS会导致您的连接失败并出现此类错误。我遇到了同样的问题。

请参阅此文章:http://ste.vn/2015/06/10/configuring-app-transport-security-ios-9-osx-10-11/

如果您不想阅读,只需右键点击您的info.plist,选择&#34;打开为&#34; - &gt;&#34;源代码&#34;然后在最后<\dict>

之前粘贴它
<key>NSAppTransportSecurity</key>
<dict>
  <key>NSExceptionDomains</key>
  <dict>
    <key>api.maillgun.net</key>
    <dict>
      <!--Include to allow subdomains-->
      <key>NSIncludesSubdomains</key>
      <true/>
      <!--Include to allow HTTP requests-->
      <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
      <true/>
      <!--Include to specify minimum TLS version-->
      <key>NSTemporaryExceptionMinimumTLSVersion</key>
      <string>TLSv1.1</string>
    </dict>
  </dict>
</dict>