Send email by using the Amazon Pinpoint API
This section contains complete code examples that you can use to send email through the Amazon Pinpoint API by using an AWS SDK. You need to have verified either an email address or domain before you can send a message.
- C#
-
Use this example to send email by using the AWS SDK for .NET
. This example assumes that you've already installed and configured the AWS SDK for .NET. For more information, see Getting started with the AWS SDK for .NET in the AWS SDK for .NET Developer Guide. This example assumes that you're using a shared credentials file to specify the Access Key and Secret Access Key for an existing user. For more information, see Configuring AWS credentials in the AWS SDK for .NET Developer Guide.
This code example was tested using the AWS SDK for .NET version 3.3.29.13 and .NET Core runtime version 2.1.2.
using Amazon; using Amazon.Pinpoint; using Amazon.Pinpoint.Model; using Microsoft.Extensions.Configuration; namespace SendEmailMessage; public class SendEmailMainClass { public static async Task Main(string[] args) { var configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("settings.json") // Load test settings from .json file. .AddJsonFile("settings.local.json", true) // Optionally load local settings. .Build(); // The AWS Region that you want to use to send the email. For a list of // AWS Regions where the Amazon Pinpoint API is available, see // https://docs.aws.amazon.com/pinpoint/latest/apireference/ string region = "us-east-1"; // The "From" address. This address has to be verified in Amazon Pinpoint // in the region you're using to send email. string senderAddress = configuration["SenderAddress"]!; // The address on the "To" line. If your Amazon Pinpoint account is in // the sandbox, this address also has to be verified. string toAddress = configuration["ToAddress"]!; // The Amazon Pinpoint project/application ID to use when you send this message. // Make sure that the SMS channel is enabled for the project or application // that you choose. string appId = configuration["AppId"]!; try { await SendEmailMessage(region, appId, toAddress, senderAddress); } catch (Exception ex) { Console.WriteLine("The message wasn't sent. Error message: " + ex.Message); } } public static async Task<MessageResponse> SendEmailMessage( string region, string appId, string toAddress, string senderAddress) { var client = new AmazonPinpointClient(RegionEndpoint.GetBySystemName(region)); // The subject line of the email. string subject = "Amazon Pinpoint Email test"; // The body of the email for recipients whose email clients don't // support HTML content. string textBody = @"Amazon Pinpoint Email Test (.NET)" + "\n---------------------------------" + "\nThis email was sent using the Amazon Pinpoint API using the AWS SDK for .NET."; // The body of the email for recipients whose email clients support // HTML content. string htmlBody = @"<html>" + "\n<head></head>" + "\n<body>" + "\n <h1>Amazon Pinpoint Email Test (AWS SDK for .NET)</h1>" + "\n <p>This email was sent using the " + "\n <a href='https://aws.amazon.com/pinpoint/'>Amazon Pinpoint</a> API " + "\n using the <a href='https://aws.amazon.com/sdk-for-net/'>AWS SDK for .NET</a>" + "\n </p>" + "\n</body>" + "\n</html>"; // The character encoding the you want to use for the subject line and // message body of the email. string charset = "UTF-8"; var sendRequest = new SendMessagesRequest { ApplicationId = appId, MessageRequest = new MessageRequest { Addresses = new Dictionary<string, AddressConfiguration> { { toAddress, new AddressConfiguration { ChannelType = ChannelType.EMAIL } } }, MessageConfiguration = new DirectMessageConfiguration { EmailMessage = new EmailMessage { FromAddress = senderAddress, SimpleEmail = new SimpleEmail { HtmlPart = new SimpleEmailPart { Charset = charset, Data = htmlBody }, TextPart = new SimpleEmailPart { Charset = charset, Data = textBody }, Subject = new SimpleEmailPart { Charset = charset, Data = subject } } } } } }; Console.WriteLine("Sending message..."); SendMessagesResponse response = await client.SendMessagesAsync(sendRequest); Console.WriteLine("Message sent!"); return response.MessageResponse; } }
- Java
-
Use this example to send email by using the AWS SDK for Java
. This example assumes that you've already installed and configured the AWS SDK for Java 2.x. For more information, see Getting started in the AWS SDK for Java 2.x Developer Guide. This example assumes that you're using a shared credentials file to specify the Access Key and Secret Access Key for an existing user. For more information, see Set default credentials and Region in the AWS SDK for Java Developer Guide.
This code example was tested using the AWS SDK for Java version 2.3.1 and OpenJDK version 11.0.1.
import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.pinpoint.PinpointClient; import software.amazon.awssdk.services.pinpoint.model.AddressConfiguration; import software.amazon.awssdk.services.pinpoint.model.ChannelType; import software.amazon.awssdk.services.pinpoint.model.SimpleEmailPart; import software.amazon.awssdk.services.pinpoint.model.SimpleEmail; import software.amazon.awssdk.services.pinpoint.model.EmailMessage; import software.amazon.awssdk.services.pinpoint.model.DirectMessageConfiguration; import software.amazon.awssdk.services.pinpoint.model.MessageRequest; import software.amazon.awssdk.services.pinpoint.model.SendMessagesRequest; import software.amazon.awssdk.services.pinpoint.model.PinpointException; import software.amazon.awssdk.services.pinpointemail.PinpointEmailClient; import software.amazon.awssdk.services.pinpointemail.model.Body; import software.amazon.awssdk.services.pinpointemail.model.Content; import software.amazon.awssdk.services.pinpointemail.model.Destination; import software.amazon.awssdk.services.pinpointemail.model.EmailContent; import software.amazon.awssdk.services.pinpointemail.model.Message; import software.amazon.awssdk.services.pinpointemail.model.SendEmailRequest; import java.util.HashMap; import java.util.Map;
import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.pinpoint.PinpointClient; import software.amazon.awssdk.services.pinpoint.model.AddressConfiguration; import software.amazon.awssdk.services.pinpoint.model.ChannelType; import software.amazon.awssdk.services.pinpoint.model.SimpleEmailPart; import software.amazon.awssdk.services.pinpoint.model.SimpleEmail; import software.amazon.awssdk.services.pinpoint.model.EmailMessage; import software.amazon.awssdk.services.pinpoint.model.DirectMessageConfiguration; import software.amazon.awssdk.services.pinpoint.model.MessageRequest; import software.amazon.awssdk.services.pinpoint.model.SendMessagesRequest; import software.amazon.awssdk.services.pinpoint.model.PinpointException; import software.amazon.awssdk.services.pinpointemail.PinpointEmailClient; import software.amazon.awssdk.services.pinpointemail.model.Body; import software.amazon.awssdk.services.pinpointemail.model.Content; import software.amazon.awssdk.services.pinpointemail.model.Destination; import software.amazon.awssdk.services.pinpointemail.model.EmailContent; import software.amazon.awssdk.services.pinpointemail.model.Message; import software.amazon.awssdk.services.pinpointemail.model.SendEmailRequest; import java.util.HashMap; import java.util.Map; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class SendEmailMessage { // The character encoding the you want to use for the subject line and // message body of the email. public static String charset = "UTF-8"; // The body of the email for recipients whose email clients support HTML content. static final String body = """ Amazon Pinpoint test (AWS SDK for Java 2.x) This email was sent through the Amazon Pinpoint Email API using the AWS SDK for Java 2.x """; public static void main(String[] args) { final String usage = """ Usage: <subject> <appId> <senderAddress> <toAddress> Where: subject - The email subject to use. senderAddress - The from address. This address has to be verified in Amazon Pinpoint in the region you're using to send email\s toAddress - The to address. This address has to be verified in Amazon Pinpoint in the region you're using to send email\s """; if (args.length != 3) { System.out.println(usage); System.exit(1); } String subject = args[0]; String senderAddress = args[1]; String toAddress = args[2]; System.out.println("Sending a message"); PinpointEmailClient pinpoint = PinpointEmailClient.builder() .region(Region.US_EAST_1) .build(); sendEmail(pinpoint, subject, senderAddress, toAddress); System.out.println("Email was sent"); pinpoint.close(); } public static void sendEmail(PinpointEmailClient pinpointEmailClient, String subject, String senderAddress, String toAddress) { try { Content content = Content.builder() .data(body) .build(); Body messageBody = Body.builder() .text(content) .build(); Message message = Message.builder() .body(messageBody) .subject(Content.builder().data(subject).build()) .build(); Destination destination = Destination.builder() .toAddresses(toAddress) .build(); EmailContent emailContent = EmailContent.builder() .simple(message) .build(); SendEmailRequest sendEmailRequest = SendEmailRequest.builder() .fromEmailAddress(senderAddress) .destination(destination) .content(emailContent) .build(); pinpointEmailClient.sendEmail(sendEmailRequest); System.out.println("Message Sent"); } catch (PinpointException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
For the full SDK example, see SendEmailMessage.java
on GitHub . - JavaScript (Node.js)
-
Use this example to send email by using the AWS SDK for JavaScript in Node.js
. This example assumes that you've already installed and configured the SDK for JavaScript in Node.js. For more information, see Getting started in the AWS SDK for JavaScript in Node.js Developer Guide. This example assumes that you're using a shared credentials file to specify the Access Key and Secret Access Key for an existing user. For more information, see Setting credentials in the AWS SDK for JavaScript in Node.js Developer Guide.
This code example was tested using the SDK for JavaScript in Node.js version 2.388.0 and Node.js version 11.7.0.
"use strict"; const AWS = require("aws-sdk"); // The AWS Region that you want to use to send the email. For a list of // AWS Regions where the Amazon Pinpoint API is available, see // https://docs.aws.amazon.com/pinpoint/latest/apireference/ const aws_region = "us-west-2"; // The "From" address. This address has to be verified in Amazon Pinpoint // in the region that you use to send email. const senderAddress = "sender@example.com"; // The address on the "To" line. If your Amazon Pinpoint account is in // the sandbox, this address also has to be verified. var toAddress = "recipient@example.com"; // The Amazon Pinpoint project/application ID to use when you send this message. // Make sure that the SMS channel is enabled for the project or application // that you choose. const appId = "ce796be37f32f178af652b26eexample"; // The subject line of the email. var subject = "Amazon Pinpoint (AWS SDK for JavaScript in Node.js)"; // The email body for recipients with non-HTML email clients. var body_text = `Amazon Pinpoint Test (SDK for JavaScript in Node.js) ---------------------------------------------------- This email was sent with Amazon Pinpoint using the AWS SDK for JavaScript in Node.js. For more information, see https:\/\/aws.amazon.com/sdk-for-node-js/`; // The body of the email for recipients whose email clients support HTML content. var body_html = `<html> <head></head> <body> <h1>Amazon Pinpoint Test (SDK for JavaScript in Node.js)</h1> <p>This email was sent with <a href='https://aws.amazon.com/pinpoint/'>the Amazon Pinpoint API</a> using the <a href='https://aws.amazon.com/sdk-for-node-js/'> AWS SDK for JavaScript in Node.js</a>.</p> </body> </html>`; // The character encoding the you want to use for the subject line and // message body of the email. var charset = "UTF-8"; // Specify that you're using a shared credentials file. var credentials = new AWS.SharedIniFileCredentials({ profile: "default" }); AWS.config.credentials = credentials; // Specify the region. AWS.config.update({ region: aws_region }); //Create a new Pinpoint object. var pinpoint = new AWS.Pinpoint(); // Specify the parameters to pass to the API. var params = { ApplicationId: appId, MessageRequest: { Addresses: { [toAddress]: { ChannelType: "EMAIL", }, }, MessageConfiguration: { EmailMessage: { FromAddress: senderAddress, SimpleEmail: { Subject: { Charset: charset, Data: subject, }, HtmlPart: { Charset: charset, Data: body_html, }, TextPart: { Charset: charset, Data: body_text, }, }, }, }, }, }; //Try to send the email. pinpoint.sendMessages(params, function (err, data) { // If something goes wrong, print an error message. if (err) { console.log(err.message); } else { console.log( "Email sent! Message ID: ", data["MessageResponse"]["Result"][toAddress]["MessageId"] ); } });
- Python
-
Use this example to send email by using the AWS SDK for Python (Boto3)
. This example assumes that you've already installed and configured the SDK for Python (Boto3). For more information, see Quickstart in the AWS SDK for Python (Boto3) API Reference. import logging import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) def send_email_message( pinpoint_client, app_id, sender, to_addresses, char_set, subject, html_message, text_message, ): """ Sends an email message with HTML and plain text versions. :param pinpoint_client: A Boto3 Pinpoint client. :param app_id: The Amazon Pinpoint project ID to use when you send this message. :param sender: The "From" address. This address must be verified in Amazon Pinpoint in the AWS Region you're using to send email. :param to_addresses: The addresses on the "To" line. If your Amazon Pinpoint account is in the sandbox, these addresses must be verified. :param char_set: The character encoding to use for the subject line and message body of the email. :param subject: The subject line of the email. :param html_message: The body of the email for recipients whose email clients can display HTML content. :param text_message: The body of the email for recipients whose email clients don't support HTML content. :return: A dict of to_addresses and their message IDs. """ try: response = pinpoint_client.send_messages( ApplicationId=app_id, MessageRequest={ "Addresses": { to_address: {"ChannelType": "EMAIL"} for to_address in to_addresses }, "MessageConfiguration": { "EmailMessage": { "FromAddress": sender, "SimpleEmail": { "Subject": {"Charset": char_set, "Data": subject}, "HtmlPart": {"Charset": char_set, "Data": html_message}, "TextPart": {"Charset": char_set, "Data": text_message}, }, } }, }, ) except ClientError: logger.exception("Couldn't send email.") raise else: return { to_address: message["MessageId"] for to_address, message in response["MessageResponse"]["Result"].items() } def main(): app_id = "ce796be37f32f178af652b26eexample" sender = "sender@example.com" to_address = "recipient@example.com" char_set = "UTF-8" subject = "Amazon Pinpoint Test (SDK for Python (Boto3))" text_message = """Amazon Pinpoint Test (SDK for Python) ------------------------------------- This email was sent with Amazon Pinpoint using the AWS SDK for Python (Boto3). For more information, see https://aws.amazon.com/sdk-for-python/ """ html_message = """<html> <head></head> <body> <h1>Amazon Pinpoint Test (SDK for Python (Boto3)</h1> <p>This email was sent with <a href='https://aws.amazon.com/pinpoint/'>Amazon Pinpoint</a> using the <a href='https://aws.amazon.com/sdk-for-python/'> AWS SDK for Python (Boto3)</a>.</p> </body> </html> """ print("Sending email.") message_ids = send_email_message( boto3.client("pinpoint"), app_id, sender, [to_address], char_set, subject, html_message, text_message, ) print(f"Message sent! Message IDs: {message_ids}") if __name__ == "__main__": main()
You can also use message templates to send email messages, as shown in the following example:
import logging import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) def send_templated_email_message( pinpoint_client, project_id, sender, to_addresses, template_name, template_version ): """ Sends an email message with HTML and plain text versions. :param pinpoint_client: A Boto3 Pinpoint client. :param project_id: The Amazon Pinpoint project ID to use when you send this message. :param sender: The "From" address. This address must be verified in Amazon Pinpoint in the AWS Region you're using to send email. :param to_addresses: The addresses on the "To" line. If your Amazon Pinpoint account is in the sandbox, these addresses must be verified. :param template_name: The name of the email template to use when sending the message. :param template_version: The version number of the message template. :return: A dict of to_addresses and their message IDs. """ try: response = pinpoint_client.send_messages( ApplicationId=project_id, MessageRequest={ "Addresses": { to_address: {"ChannelType": "EMAIL"} for to_address in to_addresses }, "MessageConfiguration": {"EmailMessage": {"FromAddress": sender}}, "TemplateConfiguration": { "EmailTemplate": { "Name": template_name, "Version": template_version, } }, }, ) except ClientError: logger.exception("Couldn't send email.") raise else: return { to_address: message["MessageId"] for to_address, message in response["MessageResponse"]["Result"].items() } def main(): project_id = "296b04b342374fceb661bf494example" sender = "sender@example.com" to_addresses = ["recipient@example.com"] template_name = "My_Email_Template" template_version = "1" print("Sending email.") message_ids = send_templated_email_message( boto3.client("pinpoint"), project_id, sender, to_addresses, template_name, template_version, ) print(f"Message sent! Message IDs: {message_ids}") if __name__ == "__main__": main()
These examples assume that you're using a shared credentials file to specify the Access Key and Secret Access Key for an existing user. For more information, see Credentials
in the AWS SDK for Python (Boto3) API Reference.