Amazon SES examples using SDK for C++ - Amazon SDK for C++
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Amazon SES examples using SDK for C++

The following code examples show you how to perform actions and implement common scenarios by using the Amazon SDK for C++ with Amazon SES.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios and cross-service examples.

Scenarios are code examples that show you how to accomplish a specific task by calling multiple functions within the same service.

Each example includes a link to GitHub, where you can find instructions on how to set up and run the code in context.

Topics

Actions

The following code example shows how to use CreateReceiptFilter.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

//! Create an Amazon Simple Email Service (Amazon SES) receipt filter.. /*! \param receiptFilterName: The name for the receipt filter. \param cidr: IP address or IP address range in Classless Inter-Domain Routing (CIDR) notation. \param policy: Block or allow enum of type ReceiptFilterPolicy. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SES::createReceiptFilter(const Aws::String &receiptFilterName, const Aws::String &cidr, Aws::SES::Model::ReceiptFilterPolicy policy, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SES::SESClient sesClient(clientConfiguration); Aws::SES::Model::CreateReceiptFilterRequest createReceiptFilterRequest; Aws::SES::Model::ReceiptFilter receiptFilter; Aws::SES::Model::ReceiptIpFilter receiptIpFilter; receiptIpFilter.SetCidr(cidr); receiptIpFilter.SetPolicy(policy); receiptFilter.SetName(receiptFilterName); receiptFilter.SetIpFilter(receiptIpFilter); createReceiptFilterRequest.SetFilter(receiptFilter); Aws::SES::Model::CreateReceiptFilterOutcome createReceiptFilterOutcome = sesClient.CreateReceiptFilter( createReceiptFilterRequest); if (createReceiptFilterOutcome.IsSuccess()) { std::cout << "Successfully created receipt filter." << std::endl; } else { std::cerr << "Error creating receipt filter: " << createReceiptFilterOutcome.GetError().GetMessage() << std::endl; } return createReceiptFilterOutcome.IsSuccess(); }

The following code example shows how to use CreateReceiptRule.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

//! Create an Amazon Simple Email Service (Amazon SES) receipt rule. /*! \param receiptRuleName: The name for the receipt rule. \param s3BucketName: The name of the S3 bucket for incoming mail. \param s3ObjectKeyPrefix: The prefix for the objects in the S3 bucket. \param ruleSetName: The name of the rule set where the receipt rule is added. \param recipients: Aws::Vector of recipients. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SES::createReceiptRule(const Aws::String &receiptRuleName, const Aws::String &s3BucketName, const Aws::String &s3ObjectKeyPrefix, const Aws::String &ruleSetName, const Aws::Vector<Aws::String> &recipients, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SES::SESClient sesClient(clientConfiguration); Aws::SES::Model::CreateReceiptRuleRequest createReceiptRuleRequest; Aws::SES::Model::S3Action s3Action; s3Action.SetBucketName(s3BucketName); s3Action.SetObjectKeyPrefix(s3ObjectKeyPrefix); Aws::SES::Model::ReceiptAction receiptAction; receiptAction.SetS3Action(s3Action); Aws::SES::Model::ReceiptRule receiptRule; receiptRule.SetName(receiptRuleName); receiptRule.WithRecipients(recipients); Aws::Vector<Aws::SES::Model::ReceiptAction> receiptActionList; receiptActionList.emplace_back(receiptAction); receiptRule.SetActions(receiptActionList); createReceiptRuleRequest.SetRuleSetName(ruleSetName); createReceiptRuleRequest.SetRule(receiptRule); auto outcome = sesClient.CreateReceiptRule(createReceiptRuleRequest); if (outcome.IsSuccess()) { std::cout << "Successfully created receipt rule." << std::endl; } else { std::cerr << "Error creating receipt rule. " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }

The following code example shows how to use CreateReceiptRuleSet.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

//! Create an Amazon Simple Email Service (Amazon SES) receipt rule set. /*! \param ruleSetName: The name of the rule set. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SES::createReceiptRuleSet(const Aws::String &ruleSetName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SES::SESClient sesClient(clientConfiguration); Aws::SES::Model::CreateReceiptRuleSetRequest createReceiptRuleSetRequest; createReceiptRuleSetRequest.SetRuleSetName(ruleSetName); Aws::SES::Model::CreateReceiptRuleSetOutcome outcome = sesClient.CreateReceiptRuleSet( createReceiptRuleSetRequest); if (outcome.IsSuccess()) { std::cout << "Successfully created receipt rule set." << std::endl; } else { std::cerr << "Error creating receipt rule set. " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }

The following code example shows how to use CreateTemplate.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

//! Create an Amazon Simple Email Service (Amazon SES) template. /*! \param templateName: The name of the template. \param htmlPart: The HTML body of the email. \param subjectPart: The subject line of the email. \param textPart: The plain text version of the email. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SES::createTemplate(const Aws::String &templateName, const Aws::String &htmlPart, const Aws::String &subjectPart, const Aws::String &textPart, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SES::SESClient sesClient(clientConfiguration); Aws::SES::Model::CreateTemplateRequest createTemplateRequest; Aws::SES::Model::Template aTemplate; aTemplate.SetTemplateName(templateName); aTemplate.SetHtmlPart(htmlPart); aTemplate.SetSubjectPart(subjectPart); aTemplate.SetTextPart(textPart); createTemplateRequest.SetTemplate(aTemplate); Aws::SES::Model::CreateTemplateOutcome outcome = sesClient.CreateTemplate( createTemplateRequest); if (outcome.IsSuccess()) { std::cout << "Successfully created template." << templateName << "." << std::endl; } else { std::cerr << "Error creating template. " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • For API details, see CreateTemplate in Amazon SDK for C++ API Reference.

The following code example shows how to use DeleteIdentity.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

//! Delete the specified identity (an email address or a domain). /*! \param identity: The identity to delete. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SES::deleteIdentity(const Aws::String &identity, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SES::SESClient sesClient(clientConfiguration); Aws::SES::Model::DeleteIdentityRequest deleteIdentityRequest; deleteIdentityRequest.SetIdentity(identity); Aws::SES::Model::DeleteIdentityOutcome outcome = sesClient.DeleteIdentity( deleteIdentityRequest); if (outcome.IsSuccess()) { std::cout << "Successfully deleted identity." << std::endl; } else { std::cerr << "Error deleting identity. " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • For API details, see DeleteIdentity in Amazon SDK for C++ API Reference.

The following code example shows how to use DeleteReceiptFilter.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

//! Delete an Amazon Simple Email Service (Amazon SES) receipt filter. /*! \param receiptFilterName: The name for the receipt filter. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SES::deleteReceiptFilter(const Aws::String &receiptFilterName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SES::SESClient sesClient(clientConfiguration); Aws::SES::Model::DeleteReceiptFilterRequest deleteReceiptFilterRequest; deleteReceiptFilterRequest.SetFilterName(receiptFilterName); Aws::SES::Model::DeleteReceiptFilterOutcome outcome = sesClient.DeleteReceiptFilter( deleteReceiptFilterRequest); if (outcome.IsSuccess()) { std::cout << "Successfully deleted receipt filter." << std::endl; } else { std::cerr << "Error deleting receipt filter. " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }

The following code example shows how to use DeleteReceiptRule.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

//! Delete an Amazon Simple Email Service (Amazon SES) receipt rule. /*! \param receiptRuleName: The name for the receipt rule. \param receiptRuleSetName: The name for the receipt rule set. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SES::deleteReceiptRule(const Aws::String &receiptRuleName, const Aws::String &receiptRuleSetName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SES::SESClient sesClient(clientConfiguration); Aws::SES::Model::DeleteReceiptRuleRequest deleteReceiptRuleRequest; deleteReceiptRuleRequest.SetRuleName(receiptRuleName); deleteReceiptRuleRequest.SetRuleSetName(receiptRuleSetName); Aws::SES::Model::DeleteReceiptRuleOutcome outcome = sesClient.DeleteReceiptRule( deleteReceiptRuleRequest); if (outcome.IsSuccess()) { std::cout << "Successfully deleted receipt rule." << std::endl; } else { std::cout << "Error deleting receipt rule. " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }

The following code example shows how to use DeleteReceiptRuleSet.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

//! Delete an Amazon Simple Email Service (Amazon SES) receipt rule set. /*! \param receiptRuleSetName: The name for the receipt rule set. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SES::deleteReceiptRuleSet(const Aws::String &receiptRuleSetName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SES::SESClient sesClient(clientConfiguration); Aws::SES::Model::DeleteReceiptRuleSetRequest deleteReceiptRuleSetRequest; deleteReceiptRuleSetRequest.SetRuleSetName(receiptRuleSetName); Aws::SES::Model::DeleteReceiptRuleSetOutcome outcome = sesClient.DeleteReceiptRuleSet( deleteReceiptRuleSetRequest); if (outcome.IsSuccess()) { std::cout << "Successfully deleted receipt rule set." << std::endl; } else { std::cerr << "Error deleting receipt rule set. " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }

The following code example shows how to use DeleteTemplate.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

//! Delete an Amazon Simple Email Service (Amazon SES) template. /*! \param templateName: The name for the template. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SES::deleteTemplate(const Aws::String &templateName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SES::SESClient sesClient(clientConfiguration); Aws::SES::Model::DeleteTemplateRequest deleteTemplateRequest; deleteTemplateRequest.SetTemplateName(templateName); Aws::SES::Model::DeleteTemplateOutcome outcome = sesClient.DeleteTemplate( deleteTemplateRequest); if (outcome.IsSuccess()) { std::cout << "Successfully deleted template." << std::endl; } else { std::cerr << "Error deleting template. " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • For API details, see DeleteTemplate in Amazon SDK for C++ API Reference.

The following code example shows how to use GetTemplate.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

//! Get a template's attributes. /*! \param templateName: The name for the template. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SES::getTemplate(const Aws::String &templateName, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SES::SESClient sesClient(clientConfiguration); Aws::SES::Model::GetTemplateRequest getTemplateRequest; getTemplateRequest.SetTemplateName(templateName); Aws::SES::Model::GetTemplateOutcome outcome = sesClient.GetTemplate( getTemplateRequest); if (outcome.IsSuccess()) { std::cout << "Successfully got template." << std::endl; } else { std::cerr << "Error getting template. " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • For API details, see GetTemplate in Amazon SDK for C++ API Reference.

The following code example shows how to use ListIdentities.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

//! List the identities associated with this account. /*! \param identityType: The identity type enum. "NOT_SET" is a valid option. \param identities; A vector to receive the retrieved identities. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SES::listIdentities(Aws::SES::Model::IdentityType identityType, Aws::Vector<Aws::String> &identities, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SES::SESClient sesClient(clientConfiguration); Aws::SES::Model::ListIdentitiesRequest listIdentitiesRequest; if (identityType != Aws::SES::Model::IdentityType::NOT_SET) { listIdentitiesRequest.SetIdentityType(identityType); } Aws::String nextToken; // Used for paginated results. do { if (!nextToken.empty()) { listIdentitiesRequest.SetNextToken(nextToken); } Aws::SES::Model::ListIdentitiesOutcome outcome = sesClient.ListIdentities( listIdentitiesRequest); if (outcome.IsSuccess()) { const auto &retrievedIdentities = outcome.GetResult().GetIdentities(); if (!retrievedIdentities.empty()) { identities.insert(identities.cend(), retrievedIdentities.cbegin(), retrievedIdentities.cend()); } nextToken = outcome.GetResult().GetNextToken(); } else { std::cout << "Error listing identities. " << outcome.GetError().GetMessage() << std::endl; return false; } } while (!nextToken.empty()); return true; }
  • For API details, see ListIdentities in Amazon SDK for C++ API Reference.

The following code example shows how to use ListReceiptFilters.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

//! List the receipt filters associated with this account. /*! \param filters; A vector of "ReceiptFilter" to receive the retrieved filters. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SES::listReceiptFilters(Aws::Vector<Aws::SES::Model::ReceiptFilter> &filters, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SES::SESClient sesClient(clientConfiguration); Aws::SES::Model::ListReceiptFiltersRequest listReceiptFiltersRequest; Aws::SES::Model::ListReceiptFiltersOutcome outcome = sesClient.ListReceiptFilters( listReceiptFiltersRequest); if (outcome.IsSuccess()) { auto &retrievedFilters = outcome.GetResult().GetFilters(); if (!retrievedFilters.empty()) { filters.insert(filters.cend(), retrievedFilters.cbegin(), retrievedFilters.cend()); } } else { std::cerr << "Error retrieving IP address filters: " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }

The following code example shows how to use SendEmail.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

//! Send an email to a list of recipients. /*! \param recipients; Vector of recipient email addresses. \param subject: Email subject. \param htmlBody: Email body as HTML. At least one body data is required. \param textBody: Email body as plain text. At least one body data is required. \param senderEmailAddress: Email address of sender. Ignored if empty string. \param ccAddresses: Vector of cc addresses. Ignored if empty. \param replyToAddress: Reply to email address. Ignored if empty string. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SES::sendEmail(const Aws::Vector<Aws::String> &recipients, const Aws::String &subject, const Aws::String &htmlBody, const Aws::String &textBody, const Aws::String &senderEmailAddress, const Aws::Vector<Aws::String> &ccAddresses, const Aws::String &replyToAddress, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SES::SESClient sesClient(clientConfiguration); Aws::SES::Model::Destination destination; if (!ccAddresses.empty()) { destination.WithCcAddresses(ccAddresses); } if (!recipients.empty()) { destination.WithToAddresses(recipients); } Aws::SES::Model::Body message_body; if (!htmlBody.empty()) { message_body.SetHtml( Aws::SES::Model::Content().WithCharset("UTF-8").WithData(htmlBody)); } if (!textBody.empty()) { message_body.SetText( Aws::SES::Model::Content().WithCharset("UTF-8").WithData(textBody)); } Aws::SES::Model::Message message; message.SetBody(message_body); message.SetSubject( Aws::SES::Model::Content().WithCharset("UTF-8").WithData(subject)); Aws::SES::Model::SendEmailRequest sendEmailRequest; sendEmailRequest.SetDestination(destination); sendEmailRequest.SetMessage(message); if (!senderEmailAddress.empty()) { sendEmailRequest.SetSource(senderEmailAddress); } if (!replyToAddress.empty()) { sendEmailRequest.AddReplyToAddresses(replyToAddress); } auto outcome = sesClient.SendEmail(sendEmailRequest); if (outcome.IsSuccess()) { std::cout << "Successfully sent message with ID " << outcome.GetResult().GetMessageId() << "." << std::endl; } else { std::cerr << "Error sending message. " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • For API details, see SendEmail in Amazon SDK for C++ API Reference.

