From a676b796d3a4d9c04c2ca2c089de7980f47f9124 Mon Sep 17 00:00:00 2001 From: Rachel Hagerman <110480692+rlhagerm@users.noreply.github.com> Date: Tue, 30 Apr 2024 13:41:08 -0500 Subject: [PATCH] .NET: Convert legacy Pinpoint examples (#6380) --- .doc_gen/metadata/pinpoint_metadata.yaml | 12 ++ .../pinpoint_send_email_message_api.cs | 121 ---------------- .../Pinpoint/pinpoint_send_email_smtp.cs | 116 --------------- .../Pinpoint/pinpoint_send_sms_message_api.cs | 99 ------------- dotnetv3/CloudFormation/README.md | 2 +- dotnetv3/Pinpoint/Pinpoint.sln | 46 ++++++ .../PinpointTests/PinpointTests.csproj | 38 +++++ .../PinpointTests/SendEmailMessageTests.cs | 47 +++++++ .../PinpointTests/SendSmsMessageTests.cs | 52 +++++++ .../Pinpoint/PinpointTests/testsettings.json | 9 ++ dotnetv3/Pinpoint/README.md | 95 +++++++++++++ .../Pinpoint/SendEmailMessage/SendEmail.cs | 132 ++++++++++++++++++ .../SendEmailMessage/SendEmailMessage.csproj | 27 ++++ .../Pinpoint/SendEmailMessage/settings.json | 5 + dotnetv3/Pinpoint/SendSmsMessage/SendSms.cs | 115 +++++++++++++++ .../SendSmsMessage/SendSmsMessage.csproj | 28 ++++ .../Pinpoint/SendSmsMessage/settings.json | 7 + dotnetv3/Pinpoint/dead-snippets.txt | 4 + 18 files changed, 618 insertions(+), 337 deletions(-) delete mode 100644 .dotnet/example_code_legacy/Pinpoint/pinpoint_send_email_message_api.cs delete mode 100644 .dotnet/example_code_legacy/Pinpoint/pinpoint_send_email_smtp.cs delete mode 100644 .dotnet/example_code_legacy/Pinpoint/pinpoint_send_sms_message_api.cs create mode 100644 dotnetv3/Pinpoint/Pinpoint.sln create mode 100644 dotnetv3/Pinpoint/PinpointTests/PinpointTests.csproj create mode 100644 dotnetv3/Pinpoint/PinpointTests/SendEmailMessageTests.cs create mode 100644 dotnetv3/Pinpoint/PinpointTests/SendSmsMessageTests.cs create mode 100644 dotnetv3/Pinpoint/PinpointTests/testsettings.json create mode 100644 dotnetv3/Pinpoint/README.md create mode 100644 dotnetv3/Pinpoint/SendEmailMessage/SendEmail.cs create mode 100644 dotnetv3/Pinpoint/SendEmailMessage/SendEmailMessage.csproj create mode 100644 dotnetv3/Pinpoint/SendEmailMessage/settings.json create mode 100644 dotnetv3/Pinpoint/SendSmsMessage/SendSms.cs create mode 100644 dotnetv3/Pinpoint/SendSmsMessage/SendSmsMessage.csproj create mode 100644 dotnetv3/Pinpoint/SendSmsMessage/settings.json create mode 100644 dotnetv3/Pinpoint/dead-snippets.txt diff --git a/.doc_gen/metadata/pinpoint_metadata.yaml b/.doc_gen/metadata/pinpoint_metadata.yaml index 14fb72ae75c..33d90656e7a 100644 --- a/.doc_gen/metadata/pinpoint_metadata.yaml +++ b/.doc_gen/metadata/pinpoint_metadata.yaml @@ -4,6 +4,18 @@ pinpoint_SendMessages: synopsis: send email and text messages with &PIN;. category: languages: + .NET: + versions: + - sdk_version: 3 + github: dotnetv3/Pinpoint + sdkguide: + excerpts: + - description: Send an email message. + snippet_tags: + - pinpoint.dotnet.pinpoint_send_email_message_api.complete + - description: Send an SMS message. + snippet_tags: + - pinpoint.dotnet.pinpoint_send_sms_message_api.complete Kotlin: versions: - sdk_version: 1 diff --git a/.dotnet/example_code_legacy/Pinpoint/pinpoint_send_email_message_api.cs b/.dotnet/example_code_legacy/Pinpoint/pinpoint_send_email_message_api.cs deleted file mode 100644 index eea77e4c6cc..00000000000 --- a/.dotnet/example_code_legacy/Pinpoint/pinpoint_send_email_message_api.cs +++ /dev/null @@ -1,121 +0,0 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -// snippet-start:[pinpoint.dotnet.pinpoint_send_email_message_api.complete] - -using System; -using System.Collections.Generic; -using Amazon; -using Amazon.Pinpoint; -using Amazon.Pinpoint.Model; - -namespace PinpointEmailSendMessageAPI -{ - class MainClass - { - // 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/ - static string region = "us-west-2"; - - // The "From" address. This address has to be verified in Amazon Pinpoint - // in the region you're using to send email. - static string 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. - static string 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. - static string appId = "ce796be37f32f178af652b26eexample"; - - // The subject line of the email. - static string subject = "Amazon Pinpoint Email test"; - - // The body of the email for recipients whose email clients don't - // support HTML content. - static string textBody = @"Amazon Pinpoint Email Test (.NET) ---------------------------------- -This 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. - static string htmlBody = @" - - -

Amazon Pinpoint Email Test (AWS SDK for .NET)

-

This email was sent using the - Amazon Pinpoint API - using the - AWS SDK for .NET.

- -"; - - // The character encoding the you want to use for the subject line and - // message body of the email. - static string charset = "UTF-8"; - - public static void Main(string[] args) - { - using (var client = new AmazonPinpointClient(RegionEndpoint.GetBySystemName(region))) - { - var sendRequest = new SendMessagesRequest - { - ApplicationId = appId, - MessageRequest = new MessageRequest - { - Addresses = new Dictionary - { - { - toAddress, - new AddressConfiguration - { - 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 - } - } - } - } - } - }; - try - { - Console.WriteLine("Sending message..."); - SendMessagesResponse response = client.SendMessages(sendRequest); - Console.WriteLine("Message sent!"); - } - catch (Exception ex) - { - Console.WriteLine("The message wasn't sent. Error message: " + ex.Message); - } - } - } - } -} - -// snippet-end:[pinpoint.dotnet.pinpoint_send_email_message_api.complete] diff --git a/.dotnet/example_code_legacy/Pinpoint/pinpoint_send_email_smtp.cs b/.dotnet/example_code_legacy/Pinpoint/pinpoint_send_email_smtp.cs deleted file mode 100644 index 36d50d28cfa..00000000000 --- a/.dotnet/example_code_legacy/Pinpoint/pinpoint_send_email_smtp.cs +++ /dev/null @@ -1,116 +0,0 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -// snippet-start:[pinpoint.dotnet.pinpoint_send_email_smtp.complete] - -using System; -using System.Net; -using System.Net.Mail; - -namespace PinpointEmailSMTP -{ - class MainClass - { - // If you're using Amazon Pinpoint in a region other than US West (Oregon), - // replace email-smtp.us-west-2.amazonaws.com with the Amazon Pinpoint SMTP - // endpoint in the appropriate AWS Region. - static string smtpEndpoint = "email-smtp.us-west-2.amazonaws.com"; - - // The port to use when connecting to the SMTP server. - static int port = 587; - - // Replace sender@example.com with your "From" address. - // This address must be verified with Amazon Pinpoint. - static string senderName = "Mary Major"; - static string senderAddress = "sender@example.com"; - - // Replace recipient@example.com with a "To" address. If your account - // is still in the sandbox, this address must be verified. - static string toAddress = "recipient@example.com"; - - // CC and BCC addresses. If your account is in the sandbox, these - // addresses have to be verified. - static string ccAddress = "cc-recipient@example.com"; - static string bccAddress = "bcc-recipient@example.com"; - - // Replace smtp_username with your &PINlong; SMTP user name. - static string smtpUsername = "AKIAIOSFODNN7EXAMPLE"; - - // Use @ASMlong; to expose your &PIN; SMTP password. - static string smtpPassword = System.getEnvironmentVariable("SMTP_PASSWORD"); - - // (Optional) the name of a configuration set to use for this message. - static string configurationSet = "ConfigSet"; - - // The subject line of the email - static string subject = - "Amazon Pinpoint test (SMTP interface accessed using C#)"; - - // The body of the email for recipients whose email clients don't - // support HTML content. - static AlternateView textBody = AlternateView. - CreateAlternateViewFromString("Amazon Pinpoint Email Test (.NET)\r\n" - + "This email was sent using the Amazon Pinpoint SMTP " - + "interface.", null, "text/plain"); - - // The body of the email for recipients whose email clients support - // HTML content. - static AlternateView htmlBody = AlternateView. - CreateAlternateViewFromString("" - + "

Amazon Pinpoint SMTP Interface Test

This " - + "email was sent using the " - + "Amazon Pinpoint" - + " SMTP interface.

", null, "text/html"); - - // The message tags that you want to apply to the email. - static string tag0 = "key0=value0"; - static string tag1 = "key1=value1"; - - public static void Main(string[] args) - { - // Create a new MailMessage object - MailMessage message = new MailMessage(); - - // Add sender and recipient email addresses to the message - message.From = new MailAddress(senderAddress,senderName); - message.To.Add(new MailAddress(toAddress)); - message.CC.Add(new MailAddress(ccAddress)); - message.Bcc.Add(new MailAddress(bccAddress)); - - // Add the subject line, text body, and HTML body to the message - message.Subject = subject; - message.AlternateViews.Add(textBody); - message.AlternateViews.Add(htmlBody); - - // Add optional headers for configuration set and message tags to the message - message.Headers.Add("X-SES-CONFIGURATION-SET", configurationSet); - message.Headers.Add("X-SES-MESSAGE-TAGS", tag0); - message.Headers.Add("X-SES-MESSAGE-TAGS", tag1); - - using (var client = new System.Net.Mail.SmtpClient(smtpEndpoint, port)) - { - // Create a Credentials object for connecting to the SMTP server - client.Credentials = - new NetworkCredential(smtpUsername, smtpPassword); - - client.EnableSsl = true; - - // Send the message - try - { - Console.WriteLine("Attempting to send email..."); - client.Send(message); - Console.WriteLine("Email sent!"); - } - // Show an error message if the message can't be sent - catch (Exception ex) - { - Console.WriteLine("The email wasn't sent."); - Console.WriteLine("Error message: " + ex.Message); - } - } - } - } -} - -// snippet-end:[pinpoint.dotnet.pinpoint_send_email_smtp.complete] diff --git a/.dotnet/example_code_legacy/Pinpoint/pinpoint_send_sms_message_api.cs b/.dotnet/example_code_legacy/Pinpoint/pinpoint_send_sms_message_api.cs deleted file mode 100644 index 3b9bea229cd..00000000000 --- a/.dotnet/example_code_legacy/Pinpoint/pinpoint_send_sms_message_api.cs +++ /dev/null @@ -1,99 +0,0 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -// snippet-start:[pinpoint.dotnet.pinpoint_send_sms_message_api.complete] - -using System; -using System.Collections.Generic; -using Amazon; -using Amazon.Pinpoint; -using Amazon.Pinpoint.Model; - -namespace SendMessage -{ - class MainClass - { - // The AWS Region that you want to use to send the message. For a list of - // AWS Regions where the Amazon Pinpoint API is available, see - // https://docs.aws.amazon.com/pinpoint/latest/apireference/ - private static readonly string region = "us-east-1"; - - // The phone number or short code to send the message from. The phone number - // or short code that you specify has to be associated with your Amazon Pinpoint - // account. For best results, specify long codes in E.164 format. - private static readonly string originationNumber = "+12065550199"; - - // The recipient's phone number. For best results, you should specify the - // phone number in E.164 format. - private static readonly string destinationNumber = "+14255550142"; - - // The content of the SMS message. - private static readonly string message = "This message was sent through Amazon Pinpoint" - + "using the AWS SDK for .NET. Reply STOP to opt out."; - - // The 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. - private static readonly string appId = "ce796be37f32f178af652b26eexample"; - - // The type of SMS message that you want to send. If you plan to send - // time-sensitive content, specify TRANSACTIONAL. If you plan to send - // marketing-related content, specify PROMOTIONAL. - private static readonly string messageType = "TRANSACTIONAL"; - - // The registered keyword associated with the originating short code. - private static readonly string registeredKeyword = "myKeyword"; - - // The sender ID to use when sending the message. Support for sender ID - // varies by country or region. For more information, see - // https://docs.aws.amazon.com/pinpoint/latest/userguide/channels-sms-countries.html - private static readonly string senderId = "mySenderId"; - - public static void Main(string[] args) - { - using (AmazonPinpointClient client = new AmazonPinpointClient(RegionEndpoint.GetBySystemName(region))) - { - SendMessagesRequest sendRequest = new SendMessagesRequest - { - ApplicationId = appId, - MessageRequest = new MessageRequest - { - Addresses = new Dictionary - { - { - destinationNumber, - new AddressConfiguration - { - ChannelType = "SMS" - } - } - }, - MessageConfiguration = new DirectMessageConfiguration - { - SMSMessage = new SMSMessage - { - Body = message, - MessageType = messageType, - OriginationNumber = originationNumber, - SenderId = senderId, - Keyword = registeredKeyword - } - } - } - }; - try - { - Console.WriteLine("Sending message..."); - SendMessagesResponse response = client.SendMessages(sendRequest); - Console.WriteLine("Message sent!"); - } - catch (Exception ex) - { - Console.WriteLine("The message wasn't sent. Error message: " + ex.Message); - } - } - } - } -} - -// snippet-end:[pinpoint.dotnet.pinpoint_send_sms_message_api.complete] diff --git a/dotnetv3/CloudFormation/README.md b/dotnetv3/CloudFormation/README.md index 23d806d7fb6..d6a8156de05 100644 --- a/dotnetv3/CloudFormation/README.md +++ b/dotnetv3/CloudFormation/README.md @@ -31,7 +31,7 @@ For prerequisites, see the [README](../README.md#Prerequisites) in the `dotnetv3 ### Get started -- [Hello CloudFormation](../CloudFormation/Actions/HelloCloudFormation.cs#L4) (`DescribeStackResources`) +- [Hello CloudFormation](Actions/HelloCloudFormation.cs#L4) (`DescribeStackResources`) diff --git a/dotnetv3/Pinpoint/Pinpoint.sln b/dotnetv3/Pinpoint/Pinpoint.sln new file mode 100644 index 00000000000..d0075e41772 --- /dev/null +++ b/dotnetv3/Pinpoint/Pinpoint.sln @@ -0,0 +1,46 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.9.34714.143 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SendEmailMessage", "SendEmailMessage\SendEmailMessage.csproj", "{2B5B4F63-6DE6-4116-AE19-F4D65347FA21}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SendSmsMessage", "SendSmsMessage\SendSmsMessage.csproj", "{2DDD1D2D-A6BC-4FB4-B5F0-2C742CFD90EE}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PinpointTests", "PinpointTests\PinpointTests.csproj", "{AADC09C6-0D35-42A8-8407-BAAB9661D80C}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{7BC119BC-3900-4263-9315-FEA7891F4D5B}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{0EDC4692-BBC6-4194-8224-BFB1FD70017C}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {2B5B4F63-6DE6-4116-AE19-F4D65347FA21}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2B5B4F63-6DE6-4116-AE19-F4D65347FA21}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2B5B4F63-6DE6-4116-AE19-F4D65347FA21}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2B5B4F63-6DE6-4116-AE19-F4D65347FA21}.Release|Any CPU.Build.0 = Release|Any CPU + {2DDD1D2D-A6BC-4FB4-B5F0-2C742CFD90EE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2DDD1D2D-A6BC-4FB4-B5F0-2C742CFD90EE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2DDD1D2D-A6BC-4FB4-B5F0-2C742CFD90EE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2DDD1D2D-A6BC-4FB4-B5F0-2C742CFD90EE}.Release|Any CPU.Build.0 = Release|Any CPU + {AADC09C6-0D35-42A8-8407-BAAB9661D80C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AADC09C6-0D35-42A8-8407-BAAB9661D80C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AADC09C6-0D35-42A8-8407-BAAB9661D80C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AADC09C6-0D35-42A8-8407-BAAB9661D80C}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {2B5B4F63-6DE6-4116-AE19-F4D65347FA21} = {7BC119BC-3900-4263-9315-FEA7891F4D5B} + {2DDD1D2D-A6BC-4FB4-B5F0-2C742CFD90EE} = {7BC119BC-3900-4263-9315-FEA7891F4D5B} + {AADC09C6-0D35-42A8-8407-BAAB9661D80C} = {0EDC4692-BBC6-4194-8224-BFB1FD70017C} + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {8791ADC2-25CF-406B-B4B4-3FD3AAF3533A} + EndGlobalSection +EndGlobal diff --git a/dotnetv3/Pinpoint/PinpointTests/PinpointTests.csproj b/dotnetv3/Pinpoint/PinpointTests/PinpointTests.csproj new file mode 100644 index 00000000000..2065c6e3cd7 --- /dev/null +++ b/dotnetv3/Pinpoint/PinpointTests/PinpointTests.csproj @@ -0,0 +1,38 @@ + + + + net6.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + + + + + PreserveNewest + + + PreserveNewest + testsettings.json + + + + diff --git a/dotnetv3/Pinpoint/PinpointTests/SendEmailMessageTests.cs b/dotnetv3/Pinpoint/PinpointTests/SendEmailMessageTests.cs new file mode 100644 index 00000000000..ff560c74501 --- /dev/null +++ b/dotnetv3/Pinpoint/PinpointTests/SendEmailMessageTests.cs @@ -0,0 +1,47 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +using Microsoft.Extensions.Configuration; +using SendEmailMessage; + +namespace PinpointTests; + +public class SendEmailMessageTests +{ + private readonly IConfiguration _configuration; + private readonly string _region; + private readonly string _senderAddress; + private readonly string _toAddress; + private readonly string _appId; + + /// + /// Constructor for the test class. + /// + public SendEmailMessageTests() + { + _configuration = new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("testsettings.json") // Load test settings from .json file. + .AddJsonFile("testsettings.local.json", + true) // Optionally, load local settings. + .Build(); + + _region = "us-east-1"; + _senderAddress = _configuration["SenderAddress"]!; + _toAddress = _configuration["ToAddress"]!; + _appId = _configuration["AppId"]!; + } + + /// + /// SendEmail should return exactly one message response. + /// + [Fact] + [Trait("Category", "Integration")] + public async Task SendEmailMessage_ShouldReturnOneResponse() + { + var messageResponse = await SendEmailMainClass.SendEmailMessage( + _region, _appId, _toAddress, _senderAddress); + Assert.NotNull(messageResponse); + Assert.Single(messageResponse.Result); + } +} \ No newline at end of file diff --git a/dotnetv3/Pinpoint/PinpointTests/SendSmsMessageTests.cs b/dotnetv3/Pinpoint/PinpointTests/SendSmsMessageTests.cs new file mode 100644 index 00000000000..b6fbeb2a920 --- /dev/null +++ b/dotnetv3/Pinpoint/PinpointTests/SendSmsMessageTests.cs @@ -0,0 +1,52 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +using Amazon.Pinpoint; +using Microsoft.Extensions.Configuration; +using SendSmsMessage; + +namespace PinpointTests; + +public class SendSmsMessageTests +{ + private readonly IConfiguration _configuration; + private readonly string _region; + private readonly string _destinationNumber; + private readonly string _originationNumber; + private readonly string _registeredKeyword; + private readonly string _senderId; + private readonly string _appId; + + /// + /// Constructor for the test class. + /// + public SendSmsMessageTests() + { + _configuration = new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("testsettings.json") // Load test settings from .json file. + .AddJsonFile("testsettings.local.json", + true) // Optionally, load local settings. + .Build(); + + _region = "us-east-1"; + _destinationNumber = _configuration["DestinationNumber"]!; + _originationNumber = _configuration["OriginationNumber"]!; + _registeredKeyword = _configuration["RegisteredKeyword"]!; + _senderId = _configuration["SenderId"]!; + _appId = _configuration["AppId"]!; + } + + /// + /// SendSmsMessage should return exactly one message response. + /// + [Fact] + [Trait("Category", "Integration")] + public async Task SendSmsMessage_ShouldReturnOneResponse() + { + var messageResponse = await SendSmsMessageMainClass.SendSmsMessage(_region, _appId, _destinationNumber, + _originationNumber, _registeredKeyword, _senderId, MessageType.TRANSACTIONAL); + Assert.NotNull(messageResponse); + Assert.Single(messageResponse.MessageResponse.Result); + } +} \ No newline at end of file diff --git a/dotnetv3/Pinpoint/PinpointTests/testsettings.json b/dotnetv3/Pinpoint/PinpointTests/testsettings.json new file mode 100644 index 00000000000..a6e2db522d5 --- /dev/null +++ b/dotnetv3/Pinpoint/PinpointTests/testsettings.json @@ -0,0 +1,9 @@ +{ + "SenderAddress": "sender@example.com", + "ToAddress": "recipient@example.com", + "AppId": "ce796be37f32f178af652b26eexample", + "OriginationNumber": "+12344567", + "DestinationNumber": "+12345567", + "RegisteredKeyword": "myKeyword", + "SenderId": "mySenderId" +} diff --git a/dotnetv3/Pinpoint/README.md b/dotnetv3/Pinpoint/README.md new file mode 100644 index 00000000000..ac4b0a74b48 --- /dev/null +++ b/dotnetv3/Pinpoint/README.md @@ -0,0 +1,95 @@ +# Amazon Pinpoint code examples for the SDK for .NET + +## Overview + +Shows how to use the AWS SDK for .NET to work with Amazon Pinpoint. + + + + +_Amazon Pinpoint helps you engage your customers by sending them email, SMS and voice messages, and push notifications._ + +## ⚠ Important + +* Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/) and [Free Tier](https://aws.amazon.com/free/). +* Running the tests might result in charges to your AWS account. +* We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege). +* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services). + + + + +## Code examples + +### Prerequisites + +For prerequisites, see the [README](../README.md#Prerequisites) in the `dotnetv3` folder. + + + + + +### Single actions + +Code excerpts that show you how to call individual service functions. + +- [Send email and text messages](SendEmailMessage/SendEmail.cs#L4) (`SendMessages`) + + + + + +## Run the examples + +### Instructions + +For general instructions to run the examples, see the +[README](../README.md#building-and-running-the-code-examples) in the `dotnetv3` folder. + +Some projects might include a settings.json file. Before compiling the project, +you can change these values to match your own account and resources. Alternatively, +add a settings.local.json file with your local settings, which will be loaded automatically +when the application runs. + +After the example compiles, you can run it from the command line. To do so, navigate to +the folder that contains the .csproj file and run the following command: + +``` +dotnet run +``` + +Alternatively, you can run the example from within your IDE. + + + + + + + +### Tests + +⚠ Running tests might result in charges to your AWS account. + + +To find instructions for running these tests, see the [README](../README.md#Tests) +in the `dotnetv3` folder. + + + + + + +## Additional resources + +- [Amazon Pinpoint Developer Guide](https://docs.aws.amazon.com/pinpoint/latest/developerguide/welcome.html) +- [Amazon Pinpoint API Reference](https://docs.aws.amazon.com/pinpoint/latest/apireference/welcome.html) +- [SDK for .NET Amazon Pinpoint reference](https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/Pinpoint/NPinpoint.html) + + + + +--- + +Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 \ No newline at end of file diff --git a/dotnetv3/Pinpoint/SendEmailMessage/SendEmail.cs b/dotnetv3/Pinpoint/SendEmailMessage/SendEmail.cs new file mode 100644 index 00000000000..b2e66d0fa96 --- /dev/null +++ b/dotnetv3/Pinpoint/SendEmailMessage/SendEmail.cs @@ -0,0 +1,132 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +// snippet-start:[pinpoint.dotnet.pinpoint_send_email_message_api.complete] + +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 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 = @"" + + "\n" + + "\n" + + "\n

Amazon Pinpoint Email Test (AWS SDK for .NET)

" + + "\n

This email was sent using the " + + "\n Amazon Pinpoint API " + + "\n using the AWS SDK for .NET" + + "\n

" + + "\n" + + "\n"; + + // 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 + { + { + 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; + } +} + +// snippet-end:[pinpoint.dotnet.pinpoint_send_email_message_api.complete] \ No newline at end of file diff --git a/dotnetv3/Pinpoint/SendEmailMessage/SendEmailMessage.csproj b/dotnetv3/Pinpoint/SendEmailMessage/SendEmailMessage.csproj new file mode 100644 index 00000000000..76586fd4c1b --- /dev/null +++ b/dotnetv3/Pinpoint/SendEmailMessage/SendEmailMessage.csproj @@ -0,0 +1,27 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + + + + + PreserveNewest + + + PreserveNewest + settings.json + + + + diff --git a/dotnetv3/Pinpoint/SendEmailMessage/settings.json b/dotnetv3/Pinpoint/SendEmailMessage/settings.json new file mode 100644 index 00000000000..9081f59e539 --- /dev/null +++ b/dotnetv3/Pinpoint/SendEmailMessage/settings.json @@ -0,0 +1,5 @@ +{ + "SenderAddress": "sender@example.com", + "ToAddress": "recipient@example.com", + "AppId": "ce796be37f32f178af652b26eexample" +} diff --git a/dotnetv3/Pinpoint/SendSmsMessage/SendSms.cs b/dotnetv3/Pinpoint/SendSmsMessage/SendSms.cs new file mode 100644 index 00000000000..7bc27d882ec --- /dev/null +++ b/dotnetv3/Pinpoint/SendSmsMessage/SendSms.cs @@ -0,0 +1,115 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +// snippet-start:[pinpoint.dotnet.pinpoint_send_sms_message_api.complete] + +using Amazon; +using Amazon.Pinpoint; +using Amazon.Pinpoint.Model; +using Microsoft.Extensions.Configuration; + +namespace SendSmsMessage; + +public class SendSmsMessageMainClass +{ + 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 message. 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 phone number or short code to send the message from. The phone number + // or short code that you specify has to be associated with your Amazon Pinpoint + // account. For best results, specify long codes in E.164 format. + string originationNumber = configuration["OriginationNumber"]!; + + // The recipient's phone number. For best results, you should specify the + // phone number in E.164 format. + string destinationNumber = configuration["DestinationNumber"]!; + + // The 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"]!; + + // The type of SMS message that you want to send. If you plan to send + // time-sensitive content, specify TRANSACTIONAL. If you plan to send + // marketing-related content, specify PROMOTIONAL. + MessageType messageType = MessageType.TRANSACTIONAL; + + // The registered keyword associated with the originating short code. + string? registeredKeyword = configuration["RegisteredKeyword"]; + + // The sender ID to use when sending the message. Support for sender ID + // varies by country or region. For more information, see + // https://docs.aws.amazon.com/pinpoint/latest/userguide/channels-sms-countries.html + string? senderId = configuration["SenderId"]; + + try + { + var response = await SendSmsMessage(region, appId, destinationNumber, + originationNumber, registeredKeyword, senderId, messageType); + Console.WriteLine($"Message sent to {response.MessageResponse.Result.Count} recipient(s)."); + foreach (var messageResultValue in + response.MessageResponse.Result.Select(r => r.Value)) + { + Console.WriteLine($"{messageResultValue.MessageId} Status: {messageResultValue.DeliveryStatus}"); + } + } + catch (Exception ex) + { + Console.WriteLine("The message wasn't sent. Error message: " + ex.Message); + } + } + + public static async Task SendSmsMessage( + string region, string appId, string destinationNumber, string originationNumber, + string? keyword, string? senderId, MessageType messageType) + { + + // The content of the SMS message. + string message = "This message was sent through Amazon Pinpoint using" + + " the AWS SDK for .NET. Reply STOP to opt out."; + + + var client = new AmazonPinpointClient(RegionEndpoint.GetBySystemName(region)); + + SendMessagesRequest sendRequest = new SendMessagesRequest + { + ApplicationId = appId, + MessageRequest = new MessageRequest + { + Addresses = + new Dictionary + { + { + destinationNumber, + new AddressConfiguration { ChannelType = ChannelType.SMS } + } + }, + MessageConfiguration = new DirectMessageConfiguration + { + SMSMessage = new SMSMessage + { + Body = message, + MessageType = MessageType.TRANSACTIONAL, + OriginationNumber = originationNumber, + SenderId = senderId, + Keyword = keyword + } + } + } + }; + SendMessagesResponse response = await client.SendMessagesAsync(sendRequest); + return response; + } +} +// snippet-end:[pinpoint.dotnet.pinpoint_send_sms_message_api.complete] \ No newline at end of file diff --git a/dotnetv3/Pinpoint/SendSmsMessage/SendSmsMessage.csproj b/dotnetv3/Pinpoint/SendSmsMessage/SendSmsMessage.csproj new file mode 100644 index 00000000000..572954f0748 --- /dev/null +++ b/dotnetv3/Pinpoint/SendSmsMessage/SendSmsMessage.csproj @@ -0,0 +1,28 @@ + + + + Exe + net6.0 + enable + enable + + + + + PreserveNewest + + + PreserveNewest + settings.json + + + + + + + + + + + + diff --git a/dotnetv3/Pinpoint/SendSmsMessage/settings.json b/dotnetv3/Pinpoint/SendSmsMessage/settings.json new file mode 100644 index 00000000000..8b43aec6975 --- /dev/null +++ b/dotnetv3/Pinpoint/SendSmsMessage/settings.json @@ -0,0 +1,7 @@ +{ + "OriginationNumber": "+12344567", + "DestinationNumber": "+12345567", + "AppId": "ce796be37f32f178af652b26eexample", + "RegisteredKeyword": "myKeyword", + "SenderId": "mySenderId" +} diff --git a/dotnetv3/Pinpoint/dead-snippets.txt b/dotnetv3/Pinpoint/dead-snippets.txt new file mode 100644 index 00000000000..4864f13cedb --- /dev/null +++ b/dotnetv3/Pinpoint/dead-snippets.txt @@ -0,0 +1,4 @@ +// snippet-start:[pinpoint.dotnet.pinpoint_send_email_smtp.complete] +This code example has been superseded by the examples in +Code examples > Actions and scenarios > Pinpoint +// snippet-end:[pinpoint.dotnet.pinpoint_send_email_smtp.complete] \ No newline at end of file