简体   繁体   English

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

[英]How to send email using Alamofire and Mailgun with Swift?

I am trying to send email using Alamofire and Mailgun but just can't get it working. 我正在尝试使用Alamofire和Mailgun发送电子邮件,但却无法使其正常运行。

I am using the Mailgun sandbox and Alamofire is added corectly but when I run this code: 我正在使用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)
}

I get this error log: 我收到此错误日志:

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

The problem is Apple's new App Transport Security (ATS) feature that is meant to improve security. 问题是Apple新的App Transport Security(ATS)功能旨在提高安全性。 Because your app is trying to connect to an HTTP server (api.mailgun.net) that doesn't support the latest SSL technology (TLSv1.2), ATS causes your connection to fail with such an error. 由于您的应用程序正在尝试连接到不支持最新SSL技术(TLSv1.2)的HTTP服务器(api.mailgun.net),因此ATS会导致您的连接失败并出现此类错误。 I had the same problem. 我有同样的问题。

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

If you don't want to read it, just right click on your info.plist, select "Open as"->"Source Code" and then paste this before the last <\\dict> 如果您不想阅读它,只需右键单击您的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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Mailgun在Swift中发送电子邮件 - How to send emails in Swift using Mailgun 如何在 swift 5 中使用 Alamofire multipartFormData 发送数据和图像 - How to send data along with an image using Alamofire multipartFormData in swift 5 如何使用swift中的POST方法使用参数和标头在Alamofire中发送请求 - How to send request in Alamofire with parameters and headers using POST method in swift 如何在Alamofire 4.5,Swift 4中的标头中发送`apikey`? - How to send `apikey` in header in Alamofire 4.5,Swift 4? 如何在 Swift 中将数组作为参数发送到 Alamofire - how to send array to Alamofire as a Paremeter in Swift 如何快速使用 Gmail api 发送电子邮件 - How send Email using Gmail api in swift IOS Swift Mailgun不发送电子邮件 - IOS Swift Mailgun not sending email 如何在Swift 3中使用Alamofire制作并发送字典数组(JSON格式)发送到服务器 - How Can I make & send array of dictionary (JSON Format ) send to server using Alamofire in swift 3 如何使用 swift 4 从数据中使用 Alamofire multipart 在 params 中发送数组 - How to send array in params using Alamofire multipart from data using swift 4 如何使用Alamofire 4在Swift 3中为JSON数据发送带有参数和正文的POST请求? - How to send POST request with both parameter and body for JSON data in Swift 3 using Alamofire 4?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM