Ejemplo n.º 1
0
        public async Task SendEmail_WithOneParamNull_ShouldThrowArgumentNullException(string emailAddress, string subject, string body)
        {
            string nullParam = null;

            if (emailAddress == null)
            {
                nullParam = nameof(emailAddress);
            }
            else if (subject == null)
            {
                nullParam = nameof(subject);
            }
            else if (body == null)
            {
                nullParam = nameof(body);
            }
            var exception = await Assert.ThrowsAsync <ArgumentNullException>(() => _mailgunEmailService.SendEmailAsync(emailAddress, subject, body));

            Assert.Equal(nullParam, exception.ParamName);
        }
        public static void Handler(SQSEvent sqsEvent, ILambdaContext context)
        {
            context.Logger.LogLine($"Received SQSEvent with {sqsEvent.Records.Count} SQSMessage record(s)");

            context.Logger.LogLine("Retrieving ApiKey");
            string apiKey = String.Empty;

            using (var ssmClient = new AmazonSimpleSystemsManagementClient())
            {
                var response = ssmClient.GetParameterAsync(
                    new Amazon.SimpleSystemsManagement.Model.GetParameterRequest
                {
                    Name           = Environment.GetEnvironmentVariable("Email__ServiceApiKeyName"),
                    WithDecryption = true
                }
                    ).GetAwaiter().GetResult();
                apiKey = response.Parameter.Value;
            }

            context.Logger.LogLine("Creating EmailService object");
            var emailService = new MailgunEmailService(new MailgunEmailService.Options
            {
                FromAddress   = Environment.GetEnvironmentVariable("Email__FromAddress"),
                MailDomain    = Environment.GetEnvironmentVariable("Email__MailDomain"),
                ServiceUrl    = Environment.GetEnvironmentVariable("Email__ServiceUrl"),
                ServiceApiKey = apiKey
            });

            foreach (var sqsMessage in sqsEvent.Records)
            {
                context.Logger.LogLine($"Processing message {sqsMessage.MessageId}");

                // Parse out recipient, subject, and email body
                var strArray  = sqsMessage.Body.Split(MessageSeparator, 3, StringSplitOptions.None);
                var recipient = strArray[0];
                context.Logger.LogLine($"({sqsMessage.MessageId}) To: {recipient}");
                var subject = strArray[1];
                context.Logger.LogLine($"({sqsMessage.MessageId}) Subject: {subject}");
                var body = strArray[2];
                context.Logger.LogLine($"({sqsMessage.MessageId}) Subject: {body}");

                // Send the e-mail
                var emailMessage = new Email.EmailMessage
                {
                    EmailAddress = recipient,
                    Subject      = subject,
                    Body         = body
                };
                var result = emailService.SendEmailAsync(emailMessage).GetAwaiter().GetResult();
                context.Logger.LogLine($"({sqsMessage.MessageId}) Email service returned success: {result.Success} details: {result.Details}");
            }
        }