コード例 #1
0
ファイル: SmtpService.cs プロジェクト: radtek/Plato
        public async Task <ICommandResult <MailMessage> > SendAsync(MailMessage message)
        {
            var result = new SmtpResult();

            try
            {
                using (var client = GetClient())
                {
                    await client.SendMailAsync(message);

                    if (_logger.IsEnabled(LogLevel.Debug))
                    {
                        _logger.LogDebug("Email sent successfully. From: {0}, To: {1}, Subject: {2}, Date UTC: {3}'",
                                         message.From.Address,
                                         String.Join(",", message.To.Select(t => t.Address).ToArray()),
                                         message.Subject,
                                         DateTime.UtcNow);
                    }

                    return(result.Success(message));
                }
            }
            catch (Exception e)
            {
                if (_logger.IsEnabled(LogLevel.Critical))
                {
                    _logger.LogCritical(e, "A exception occurred whilst sending an email. From: {0}, To: {1}, Subject: {2}, Date UTC: {3}'",
                                        message.From.Address,
                                        String.Join(",", message.To.Select(t => t.Address).ToArray()),
                                        message.Subject,
                                        DateTime.UtcNow);
                }
                return(result.Failed(new CommandError(e.Message, e.StackTrace)));
            }
        }
コード例 #2
0
ファイル: SmtpService.cs プロジェクト: duytuit/OrchardCore
        public async Task <SmtpResult> SendAsync(MailMessage message)
        {
            if (_options?.DefaultSender == null)
            {
                return(SmtpResult.Failed(S["SMTP settings must be configured before an email can be sent."]));
            }

            try
            {
                var mimeMessage = FromMailMessage(message);

                switch (_options.DeliveryMethod)
                {
                case SmtpDeliveryMethod.Network:
                    await SendOnlineMessage(mimeMessage);

                    break;

                case SmtpDeliveryMethod.SpecifiedPickupDirectory:
                    await SendOfflineMessage(mimeMessage, _options.PickupDirectoryLocation);

                    break;

                default:
                    throw new NotSupportedException($"The '{_options.DeliveryMethod}' delivery method is not supported.");
                }

                return(SmtpResult.Success);
            }
            catch (Exception ex)
            {
                return(SmtpResult.Failed(S["An error occurred while sending an email: '{0}'", ex.Message]));
            }
        }
コード例 #3
0
        public async Task <SmtpResult> SendAsync(MailMessage message)
        {
            if (_options?.DefaultSender == null)
            {
                return(SmtpResult.Failed(S[SR.SmtpSettingsMustBeConfigured]));
            }

            var mimeMessage = FromMailMessage(message);

            try
            {
                switch (_options.DeliveryMethod)
                {
                case SmtpDeliveryMethod.Network:
                    await SendOnlineMessage(mimeMessage);

                    break;

                case SmtpDeliveryMethod.SpecifiedPickupDirectory:
                    await SendOfflineMessage(mimeMessage, _options.PickupDirectoryLocation);

                    break;

                default:
                    throw new NotSupportedException(S[SR.DeliveryMethodNotSupported, _options.DeliveryMethod]);
                }

                return(SmtpResult.Success);
            }
            catch (Exception ex)
            {
                return(SmtpResult.Failed(S[SR.ErrorOccurredWhileSendingEmail, ex.Message]));
            }
        }
コード例 #4
0
        public async Task <SmtpResult> SendAsync(MailMessage message)
        {
            if (_options?.DefaultSender == null)
            {
                return(SmtpResult.Failed(S["SMTP settings must be configured before an email can be sent."]));
            }

            if (message.From == null)
            {
                message.From = new MailAddress(_options.DefaultSender);
            }

            try
            {
                using (var client = GetClient())
                {
                    await client.SendMailAsync(message);

                    return(SmtpResult.Success);
                }
            }
            catch (Exception e)
            {
                return(SmtpResult.Failed(S["An error occurred while sending an email: '{0}'", e.Message]));
            }
        }
コード例 #5
0
        /// <summary>
        /// Sends the specified message to an SMTP server for delivery.
        /// </summary>
        /// <param name="message">The message to be sent.</param>
        /// <returns>A <see cref="SmtpResult"/> that holds information about the sent message, for instance if it has sent successfully or if it has failed.</returns>
        /// <remarks>This method allows to send an email without setting <see cref="MailMessage.To"/> if <see cref="MailMessage.Cc"/> or <see cref="MailMessage.Bcc"/> is provided.</remarks>
        public async Task <SmtpResult> SendAsync(MailMessage message)
        {
            if (_options == null)
            {
                return(SmtpResult.Failed(S["SMTP settings must be configured before an email can be sent."]));
            }

            SmtpResult result;
            var        response = default(string);

            try
            {
                // Set the MailMessage.From, to avoid the confusion between _options.DefaultSender (Author) and submitter (Sender)
                var senderAddress = String.IsNullOrWhiteSpace(message.From)
                    ? _options.DefaultSender
                    : message.From;

                if (!String.IsNullOrWhiteSpace(senderAddress))
                {
                    message.From = senderAddress;
                }

                var mimeMessage = FromMailMessage(message);

                if (mimeMessage.From.Count == 0 && mimeMessage.Cc.Count == 0 && mimeMessage.Bcc.Count == 0)
                {
                    return(SmtpResult.Failed(S["The mail message should have at least one of these headers: To, Cc or Bcc."]));
                }

                switch (_options.DeliveryMethod)
                {
                case SmtpDeliveryMethod.Network:
                    response = await SendOnlineMessage(mimeMessage);

                    break;

                case SmtpDeliveryMethod.SpecifiedPickupDirectory:
                    await SendOfflineMessage(mimeMessage, _options.PickupDirectoryLocation);

                    break;

                default:
                    throw new NotSupportedException($"The '{_options.DeliveryMethod}' delivery method is not supported.");
                }

                result = SmtpResult.Success;
            }
            catch (Exception ex)
            {
                result = SmtpResult.Failed(S["An error occurred while sending an email: '{0}'", ex.Message]);
            }

            result.Response = response;

            return(result);
        }
コード例 #6
0
        public async Task <ICommandResult <MailMessage> > SendAsync(MailMessage message)
        {
            var result = new SmtpResult();

            // Ensure we've configured required email settings
            if (_smtpSettings?.DefaultFrom == null)
            {
                return(result.Failed("Email settings must be configured before an email can be sent."));
            }

            // Use application email if no from is specified
            if (message.From == null)
            {
                message.From = new MailAddress(_smtpSettings.DefaultFrom);
            }

            // Invoke EmailSending subscriptions
            foreach (var handler in _broker.Pub <MailMessage>(this, "EmailSending"))
            {
                message = await handler.Invoke(new Message <MailMessage>(message, this));
            }

            // Attempt to send the email
            var sendResult = await _smtpService.SendAsync(message);

            if (sendResult.Succeeded)
            {
                // Invoke EmailSent subscriptions
                foreach (var handler in _broker.Pub <MailMessage>(this, "EmailSent"))
                {
                    message = await handler.Invoke(new Message <MailMessage>(message, this));
                }
                return(result.Success(message));
            }

            return(result.Failed(sendResult.Errors.ToArray()));
        }
コード例 #7
0
        /// <inheritdoc/>
        public async Task <SmtpResult> SendAsync(MailMessage message)
        {
            if (_gmailSetting?.DefaultSender == null)
            {
                return(SmtpResult.Failed(S["Gmail settings must be configured before an email can be sent."]));
            }

            try
            {
                message.From = string.IsNullOrWhiteSpace(message.From)
                    ? _gmailSetting.DefaultSender
                    : message.From;

                var mimeMessage = FromMailMessage(message);

                await SendMessage(mimeMessage);

                return(SmtpResult.Success);
            }
            catch (Exception ex)
            {
                return(SmtpResult.Failed(S["An error occurred while sending an email: '{0}'", ex.Message]));
            }
        }