4

再会!我刚开始使用 Amazon SES。我想在我的 asp.net mvc (C#) 网站中使用它。

我下载并安装 AWS Toolkit for Visual Studio,创建 AWS 简单控制台应用程序。因此,我有可以使用 AmazonSimpleEmailService 客户端发送电子邮件的示例代码。

第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.");
 }

此外,我必须通过 Amazon SNS 配置 Amazon SES 反馈通知。我用示例代码找到了很好的主题:

第 3 部分: http ://sesblog.amazon.com/post/TxJE1JNZ6T9JXK/Handling-Bounces-and-Complaints

所以,我需要在第 2 部分中获得 ReceiveMessageResponse 响应并将其发送到第 3 部分。

我需要在 C# 中实现以下步骤:设置以下 AWS 组件来处理退回通知:

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.

我试着写:

// 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);

我在正确的轨道上?

我如何实施 4 部分?如何接收 XObject?

谢谢!

4

3 回答 3

3

您走在正确的轨道上 - 对于缺少的第 4 部分,您需要实现从您在步骤 1 中创建的Amazon SQS消息队列接收消息。请参阅我对使用 Amazon SQS 的 .net 应用程序示例的回答,了解在哪里可以找到相应的示例 - 归结为以下代码:

// 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)
    }
}

在循环中,您需要调用ProcessQueuedBounce()ProcessQueuedComplaint()处理退回和投诉中所示。

于 2013-08-21T00:24:28.740 回答
1

我最近不得不解决这个问题,但我找不到一个很好的代码示例来说明如何处理来自 .Net 网站的 SNS 退回通知(以及主题订阅请求)。这是我想出的用于处理来自 Amazon SES 的 SNS 退回通知的 Web API 方法。

该代码在 VB 中,但任何在线VB 到 C# 转换器都应该能够轻松地为您转换。

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
于 2016-03-15T03:29:44.947 回答
0

我今天也有同样的问题。我通过在 SNS 配置中配置 WebHook (https) 解决了这个问题。在网络服务器上,我现在处理事件。我已经创建了一个带有逻辑的 nuget 包。

我的代码 - Nager.AmazonSesNotification

[Route("SesNotification")]
[HttpPost]
public async Task<IActionResult> SesNotificationAsync()
{
    var body = string.Empty;
    using (var reader = new StreamReader(Request.Body))
    {
        body = await reader.ReadToEndAsync();
    }

    var notificationProcessor = new NotificationProcessor();
    var result = await notificationProcessor.ProcessNotificationAsync(body);

    //Your processing logic...

    return StatusCode(StatusCodes.Status200OK);
}
于 2019-08-09T20:50:13.333 回答