如何在Swift中发送带有Amazon SES的电子邮件?

时间:2015-07-01 13:04:15

标签: swift amazon-web-services amazon-ses aws-sdk

我正在尝试向Amazon SES发送电子邮件。我运行时没有错误,但没有任何反应。我试图找到文档,但亚马逊在这方面做得很糟糕。那么,有什么想法吗?编辑:我已经在亚马逊SES控制台验证了我试图发送的邮件。

这是我到目前为止所得到的:

    let sns = AWSSES.defaultSES()

    var messageBody = AWSSESContent()
    var subject = AWSSESContent()
    var body = AWSSESBody()
    subject.data = "Subject"
    messageBody.data = "Message body"
    body.text = messageBody

    var message = AWSSESMessage()
    message.subject = subject
    message.body = body

    var destination = AWSSESDestination()
    destination.toAddresses = ["emailadress@gmail.com"]

    var send = AWSSESSendEmailRequest()
    send.source = "emailadress@gmail.com"
    send.destination = destination
    send.message = message


    sns.sendEmail(send).continueWithSuccessBlock {(task: BFTask!) -> BFTask! in
        NSLog("Sent mail - success")
        return nil
    }


}

1 个答案:

答案 0 :(得分:1)

所以,问题在于我没有使用正确的区域进行SES服务。工作代码:

let credentialProvider = AWSCognitoCredentialsProvider.credentialsWithRegionType(
            CognitoRegionType,
            identityPoolId: CognitoIdentityPoolId)
        let configuration = AWSServiceConfiguration(
            region: SESServiceRegionType,
            credentialsProvider: credentialProvider)
        var sns = AWSSES(configuration: configuration)

        var messageBody = AWSSESContent()
        var subject = AWSSESContent()
        var body = AWSSESBody()
        subject.data = "Subject"
        messageBody.data = "Message body"
        body.text = messageBody

        var theMessage = AWSSESMessage()
        theMessage.subject = subject
        theMessage.body = body

        var destination = AWSSESDestination()
        destination.toAddresses = ["emailadress@gmail.com"]

        var send = AWSSESSendEmailRequest()
        send.source = "emailadress@gmail.com"
        send.destination = destination
        send.message = theMessage
        send.returnPath = "emailadress@gmail.com"

        sns.sendEmail(send).continueWithBlock {(task: AnyObject!) -> BFTask! in

            return nil
        }