AWS Code Sample


SnsSendMessage demonstrates how to send a message to a topic using Amazon SNS.

/******************************************************************************* * Copyright 2009-2019, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). You may * not use this file except in compliance with the License. A copy of the * License is located at * * * * or in the "license" file accompanying this file. This file is * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the specific * language governing permissions and limitations under the License. *******************************************************************************/ /* Build Don't forget to use the Visual Studio command prompt Tested with csc /version == (C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\bin\Roslyn) AWSSDK.Core.dll (C:\Users\USERNAME\.nuget\packages\awssdk.core\3.3.100\lib\net45) AWSSDK.SimpleNotificationService.dll (C:\Users\USERNAME\.nuget\packages\awssdk.simplenotificationservice\\lib\net45) csc SnsSendMessage.cs -reference:AWSSDK.Core.dll -reference:AWSSDK.SimpleNotificationService.dll Get the list of AWSSDK Nuget packages at: */ using System; using System.Linq; using System.Threading.Tasks; using Amazon.SimpleNotificationService; using Amazon.SimpleNotificationService.Model; namespace SnsSendMessage { class Program { static void Main(string[] args) { /* Topic ARNs must be in the correct format: * arn:aws:sns:REGION:ACCOUNT_ID:NAME * * where: * REGION is the region in which the topic is created, such as us-west-2 * ACCOUNT_ID is your (typically) 12-character account ID * NAME is the name of the topic */ string topicArn = args[0]; string message = "Hello at " + DateTime.Now.ToShortTimeString(); var client = new AmazonSimpleNotificationServiceClient(region: Amazon.RegionEndpoint.USWest2); var request = new PublishRequest { Message = message, TopicArn = topicArn }; try { var response = client.Publish(request); Console.WriteLine("Message sent to topic:"); Console.WriteLine(message); } catch (Exception ex) { Console.WriteLine("Caught exception publishing request:"); Console.WriteLine(ex.Message); } } } }

Sample Details

Service: sns

Last tested: Sep 18, 2019

Author: Doug-AWS

Type: full-example

On this page: