public async Task <ReportMessage> CreateMessageAsync(AbstractReport reportData)
        {
            var msg = new MailMessage {
                IsBodyHtml = true
            };

            var mailAddressViewModel = new MailAddressViewModel(reportData, _logger);
            var recipients           = mailAddressViewModel.GetRecipientAdrresses();

            msg.From = mailAddressViewModel.From;
            msg.To.AddRange(recipients[RecipientType.TO]);
            msg.CC.AddRange(recipients[RecipientType.CC]);

            _logger.LogInformation("Sending mail for to address - " +
                                   string.Join(";", msg.To.Select(mailAddress => mailAddress.Address)));

            _logger.LogInformation("Sending mail for cc address - " +
                                   string.Join(";", msg.CC.Select(mailAddress => mailAddress.Address)));

            _logger.LogInformation("Creating view model for generating xml");
            var emailReportViewModel = new EmailReportViewModel(reportData, _emailReportConfiguration);

            _logger.LogInformation("Generated view model");

            msg.Subject = emailReportViewModel.EmailSubject;
            msg.Body    = GenerateBodyFromViewModel(emailReportViewModel);

            return(await Task.FromResult(new ReportMessage()
            {
                MailMessage = msg, SmtpConfiguration = reportData.SmtpConfiguration
            }));
        }
Example #2
0
        public IHttpActionResult Post([FromBody] EmailReportViewModel data)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            if (data.UserId == -1 && !User.IsInRole(ConnectRoles.Admin))
            {
                return(Unauthorized());
            }

            using (var context = new ConnectContext())
            {
                switch (data.ReportType)
                {
                case ApiReportType.RecentCalibrations:
                    var recentCalibrations = context.RecentCalibrations(ConnectUser, data.UserId, data.From);
                    SendEmail(data.Recipient, "Your Recent Calibrations Report", EmailHelper.GetCalibrationDataTable(recentCalibrations));
                    break;

                case ApiReportType.CalibrationsDue:
                    var calibrationsDue = context.CalibrationsDue(ConnectUser, data.UserId, data.From, data.To.GetValueOrDefault());
                    SendEmail(data.Recipient, "Your Calibrations Due Report", EmailHelper.GetCalibrationDataTable(calibrationsDue));
                    break;
                }
            }

            return(Ok());
        }
        protected virtual string GenerateBodyFromViewModel(EmailReportViewModel viewModel)
        {
            using (new PerformanceMeasurementBlock("Generating Html content for message body", _logger))
            {
                var xml = GetXmlContent(viewModel);

                _logger.LogInformation("Generating html content for email body");

                return(GetHtml(xml));
            }
        }
        private string GetXmlContent(EmailReportViewModel viewModel)
        {
            _logger.LogInformation("Generating xml from view model");

            var xsSubmit = new DataContractSerializer(typeof(EmailReportViewModel));

            using (var stringWriter = new StringWriter())
                using (var writer = new XmlTextWriterWithoutNamespace(stringWriter))
                {
                    try
                    {
                        xsSubmit.WriteObject(writer, viewModel);
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex.Message);
                    }

                    _logger.LogInformation($"Generated xml content from view model - {stringWriter}");
                    return(stringWriter.ToString());
                }
        }
Example #5
0
        public IActionResult EmailReport([FromBody] EmailReportViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var request = new EmailReportRequest {
                ClubId         = club.Guid,
                EventId        = model.EventId,
                EmailAddresses = model.EmailAddress
            };

            if (model.SendMeACopy.HasValue && model.SendMeACopy.Value)
            {
                var user = userManager.GetUserAsync(User).Result;
                request.EmailAddresses.Add(user.Email);
            }

            var response = eventService.EmailEventReport(request);

            return(response.RequestIsFulfilled ? Ok() : BadRequest(response.Errors) as IActionResult);
        }