Beispiel #1
0
        private (string ContentTemplate, string[] AdditionalContent) GetThresholdEmail(string groupName, ThresholdResult threshold, SyncJob job)
        {
            string increasedThresholdMessage = null;
            string decreasedThresholdMessage = null;
            string contentTemplate           = SyncThresholdBothEmailBody;

            string[] additionalContent;

            increasedThresholdMessage = _localizationRepository.TranslateSetting(
                IncreaseThresholdMessage,
                job.ThresholdPercentageForAdditions.ToString(),
                threshold.IncreaseThresholdPercentage.ToString("F2"));

            decreasedThresholdMessage = _localizationRepository.TranslateSetting(
                DecreaseThresholdMessage,
                job.ThresholdPercentageForRemovals.ToString(),
                threshold.DecreaseThresholdPercentage.ToString("F2"));

            if (threshold.IsAdditionsThresholdExceeded && threshold.IsRemovalsThresholdExceeded)
            {
                additionalContent = new[]
                {
                    groupName,
                    job.TargetOfficeGroupId.ToString(),
                    $"{increasedThresholdMessage}\n{decreasedThresholdMessage}",
                    _gmmResources.LearnMoreAboutGMMUrl,
                    _emailSenderAndRecipients.SupportEmailAddresses
                };
            }
            else if (threshold.IsAdditionsThresholdExceeded)
            {
                additionalContent = new[]
                {
                    groupName,
                    job.TargetOfficeGroupId.ToString(),
                    $"{increasedThresholdMessage}\n",
                    _gmmResources.LearnMoreAboutGMMUrl,
                    _emailSenderAndRecipients.SupportEmailAddresses
                };
            }
            else
            {
                additionalContent = new[]
                {
                    groupName,
                    job.TargetOfficeGroupId.ToString(),
                    $"{decreasedThresholdMessage}\n",
                    _gmmResources.LearnMoreAboutGMMUrl,
                    _emailSenderAndRecipients.SupportEmailAddresses
                };
            }

            return(contentTemplate, additionalContent);
        }
        public async Task SendMailAsync(EmailMessage emailMessage)
        {
            if (emailMessage is null)
            {
                throw new ArgumentNullException(nameof(emailMessage));
            }

            var message = new Message
            {
                Subject = _localizationRepository.TranslateSetting(emailMessage?.Subject),
                Body    = new ItemBody
                {
                    ContentType = BodyType.Text,
                    Content     = _localizationRepository.TranslateSetting(emailMessage?.Content, emailMessage?.AdditionalContentParams)
                }
            };

            if (!string.IsNullOrEmpty(emailMessage?.ToEmailAddresses))
            {
                message.ToRecipients = GetEmailAddresses(emailMessage?.ToEmailAddresses);
            }

            if (!string.IsNullOrEmpty(emailMessage?.CcEmailAddresses))
            {
                message.CcRecipients = GetEmailAddresses(emailMessage?.CcEmailAddresses);
            }

            var securePassword = new SecureString();

            foreach (char c in emailMessage?.SenderPassword)
            {
                securePassword.AppendChar(c);
            }

            await _graphClient.Me
            .SendMail(message, SaveToSentItems : true)
            .Request().WithUsernamePassword(emailMessage?.SenderAddress, securePassword)
            .PostAsync();
        }
Beispiel #3
0
        public async Task SendMailAsync(EmailMessage emailMessage, Guid?runId)
        {
            if (emailMessage is null)
            {
                throw new ArgumentNullException(nameof(emailMessage));
            }

            var message = new Message
            {
                Subject = _localizationRepository.TranslateSetting(emailMessage?.Subject, emailMessage?.AdditionalSubjectParams),
                Body    = new ItemBody
                {
                    ContentType = BodyType.Text,
                    Content     = _localizationRepository.TranslateSetting(emailMessage?.Content, emailMessage?.AdditionalContentParams)
                }
            };

            if (!string.IsNullOrEmpty(emailMessage?.ToEmailAddresses))
            {
                message.ToRecipients = GetEmailAddresses(emailMessage?.ToEmailAddresses);
            }

            if (!string.IsNullOrEmpty(emailMessage?.CcEmailAddresses))
            {
                message.CcRecipients = GetEmailAddresses(emailMessage?.CcEmailAddresses);
            }

            var securePassword = new SecureString();

            foreach (char c in emailMessage?.SenderPassword)
            {
                securePassword.AppendChar(c);
            }

            try
            {
                await _graphClient.Me
                .SendMail(message, SaveToSentItems : true)
                .Request().WithUsernamePassword(emailMessage?.SenderAddress, securePassword)
                .PostAsync();
            }
            catch (ServiceException ex) when(ex.GetBaseException().GetType().Name == "MsalUiRequiredException")
            {
                await _loggingRepository.LogMessageAsync(new LogMessage
                {
                    RunId   = runId,
                    Message = "Email cannot be sent because Mail.Send permission has not been granted."
                });
            }
            catch (ServiceException ex) when(ex.Message.Contains("MailboxNotEnabledForRESTAPI"))
            {
                await _loggingRepository.LogMessageAsync(new LogMessage
                {
                    RunId   = runId,
                    Message = "Email cannot be sent because required licenses are missing in the service account."
                });
            }
            catch (Exception ex)
            {
                await _loggingRepository.LogMessageAsync(new LogMessage
                {
                    RunId   = runId,
                    Message = $"Email cannot be sent due to an unexpected exception.\n{ex}"
                });
            }
        }