Envie e-mails usando o Amazon Pinpoint API - Amazon Pinpoint

As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.

Envie e-mails usando o Amazon Pinpoint API

Esta seção contém exemplos de código completos que você pode usar para enviar e-mails pelo Amazon Pinpoint API usando um. AWS SDK Você precisa verificar um endereço de e-mail ou domínio antes de enviar uma mensagem.

C#

Use esse exemplo para enviar e-mail usando o AWS SDK for .NET. Esse exemplo pressupõe que você já tenha instalado e configurado o AWS SDK for .NET. Para obter mais informações, consulte Conceitos básicos do AWS SDK for .NET no Guia do desenvolvedor do AWS SDK for .NET .

Esse exemplo pressupõe que você esteja usando um arquivo de credenciais compartilhadas para especificar a chave de acesso e a chave de acesso secreta de um usuário existente. Para obter mais informações, consulte Configuração de AWS credenciais no Guia do AWS SDK for .NET desenvolvedor.

Esse exemplo de código foi testado usando a AWS SDK for .NET versão 3.3.29.13 e. NETCore runtime versão 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 esse exemplo para enviar e-mail usando o AWS SDK for Java. Esse exemplo pressupõe que você já tenha instalado e configurado o AWS SDK for Java 2.x. Para obter mais informações, consulte Conceitos básicos no Guia do desenvolvedor do AWS SDK for Java 2.x .

Esse exemplo pressupõe que você esteja usando um arquivo de credenciais compartilhadas para especificar a chave de acesso e a chave de acesso secreta de um usuário existente. Para mais informações, consulte Configurar as credenciais padrão e a Região no Guia do desenvolvedor do AWS SDK for Java .

Esse exemplo de código foi testado usando a AWS SDK for Java versão 2.3.1 e a JDK versão Open 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); } } }

Para ver o SDK exemplo completo, consulte SendEmailMessage.java on GitHub.

JavaScript (Node.js)

Use este exemplo para enviar e-mails usando o AWS SDKfor JavaScript em Node.js. Este exemplo pressupõe que você já tenha instalado e configurado o SDK for JavaScript no Node.js. Para obter mais informações, consulte Introdução no Guia do AWS SDK desenvolvedor do For JavaScript in Node.js.

Esse exemplo pressupõe que você esteja usando um arquivo de credenciais compartilhadas para especificar a chave de acesso e a chave de acesso secreta de um usuário existente. Para obter mais informações, consulte Configuração de credenciais no Formulário no AWS SDKGuia do Desenvolvedor do Node.js. JavaScript

Esse exemplo de código foi testado usando o SDK for JavaScript na versão 2.388.0 do Node.js e na versão 11.7.0 do Node.js.

"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 esse exemplo para enviar e-mail usando o AWS SDK for Python (Boto3). Este exemplo pressupõe que você já tenha instalado e configurado o SDK para Python (Boto3). Para obter mais informações, consulte Início rápido na referência AWS SDKpara Python (APIBoto3).

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()

Você também pode usar modelos de mensagem para enviar mensagens de e-mail, conforme mostrado no exemplo a seguir:

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()

Esses exemplos pressupõem que você esteja usando um arquivo de credenciais compartilhadas para especificar a chave de acesso e a chave de acesso secreta de um usuário existente. Para obter mais informações, consulte Credenciais na Referência AWS SDKpara Python (APIBoto3).