private void GetRules(string ruleset) { var response = sesclient.DescribeReceiptRuleSetAsync(new DescribeReceiptRuleSetRequest { RuleSetName = ruleset }); try { DescribeReceiptRuleSetResponse result = response.Result; List <ReceiptRule> rules = result.Rules; foreach (ReceiptRule rule in rules) { var recipients = rule.Recipients; foreach (var recipient in recipients) { string[] parts = recipient.Split('@'); var actions = rule.Actions; var name = parts[0]; var domain = parts[1]; string bucket = ""; string topic = ""; foreach (var action in actions) { try { bucket = action.S3Action.BucketName; topic = action.S3Action.TopicArn; } catch (AmazonSimpleEmailServiceException e) { Console.WriteLine("No Topic found in SES S3Action. Message: '{0}' ", e.Message); if (topic == "") { try { topic = action.SNSAction.TopicArn; } catch (AmazonSimpleEmailServiceException e1) { Console.WriteLine("No Topic available from SES SNSAction on {0}. Message:'{1}'", recipient, e1.Message); } } } } if (bucket != "" && topic != "") { var mbx = new FastEmailMailbox(name, domain, bucket, topic); Mailboxes.Add(recipient, mbx); } } } } catch { Console.WriteLine("Unhandled exception while processing SES mailboxes"); } }
public void MonitorEmail(string emailaddr) { if (Exists(emailaddr) && !monitors.ContainsKey(emailaddr)) { FastEmailMailbox mbx = Mailboxes[emailaddr]; FastEmailMonitor monitor = new FastEmailMonitor(); monitor.mbx = mbx; monitor.sqsqueue_name = mbx.S3Bucket + "-" + Process.GetCurrentProcess().Id + "-" + System.Environment.MachineName; create_sqsclient(); try { // create the queue var sqscreateresponse = sqsclient.CreateQueueAsync(new CreateQueueRequest { QueueName = monitor.sqsqueue_name }); CreateQueueResponse sqsresult = sqscreateresponse.Result; monitor.sqsqueue_url = sqsresult.QueueUrl; } catch (AmazonSQSException e) { Console.WriteLine("Exception while creating SQS Queue for {0}: {1}", emailaddr, e.Message); } // get the queue arn try { List <string> attr = new List <string>() { "QueueArn" }; var sqsattrresponse = sqsclient.GetQueueAttributesAsync(new GetQueueAttributesRequest { QueueUrl = monitor.sqsqueue_url, AttributeNames = attr }); GetQueueAttributesResponse sqsresponse = sqsattrresponse.Result; monitor.sqsqueue_arn = sqsresponse.QueueARN; } catch (AmazonSQSException e) { Console.WriteLine("Exception while getting QueueARN SQS Queue for {0}: {1}", emailaddr, e.Message); } // add permission string perm = @"{ ""Version"":""2012-10-17"", ""Statement"":[ { ""Sid"":""Policy-" + monitor.mbx.TopicArn; perm += @""", ""Effect"":""Allow"", ""Principal"":""*"", ""Action"":""sqs:SendMessage"", ""Resource"":""" + monitor.sqsqueue_arn; perm += @""", ""Condition"":{ ""ArnEquals"":{ ""aws:SourceArn"":""" + monitor.mbx.TopicArn; perm += @""" } } } ] }"; var policy = new Dictionary <string, string>(); policy.Add("Policy", perm); try { var qsattrrequest = sqsclient.SetQueueAttributesAsync(new SetQueueAttributesRequest { QueueUrl = monitor.sqsqueue_url, Attributes = policy }); var qsattrresponse = qsattrrequest.Result; } catch (AmazonSQSException e) { Console.WriteLine("Exception while adding permission policy to queue for {0}: {1}", emailaddr, e.Message); } create_snsclient(); try { var snsresponse = snsclient.SubscribeAsync(new SubscribeRequest { Protocol = "sqs", Endpoint = monitor.sqsqueue_arn, TopicArn = monitor.mbx.TopicArn }); var subresult = snsresponse.Result; monitor.subscription_arn = subresult.SubscriptionArn; } catch (AmazonSimpleNotificationServiceException e) { Console.WriteLine("Exception while subscribing to queue for {0}: {1}", emailaddr, e.Message); } monitors.Add(emailaddr, monitor); } }