简体   繁体   中英

AWS iOS SDK: Sending Email via AWSSES

Does anyone have any experience using the latest Amazon AWS SDK 2.3.6 for sending an email via SES SMTP? I currently have an api key, secret, and smtp_url.

Thanks!

Just figured it out. I confess Amazon's documentation is a little dense. Hope this helps someone else!

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
    }
}];

The code snippet of Send email in Swift 3.0 below.

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

To add to unicornherder's answer: your code worked well for my iOS app. However, because my app users are authenticated by Cognito, I did not need your code used to set up AWSStaticCredentialsProvider. This already happens in my AppDelegate per the sample code.

I did need to give my Cognito-authorized users permission to use SES, however. This last step is accomplished by adding the permission to the authUser role.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM