Exemplo n.º 1
0
        public async Task <IActionResult> DownloadAsPdf(Guid id)
        {
            string validationUrl = GetTicketValidationUriFormatString();
            Stream stream;

            try
            {
                stream = await _pdfTicketService.GeneratePdfAsync(id, validationUrl);
            }
            catch (TicketNotFoundException)
            {
                return(NotFound());
            }
            string fileDownloadName = "ticket.pdf";

            Response.Headers.Add("Content-Disposition", $"inline; filename={fileDownloadName}");
            return(File(stream, "application/pdf"));
        }
Exemplo n.º 2
0
        private async Task SendTicketByMailAsync(TicketDeliveryData args,
                                                 string ticketValidationUriFormat,
                                                 string homepageUrl)
        {
            if (args.MailSettings == null)
            {
                throw new EventManagementException(
                          "The mail settings haven't been configured for the event.");
            }
            ModelValidator.Validate(args.MailSettings);
            if (string.IsNullOrEmpty(args.Ticket.Mail))
            {
                await LogAuditEvent(args, false, "Ticket konnte nicht versendet werden, weil keine E-Mail-Adresse vorhanden ist.");

                throw new EventManagementException("The ticket has no email address.");
            }

            System.IO.Stream stream = await _pdfTicketService
                                      .GeneratePdfAsync(args.Ticket.Id, ticketValidationUriFormat);

            IList <string> recipients = new[] { args.Ticket.Mail };

            if (args.MailSettings.EnableDemoMode)
            {
                if (args.MailSettings.DemoEmailRecipients.Any())
                {
                    _logger.LogWarning("Demo Mode is enabled. The e-mails will be sent to a predefined list of recipients only.");
                    recipients = args.MailSettings.DemoEmailRecipients
                                 .Select(r => r.EmailAddress)
                                 .ToList();
                }
                else
                {
                    _logger.LogWarning("Demo Mode is enabled, but no test recipients are defined.");
                }
            }

            var message = new EmailMessage
            {
                From        = { args.MailSettings.SenderAddress },
                To          = recipients,
                Subject     = args.MailSettings.Subject,
                Body        = args.MailSettings.Body,
                Attachments =
                {
                    new EmailAttachment
                    {
                        FileName    = $"ticket-{args.Ticket.TicketNumber}.pdf",
                        ContentType = "application/pdf",
                        Stream      = stream
                    }
                }
            };

            if (!string.IsNullOrEmpty(args.MailSettings.ReplyToAddress))
            {
                message.ReplyTo.Add(args.MailSettings.ReplyToAddress);
            }

            var mail = await EmailTemplateService
                       .RenderTicketMailAsync(message, args.Ticket, homepageUrl);

            try
            {
                await _emailService.SendMailAsync(args.MailSettings, mail);
            }
            catch
            {
                await LogAuditEvent(args, false);

                throw;
            }

            await _ticketDataRepo.UpdateDeliveryStatusAsync(args.Ticket.Id,
                                                            true, DateTime.UtcNow, TicketDeliveryType.Email);

            await LogAuditEvent(args, true);
        }