The following code example shows how to use SendTemplatedEmail.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

//! Send a templated email to a list of recipients. /*! \param recipients; Vector of recipient email addresses. \param templateName: The name of the template to use. \param templateData: Map of key-value pairs for replacing text in template. \param senderEmailAddress: Email address of sender. Ignored if empty string. \param ccAddresses: Vector of cc addresses. Ignored if empty. \param replyToAddress: Reply to email address. Ignored if empty string. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SES::sendTemplatedEmail(const Aws::Vector<Aws::String> &recipients, const Aws::String &templateName, const Aws::Map<Aws::String, Aws::String> &templateData, const Aws::String &senderEmailAddress, const Aws::Vector<Aws::String> &ccAddresses, const Aws::String &replyToAddress, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SES::SESClient sesClient(clientConfiguration); Aws::SES::Model::Destination destination; if (!ccAddresses.empty()) { destination.WithCcAddresses(ccAddresses); } if (!recipients.empty()) { destination.WithToAddresses(recipients); } Aws::SES::Model::SendTemplatedEmailRequest sendTemplatedEmailRequest; sendTemplatedEmailRequest.SetDestination(destination); sendTemplatedEmailRequest.SetTemplate(templateName); std::ostringstream templateDataStream; templateDataStream << "{"; size_t dataCount = 0; for (auto &pair: templateData) { templateDataStream << "\"" << pair.first << "\":\"" << pair.second << "\""; dataCount++; if (dataCount < templateData.size()) { templateDataStream << ","; } } templateDataStream << "}"; sendTemplatedEmailRequest.SetTemplateData(templateDataStream.str()); if (!senderEmailAddress.empty()) { sendTemplatedEmailRequest.SetSource(senderEmailAddress); } if (!replyToAddress.empty()) { sendTemplatedEmailRequest.AddReplyToAddresses(replyToAddress); } auto outcome = sesClient.SendTemplatedEmail(sendTemplatedEmailRequest); if (outcome.IsSuccess()) { std::cout << "Successfully sent templated message with ID " << outcome.GetResult().GetMessageId() << "." << std::endl; } else { std::cerr << "Error sending templated message. " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }

The following code example shows how to use UpdateTemplate.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

//! Update an Amazon Simple Email Service (Amazon SES) template. /*! \param templateName: The name of the template. \param htmlPart: The HTML body of the email. \param subjectPart: The subject line of the email. \param textPart: The plain text version of the email. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SES::updateTemplate(const Aws::String &templateName, const Aws::String &htmlPart, const Aws::String &subjectPart, const Aws::String &textPart, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SES::SESClient sesClient(clientConfiguration); Aws::SES::Model::Template templateValues; templateValues.SetTemplateName(templateName); templateValues.SetSubjectPart(subjectPart); templateValues.SetHtmlPart(htmlPart); templateValues.SetTextPart(textPart); Aws::SES::Model::UpdateTemplateRequest updateTemplateRequest; updateTemplateRequest.SetTemplate(templateValues); Aws::SES::Model::UpdateTemplateOutcome outcome = sesClient.UpdateTemplate(updateTemplateRequest); if (outcome.IsSuccess()) { std::cout << "Successfully updated template." << std::endl; } else { std::cerr << "Error updating template. " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • For API details, see UpdateTemplate in Amazon SDK for C++ API Reference.

The following code example shows how to use VerifyEmailIdentity.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

//! Add an email address to the list of identities associated with this account and //! initiate verification. /*! \param emailAddress; The email address to add. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SES::verifyEmailIdentity(const Aws::String &emailAddress, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SES::SESClient sesClient(clientConfiguration); Aws::SES::Model::VerifyEmailIdentityRequest verifyEmailIdentityRequest; verifyEmailIdentityRequest.SetEmailAddress(emailAddress); Aws::SES::Model::VerifyEmailIdentityOutcome outcome = sesClient.VerifyEmailIdentity(verifyEmailIdentityRequest); if (outcome.IsSuccess()) { std::cout << "Email verification initiated." << std::endl; } else { std::cerr << "Error initiating email verification. " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }