C# 在ASP.NET MVC(C)中通过Amazon SNS配置Amazon SES反馈通知
你好! 我刚开始学习亚马逊SES。 我想在我的asp.NETMVC网站中使用它 我下载并安装用于VisualStudio的AWS工具包,创建AWS简单控制台应用程序。 所以,我有可以使用AmazonSimpleEmailService客户端发送电子邮件的示例代码 第1部分:C# 在ASP.NET MVC(C)中通过Amazon SNS配置Amazon SES反馈通知,c#,amazon-web-services,amazon-ses,C#,Amazon Web Services,Amazon Ses,你好! 我刚开始学习亚马逊SES。 我想在我的asp.NETMVC网站中使用它 我下载并安装用于VisualStudio的AWS工具包,创建AWS简单控制台应用程序。 所以,我有可以使用AmazonSimpleEmailService客户端发送电子邮件的示例代码 第1部分: using (AmazonSimpleEmailService client = AWSClientFactory.CreateAmazonSimpleEmailServiceClient(RegionEndpoint.US
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.");
}
我试着写:
// 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中创建的消息队列接收消息。请参阅我的答案,以了解在何处可以找到相应的示例-它归结为以下代码:
// 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或ProcessQueuedCompaint,如中所示。我最近不得不解决这个问题,但我找不到一个好的代码示例来说明如何处理SNS跳出通知以及.Net网站的主题订阅请求。下面是我提出的处理来自AmazonSES的SNS跳出通知的WebAPI方法 该代码是在VB中,但任何在线应该能够很容易地得到它为您转换
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解决了这个问题。我现在在Web服务器上处理事件。我已经用这个逻辑创建了一个nuget包 我的密码- [路线识别] [HttpPost] 公共异步任务seNotificationAsync { var body=string.Empty; 使用var reader=new StreamReaderRequest.Body { body=wait reader.ReadToEndAsync; } var notificationProcessor=新notificationProcessor; var result=等待notificationProcessor.ProcessNotificationAsyncbody; //您的处理逻辑。。。 返回StatusCodeStatusCodes.Status200OK; }
非常感谢。但是我实现了在没有SQS的情况下接收SNS通知。我希望这在没有SQS的情况下能起作用。嗨,我写了SES的博客文章。如果您使用HTTP/S端点而不是SQS,则反弹和投诉处理将正常工作。然而,SNS因互联网问题而受到质疑。我在示例代码中使用了SQS,因为SNS到SQS的交付具有明显更高的可靠性,而反弹和投诉处理通常被认为是一项关键操作。
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