2015-10-01 121 views
2

我正在嘗試使用Alamofire和Mailgun發送電子郵件,但無法使其工作。如何使用Alamofire和Mailgun使用Swift發送電子郵件?

我使用的Mailgun沙箱和Alamofire是corectly增加,但是當我運行此代碼:

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 


    let key = "my correct key" 

    let parameters = [ 
     "Authorization" : "api:my correct key", 
     "from": "sender email", 
     "to": "destination email", 
     "subject": "Test", 
     "text": "Testing email send" 
    ] 

    let r = Alamofire.request(.POST, "https://api.mailgun.net/v3/<my sandbox>/messages", parameters:parameters) 
     .authenticate(user: "api", password: key) 
     .response { (request, response, data, error) in 
      print(request) 
      print(response) 
      print(error) 
    } 
    print(r) 
} 

我得到這個錯誤日誌:

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}) 
+0

您是否找到解決方案? – Nico

回答

1

問題是,蘋果的新應用程序傳輸安全(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,選擇「打開爲」 - >「源代碼」,然後之前的最後粘貼此<\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> 
+0

我向plist補充說,但得到了同樣的錯誤。 –

相關問題