Setting up Amazon SAS feedback notifications through ASUS Amazon in ASP.NET MVC (C #)

Good afternoon! I just started working with Amazon. I want to use this on my asp.net mvc website (C #).

I download and install the AWS Toolkit for Visual Studio, create a simple AWS console application. So, I have an example code that can send email using the AmazonSimpleEmailService client.

PART 1:

using (AmazonSimpleEmailService client = AWSClientFactory.CreateAmazonSimpleEmailServiceClient(RegionEndpoint.USEast1)) { var sendRequest = new SendEmailRequest { Source = senderAddress, Destination = new Destination { ToAddresses = new List<string> { receiverAddress } }, Message = new Message { Subject = new Content("Sample Mail using SES"), Body = new Body { Text = new Content("Sample message content.") } } }; Console.WriteLine("Sending email using AWS SES..."); SendEmailResponse response = client.SendEmail(sendRequest); Console.WriteLine("The email was sent successfully."); } 

In addition, I have to set up Amazon SES feedback notifications through Amazon SNS. I find a nice topic with sample code:

PART 3: http://sesblog.amazon.com/post/TxJE1JNZ6T9JXK/Handling-Bounces-and-Complaints

So, I need to do PART 2, where I get a ReceiveMessageResponse response and send it to PART 3.

I need to implement the following steps in C #: Configure the following AWS components to handle failure notifications:

 1. Create an Amazon SQS queue named ses-bounces-queue. 2. Create an Amazon SNS topic named ses-bounces-topic. 3. Configure the Amazon SNS topic to publish to the SQS queue. 4. Configure Amazon SES to publish bounce notifications using ses-bounces-topic to ses-bounces-queue. 

I am trying to write it:

 // 1. Create an Amazon SQS queue named ses-bounces-queue. AmazonSQS sqs = AWSClientFactory.CreateAmazonSQSClient(RegionEndpoint.USWest2); CreateQueueRequest sqsRequest = new CreateQueueRequest(); sqsRequest.QueueName = "ses-bounces-queue"; CreateQueueResponse createQueueResponse = sqs.CreateQueue(sqsRequest); String myQueueUrl; myQueueUrl = createQueueResponse.CreateQueueResult.QueueUrl; // 2. Create an Amazon SNS topic named ses-bounces-topic AmazonSimpleNotificationService sns = new AmazonSimpleNotificationServiceClient(RegionEndpoint.USWest2); string topicArn = sns.CreateTopic(new CreateTopicRequest { Name = "ses-bounces-topic" }).CreateTopicResult.TopicArn; // 3. Configure the Amazon SNS topic to publish to the SQS queue sns.Subscribe(new SubscribeRequest { TopicArn = topicArn, Protocol = "https", Endpoint = "ses-bounces-queue" }); // 4. Configure Amazon SES to publish bounce notifications using ses-bounces-topic to ses-bounces-queue clientSES.SetIdentityNotificationTopic(XObject); 

Am I on the right track?

How can I implement 4 parts? How to get XObject?

Thanks!

+4
source share
2 answers

You are on the right track - for the missing part 4, you need to implement receiving messages from the Amazon SQS message queue that you created in step 1. See my answer to .net application example using Amazon SQS , where you can find the corresponding example - it comes down to the following code:

 // receive a message ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(); receiveMessageRequest.QueueUrl = myQueueUrl; ReceiveMessageResponse receiveMessageResponse = sqs. ReceiveMessage(receiveMessageRequest); if (receiveMessageResponse.IsSetReceiveMessageResult()) { Console.WriteLine("Printing received message.\n"); ReceiveMessageResult receiveMessageResult = receiveMessageResponse. ReceiveMessageResult; foreach (Message message in receiveMessageResult.Message) { // process the message (see below) } } 

Inside the loop, you need to call either ProcessQueuedBounce() or ProcessQueuedComplaint() , as shown in Handling bounces and complaints .

+3
source

I recently had to solve this problem, and I could not find a good code example on how to process the SNS failure notification (as well as the topic subscription request) from the .Net website. Here is the Web API method I came across to handle Amazon SAS SNS failure notifications.

The code is in VB, but any online VB to C # converter should be able to easily convert it for you.

 Imports System.Web.Http Imports Amazon.SimpleNotificationService Namespace Controllers Public Class AmazonController Inherits ApiController <HttpPost> <Route("amazon/bounce-handler")> Public Function HandleBounce() As IHttpActionResult Try Dim msg = Util.Message.ParseMessage(Request.Content.ReadAsStringAsync().Result) If Not msg.IsMessageSignatureValid Then Return BadRequest("Invalid Signature!") End If If msg.IsSubscriptionType Then msg.SubscribeToTopic() Return Ok("Subscribed!") End If If msg.IsNotificationType Then Dim bmsg = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Message)(msg.MessageText) If bmsg.notificationType = "Bounce" Then Dim emails = (From e In bmsg.bounce.bouncedRecipients Select e.emailAddress).Distinct() If bmsg.bounce.bounceType = "Permanent" Then For Each e In emails 'this email address is permanantly bounced. don't ever send any mails to this address. remove from list. Next Else For Each e In emails 'this email address is temporarily bounced. don't send any more emails to this for a while. mark in db as temp bounce. Next End If End If End If Catch ex As Exception 'log or notify of this error to admin for further investigation End Try Return Ok("done...") End Function Private Class BouncedRecipient Public Property emailAddress As String Public Property status As String Public Property diagnosticCode As String Public Property action As String End Class Private Class Bounce Public Property bounceSubType As String Public Property bounceType As String Public Property reportingMTA As String Public Property bouncedRecipients As BouncedRecipient() Public Property timestamp As DateTime Public Property feedbackId As String End Class Private Class Mail Public Property timestamp As DateTime Public Property source As String Public Property sendingAccountId As String Public Property messageId As String Public Property destination As String() Public Property sourceArn As String End Class Private Class Message Public Property notificationType As String Public Property bounce As Bounce Public Property mail As Mail End Class End Class End Namespace 
+1
source

Source: https://habr.com/ru/post/1496796/


All Articles