AWS iOS SDK:通过AWSSES发送电子邮件

时间:2016-04-05 06:11:53

标签: ios objective-c aws-sdk

有没有人使用最新的Amazon AWS SDK 2.3.6通过SES SMTP发送电子邮件的经验?我目前有api密钥,秘密和smtp_url。

谢谢!

3 个答案:

答案 0 :(得分:4)

刚想通了。我承认亚马逊的文档有点密集。希望这有助于其他人!

AWSSESSendEmailRequest *awsSESSendEmailRequest = [AWSSESSendEmailRequest new];
awsSESSendEmailRequest.source = @"source@email";
AWSSESDestination *awsSESDestination = [AWSSESDestination new];
awsSESDestination.toAddresses = [NSMutableArray arrayWithObjects:@"to@email",nil];
awsSESSendEmailRequest.destination = awsSESDestination;

AWSSESMessage *awsSESMessage = [AWSSESMessage new];
AWSSESContent *awsSESSubject = [AWSSESContent new];
awsSESSubject.data = @"Subject goes here";
awsSESSubject.charset = @"UTF-8";

awsSESMessage.subject = awsSESSubject;
AWSSESContent *awsSESContent = [AWSSESContent new];
awsSESContent.data = @"Message goes here";
awsSESContent.charset = @"UTF-8";

AWSSESBody *awsSESBody = [AWSSESBody new];
awsSESBody.text = awsSESContent;
awsSESMessage.body = awsSESBody;
awsSESSendEmailRequest.message = awsSESMessage;


AWSStaticCredentialsProvider *credentialsProvider = [[AWSStaticCredentialsProvider alloc] initWithAccessKey:@"ACCESS-KEY"
                                                                                                  secretKey:@"SECRET-KEY"];
AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSWest2
                                                                     credentialsProvider:credentialsProvider];
[AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;

[[AWSSES defaultSES] sendEmail:awsSESSendEmailRequest completionHandler:^(AWSSESSendEmailResponse * _Nullable response, NSError * _Nullable error) {
    if (error)
    {
        // error
    }
    else
    {
        // success
    }
}];

答案 1 :(得分:0)

以下Swift 3.0中发送电子邮件的代码段。

    let serviceRegionType = AWSRegionType.usEast1
    let credentialsProvider = AWSStaticCredentialsProvider.init(accessKey: "access", secretKey: "secret")
    let configuration = AWSServiceConfiguration(region: serviceRegionType, credentialsProvider: credentialsProvider)
    AWSServiceManager.default().defaultServiceConfiguration = configuration

    let subject = AWSSESContent()
    subject?.data = "Subject"
    subject?.charset = "UTF-8"

    let messageBody = AWSSESContent()
    messageBody?.data = "Sample Message body"
    messageBody?.charset = "UTF-8"

    let body = AWSSESBody()
    body?.text = messageBody

    let theMessage = AWSSESMessage()
    theMessage?.subject = subject
    theMessage?.body = body

    let destination = AWSSESDestination()
    destination?.toAddresses = ["toaddress"]

    let send = AWSSESSendEmailRequest()
    send?.source = "source mail"
    send?.destination = destination
    send?.message = theMessage

    AWSSES.default().sendEmail(send!) { (response:AWSSESSendEmailResponse?, mailError: Error?) in
        print(mailError?.localizedDescription)
        if ((response?.messageId) != nil) {
            print("Mail has delivered succesfully")
        } else {
            print("Mail has failed to delivered")
        }
    }

答案 2 :(得分:0)

要添加到unicornherder的答案:您的代码对我的iOS应用程序运行良好。但是,由于我的应用程序用户已通过Cognito身份验证,因此不需要您的代码来设置AWSStaticCredentialsProvider。根据示例代码,这已经在我的AppDelegate中发生。

但是,

没有需要授予我的Cognito授权用户使用SES的权限。最后一步是通过将权限添加到authUser角色来完成的。