public void CanConvertMailMessageToSendEmailRequest() { var emailService = default(SimpleEmailService); var email = default(Email); var sendEmailRequest = default(SendEmailRequest); "Given I have an email".Context(() => { var fakeEmailRenderer = A.Fake <IEmailViewRenderer>(); var fakeEmailParser = A.Fake <IEmailParser>(); var fakeAmazonEmailService = A.Fake <global::Amazon.SimpleEmail.AmazonSimpleEmailService>(); email = new Email("test"); A.CallTo(() => fakeEmailRenderer.Render(email, null)).Returns(""); A.CallTo(() => fakeEmailParser.Parse("", email)).Returns(new MailMessage("*****@*****.**", "*****@*****.**", "Subject", "Body")); A.CallTo(() => fakeAmazonEmailService.SendEmail(A <SendEmailRequest> .Ignored)).Invokes(x => sendEmailRequest = x.Arguments.Get <SendEmailRequest>(0)) .Returns(new SendEmailResponse()); emailService = new SimpleEmailService(fakeEmailRenderer, fakeEmailParser, fakeAmazonEmailService); }); "When I create an Amazon SES Request".Do(() => emailService.Send(email)); "Then the destination address is set".Observation(() => sendEmailRequest.Destination.ToAddresses[0].ShouldBeEquivalentTo("*****@*****.**")); "And the source address is set".Observation(() => sendEmailRequest.Source.ShouldBeEquivalentTo("*****@*****.**")); "And the subject is set".Observation(() => sendEmailRequest.Message.Subject.Data.ShouldBeEquivalentTo("Subject")); "And the body is set".Observation(() => sendEmailRequest.Message.Body.Text.Data.ShouldBeEquivalentTo("Body")); }
public void CanConvertMailMessageToSendEmailRequest() { var emailService = default(SimpleEmailService); var email = default(Email); var sendEmailRequest = default(SendEmailRequest); "Given I have an email".Context(() => { var fakeEmailRenderer = A.Fake<IEmailViewRenderer>(); var fakeEmailParser = A.Fake<IEmailParser>(); var fakeAmazonEmailService = A.Fake<global::Amazon.SimpleEmail.AmazonSimpleEmailService>(); email = new Email("test"); A.CallTo(() => fakeEmailRenderer.Render(email, null)).Returns(""); A.CallTo(() => fakeEmailParser.Parse("", email)).Returns(new MailMessage("*****@*****.**","*****@*****.**","Subject","Body")); A.CallTo(() => fakeAmazonEmailService.SendEmail(A<SendEmailRequest>.Ignored)).Invokes(x => sendEmailRequest = x.Arguments.Get<SendEmailRequest>(0)) .Returns(new SendEmailResponse()); emailService = new SimpleEmailService(fakeEmailRenderer, fakeEmailParser, fakeAmazonEmailService); }); "When I create an Amazon SES Request".Do(() => emailService.Send(email)); "Then the destination address is set".Observation(() => sendEmailRequest.Destination.ToAddresses[0].ShouldBeEquivalentTo("*****@*****.**")); "And the source address is set".Observation(() => sendEmailRequest.Source.ShouldBeEquivalentTo("*****@*****.**")); "And the subject is set".Observation(() => sendEmailRequest.Message.Subject.Data.ShouldBeEquivalentTo("Subject")); "And the body is set".Observation(() => sendEmailRequest.Message.Body.Text.Data.ShouldBeEquivalentTo("Body")); }
public ActionResult Index() { var emailService = new SimpleEmailService(); var renderedTemplate = emailService.GetRenderedTemplate(ControllerContext, "~/Views/MailTemplates/Template1.cshtml", new SampleEmailModel { Title = "Mr.", Name = "Willy Wonka" }); emailService.Send(new SimpleMailMessage { To = "*****@*****.**", Cc = "[email protected], [email protected]", From = "*****@*****.**", Bcc = "*****@*****.**", Subject = "Blah", Body = renderedTemplate, IsHtml = true }); return(View(new FooViewModel { Age = 38, Name = "Kevin" })); }
public virtual async Task Next(SimpleEmailService <S3ReceiptAction> message, ILambdaContext context) { if (NextHandler == null) { return; } await NextHandler.Handle(message, context); }
public override async Task Handle(SimpleEmailService <S3ReceiptAction> message, ILambdaContext context) { var document = new EmailMessage(message); await _context.SaveAsync(document, new DynamoDBOperationConfig { OverrideTableName = TableName }); Log($"Message stored in DynamoDB under key {document.Id}"); await Next(message, context); }
public override async Task Handle(SimpleEmailService <S3ReceiptAction> message, ILambdaContext context) { LogPrefix = $"[{message.Mail.MessageId}] "; Log($"Message received from {message.Mail.Source}"); Log($"Message destined for {message.Receipt.Recipients.Humanize(",")}"); Log($"Message saved in bucket {message.Receipt.Action.BucketName} as {message.Receipt.Action.ObjectKey}"); await Next(message, context); }
public async Task FunctionHandlerSES(SimpleEmailService <S3ReceiptAction> sesEvent, ILambdaContext context) { if (sesEvent == null) { LambdaLogger.Log("Function invoked but no event passed. Exiting..."); return; } LambdaLogger.Log($"Function invoked directly from {sesEvent.GetType().Name} for 1 notification"); await _handlerChain.Handle(sesEvent, context); LambdaLogger.Log("Function invocation completed"); }
/// <summary> /// A simple function that takes a string and does a ToUpper /// </summary> /// <param name="input"></param> /// <param name="context"></param> /// <returns></returns> public async Task <AmazonWebServiceResponse> SendEmailHandlerAsync(Event input, ILambdaContext context) { var logs = new CloudWatchLogsService() { LogGroupName = @"/aws/codebuild/" + input.Detail.ProjectName, LogStreamName = input.Detail.BuildId.Split('/').Last().Split(':').Last(), LogRowLimit = 10000 }; var email = new SimpleEmailService() { Subject = String.Format("Build {0} failed on phase {1} at {2}.", input.Detail.ProjectName, input.Detail.CurrentPhase, input.Time), Body = String.Format("The build with id of {0} has failed. \n" + "Here is the log transcript: \n {1}", input.Detail.BuildId, await logs.GetCloudWatchLogEventsAsync()) }; return(await email.SendEmailAsync()); }
public override async Task Handle(SimpleEmailService <S3ReceiptAction> message, ILambdaContext context) { var bucket = message.Receipt.Action.BucketName; var mailbox = GetDestinationMailbox(message.Mail) ?? message.Receipt.Recipients.FirstOrDefault(); // Todo: tidy up var objectName = $"{message.Mail.MessageId}.{EmailMessageExtension}"; // add extension var originalKey = message.Receipt.Action.ObjectKey; var destinationKeyPrefix = string.IsNullOrEmpty(mailbox) // if BCCd, ? message.Receipt.Action.ObjectKeyPrefix // leave as-is : $"mailbox/{mailbox}/"; // otherwise, move to mailbox var destinationKey = $"{destinationKeyPrefix}{objectName}"; // append object name // Move the file in S3 to its destination await MoveMessage(message.Mail, bucket, originalKey, destinationKey); // Update S3 location information for the next handler message.Receipt.Action.ObjectKeyPrefix = destinationKeyPrefix; message.Receipt.Action.ObjectKey = destinationKey; await Next(message, context); }
public EmailMessage(SimpleEmailService <S3ReceiptAction> message) { Id = message.Mail.MessageId; Timestamp = message.Mail.Timestamp; Source = message.Mail.Source; SourceName = message.Mail.CommonHeaders.From.FirstOrDefault(); Destination = message.Mail.Destination.FirstOrDefault(); DestinationName = message.Mail.CommonHeaders.To.FirstOrDefault(); Subject = message.Mail.CommonHeaders.Subject; Sent = message.Mail.CommonHeaders.Date; ReturnPath = message.Mail.CommonHeaders.ReturnPath; MessageId = message.Mail.CommonHeaders.MessageId; Headers = message.Mail.Headers; SecurityAnalysis = SecurityAnalysis.CreateFrom(message.Receipt); Metadata = message.Receipt.Action; Raw = JsonConvert.SerializeObject(message); }
public override async Task Handle(SimpleEmailService <S3ReceiptAction> message, ILambdaContext context) { try { Log("Retrieving MIME message from S3 bucket"); var rawMessageRequest = await _s3client.GetObjectAsync(message.Receipt.Action.BucketName, message.Receipt.Action.ObjectKey); Log($"MIME message retrieved: {rawMessageRequest.ContentLength.Bytes().Humanize()}"); using var rawMessageStream = rawMessageRequest.ResponseStream; var mimeMessage = await MimeMessage.LoadAsync(rawMessageStream); Log("Message loaded & parsed"); Log($"Uploading to {Configuration.IMAPServer}, debug logging {(Configuration.IMAPDebugLogging ? "on" : "off")}"); await _imapClient.ConnectAsync(Configuration.IMAPServer, options : SecureSocketOptions.SslOnConnect); await _imapClient.AuthenticateAsync(Configuration.IMAPUsername, Configuration.IMAPPassword); var folder = await _imapClient.GetFolderAsync(Configuration.IMAPDestinationFolder); var open = await folder.OpenAsync(FolderAccess.ReadWrite); var upload = await folder.AppendAsync(mimeMessage, Configuration.IMAPMarkAsRead?MessageFlags.Seen : MessageFlags.Recent, progress : new LambdaProgressLogger(Log)); Log($"Stored message with ID {upload.Value.Id}"); } catch (Exception ex) { Log(ex.ToString()); throw; // ensure Lambda fails } finally { await _imapClient.DisconnectAsync(true); await Next(message, context); } }
public virtual IHandler SetNext(IHandler handler) => NextHandler = handler; // returns assigned handler public abstract Task Handle(SimpleEmailService <S3ReceiptAction> message, ILambdaContext context);