Amazon Pinpoint를 사용한 예제 AWS SDK for .NET - AWS SDK코드 예제

AWS 문서 AWS SDK SDK 예제 GitHub 리포지토리에 더 많은 예제가 있습니다.

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

Amazon Pinpoint를 사용한 예제 AWS SDK for .NET

다음 코드 예제는 AWS SDK for .NET with Amazon Pinpoint를 사용하여 작업을 수행하고 일반적인 시나리오를 구현하는 방법을 보여줍니다.

작업은 대규모 프로그램에서 발췌한 코드이며 컨텍스트에 맞춰 실행해야 합니다. 작업은 개별 서비스 함수를 호출하는 방법을 보여 주며 관련 시나리오와 교차 서비스 예시에서 컨텍스트에 맞는 작업을 볼 수 있습니다.

시나리오는 동일한 서비스 내에서 여러 함수를 호출하여 특정 태스크를 수행하는 방법을 보여주는 코드 예시입니다.

각 예제에는 컨텍스트에서 코드를 설정하고 실행하는 방법에 대한 지침을 찾을 수 있는 링크가 포함되어 있습니다. GitHub

주제

작업

다음 코드 예시에서는 SendMessages을 사용하는 방법을 보여 줍니다.

AWS SDK for .NET
참고

자세한 내용은 여기를 참조하십시오 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

이메일 메시지를 전송합니다.

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

SMS메시지 보내기.

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<SendMessagesResponse> 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<string, AddressConfiguration> { { 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; } }