Пример #1
0
        public async Task <SendMailPayload> SendGroupMailAsync(string subject, string body, List <string> addresses, CancellationToken cancellationToken)
        {
            Mail newMail;

            try
            {
                var message = new Message()
                {
                    Subject = subject, BodyText = body
                };
                var messageId = await _messageRepository.CreateAsync(message);

                newMail = new Mail()
                {
                    Message = message
                };
                await _mailRepossitory.CreateAsync(newMail);
            }
            catch (Exception ex)
            {
                return(new SendMailPayload(null, $"Failed. Mail was not created. Error: {ex.Message}"));
            }

            try
            {
                using var client = await GetClientAsync(cancellationToken);

                var mail = CreateGroupMail(subject, body, addresses);

                await client.SendAsync(mail, cancellationToken : cancellationToken);

                await client.DisconnectAsync(true, cancellationToken);
            }
            catch (Exception ex)
            {
                var error = await HandleException(ex, newMail);

                return(new SendMailPayload(newMail, error.FailMessage));
            }

            newMail.Status = MailStatus.Successfully;
            await _mailRepossitory.UpdateAsync(newMail);

            return(new SendMailPayload(newMail, null));
        }