Beispiel #1
0
        /// <summary>
        ///     Отправка сообщения.
        /// </summary>
        /// <param name="message"> Сообщение в формате <see cref="MimeMessage" />. </param>
        /// <returns> Результат отправки. </returns>
        private async Task <SendMailResult> SendMailAsync(MimeMessage message)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(nameof(MailTransport),
                                                  "Для экземпляра сервиса Email-сообщений был вызвван метод Dispose()");
            }

            try
            {
                await MailTransport.ConnectAsync(
                    Options.Host,
                    Options.Port,
                    GetSecureSocketOptions(Options.EnableSsl));

                await MailTransport.AuthenticateAsync(
                    Options.Login,
                    Options.Password);

                await MailTransport.SendAsync(message);

                await MailTransport.DisconnectAsync(true);

                return(SendMailResult.CreateSuccessResult());
            }
            catch (Exception ex)
            {
                return(SendMailResult.CreateErrorResult(ex));
            }
        }
Beispiel #2
0
        private async Task SendMailsAsync(IReadOnlyList <Task <Mail> > produceMailTasks, WritableDataContext dbContext, CancellationToken cancellationToken)
        {
            try
            {
                for (int i = 0, n = produceMailTasks.Count; i < n; i++)
                {
                    var mail = await produceMailTasks[i].ConfigureAwait(false);

                    if (mail.Message != null)
                    {
                        if (!_smtpClient.IsConnected)
                        {
                            try
                            {
                                await _smtpClient.ConnectAsync(_smtpHost, _smtpPort, _smtpSecurity, cancellationToken).ConfigureAwait(false);
                            }
                            catch (Exception ex) when(!(ex is OperationCanceledException))
                            {
                                var smtpClient = _smtpClient;

                                _smtpClient = _smtpClientFactory();
                                smtpClient.Dispose();
                                throw;
                            }

                            if (_smtpCredentials != null)
                            {
                                await _smtpClient.AuthenticateAsync(_smtpCredentials, cancellationToken).ConfigureAwait(false);
                            }
                        }

                        await SendMailAsync(dbContext, mail, cancellationToken).ConfigureAwait(false);
                    }
                    else if (mail.Error != null)
                    {
                        await HandleMailErrorAsync(dbContext, mail, cancellationToken).ConfigureAwait(false);
                    }
                    else
                    {
                        // we should never get here
                        throw new InvalidOperationException();
                    }
                }
            }
            finally
            {
                if (_smtpClient.IsConnected)
                {
                    await _smtpClient.DisconnectAsync(quit : true, cancellationToken).ConfigureAwait(false);
                }
            }
        }