在ASP.NET MVC(C#)中通过Amazon SNS配置Amazon SES反馈通知
本文关键字:Amazon 配置 SNS SES 通知 MVC NET ASP | 更新日期: 2023-09-27 18:27:39
您好!我刚开始接触亚马逊SES。我想在我的asp.net mvc(C#)网站上使用它。
我下载并安装适用于Visual Studio的AWS工具包,创建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.");
}
此外,我必须通过亚马逊SNS配置亚马逊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部分,您需要实现从步骤1中创建的AmazonSQS消息队列接收消息。请参阅我对使用AmazonSQS的.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()
,如处理反弹和投诉中所示。
我最近不得不解决这个问题,但我找不到一个关于如何处理来自.Net网站的SNS反弹通知(以及主题订阅请求)的好代码示例。以下是我提出的Web API方法,用于处理来自Amazon SES的SNS反弹通知。
代码是用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
我今天也有同样的问题。我通过在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);
}