Beispiel #1
0
        public async Task <bool> Send(IEmailMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if (message.SendableDate > LocalTime.Now)
            {
                Log.Info($"Skipping Send() command for IEmailMessage ({message.GetId()}). SendableDate is in the future.");
                if (message.IsNew)
                {
                    await Database.Save(message);
                }

                return(false);
            }

            using (var mail = await MessageCreator.Create(message))
            {
                if (mail.To.None() && mail.CC.None() && mail.Bcc.None())
                {
                    Log.Info($"Mail message {message.GetId()} will not be sent as there is no effective recipient.");
                    return(false);
                }

                try
                {
                    await Sending.Raise(new EmailSendingEventArgs(message, mail));

                    await Dispatcher.Dispatch(mail, message);

                    if (!message.IsNew)
                    {
                        await Database.Delete(message);
                    }
                    await Sent.Raise(new EmailSendingEventArgs(message, mail));

                    return(true);
                }
                catch (Exception ex)
                {
                    await SendError.Raise(new EmailSendingEventArgs(message, mail) { Error = ex });
                    await RecordRetry(message);

                    Log.Error(ex, $"Error in sending an email for this EmailQueueItem of '{message.GetId()}' because : " + Environment.NewLine + ex.ToFullMessage());
                    return(false);
                }
            }
        }