private async Task SendCheckoutConfirmationMail(BookingDataModel booking) { TemplateDataModel template = await TemplateProvider.GetTemplateByName("slutafregningskvittering"); string mailText = TemplateProvider.MergeText(template.Text, booking); MailAddress to = new MailAddress(booking.ContactEMail, booking.ContactName); MailMessage message = new MailMessage { To = to, Subject = template.Subject, HtmlBody = mailText }; await MailDispatchService.DispatchAsync(message); byte[] mailBody = System.Text.Encoding.UTF8.GetBytes(message.HtmlBody); var addDcoumentCommand = new AddDocumentCommand { Title = message.Subject, MimeType = "text/html", Body = mailBody }; await CommandExecutor.ExecuteAsync(addDcoumentCommand); booking.AddDocument(message.Subject, addDcoumentCommand.OutputDocumentId); }
public async Task ExecuteAsync(SendBookingMailCommand command, IExecutionContext executionContext) { PermissionValidationService.EnforceCustomEntityPermission <CustomEntityUpdatePermission>(BookingCustomEntityDefinition.DefinitionCode, executionContext.UserContext); using (var scope = DomainRepository.Transactions().CreateScope()) { BookingDataModel booking = await BookingProvider.GetBookingById(command.BookingId); await booking.AddLogEntry(CurrentUserProvider, $"Sendt: {command.Subject}."); byte[] mailBody = System.Text.Encoding.UTF8.GetBytes(command.Message); var addDcoumentCommand = new AddDocumentCommand { Title = command.Subject, MimeType = "text/html", Body = mailBody }; await CommandExecutor.ExecuteAsync(addDcoumentCommand); booking.AddDocument(command.Subject, addDcoumentCommand.OutputDocumentId); UpdateCustomEntityDraftVersionCommand updateCmd = new UpdateCustomEntityDraftVersionCommand { CustomEntityDefinitionCode = BookingCustomEntityDefinition.DefinitionCode, CustomEntityId = command.BookingId, Title = booking.MakeTitle(), Publish = true, Model = booking }; await DomainRepository.CustomEntities().Versions().UpdateDraftAsync(updateCmd); MailAddress to = new MailAddress(booking.ContactEMail, booking.ContactName); MailMessage message = new MailMessage { To = to, Subject = command.Subject, HtmlBody = command.Message }; // It is not really a good idea to contact a mail server during a transaction, but ... // 1) I really don't want the mail being registered in the database as "sent" if the mail sending fails. // 2) One can hope that the dispatcher simply adds the message to an outgoing mail queue. // (and, yes, sending mails may fail much later with an "unknown recipient" or similar) await MailDispatchService.DispatchAsync(message); await scope.CompleteAsync(); } }