示例#1
0
        /// <summary>
        /// Creates an instance of <see cref="DaMailProviderBase"/>.
        /// </summary>
        public DaMailProviderBase()
        {
            var config = DaMailConfigurationManager.GetConfiguration();

            DefaultFromName  = config.DefaultSenderName;
            DefaultFromEmail = config.DefaultSenderEmail;

            var provider = config.Providers.GetByName(config.DefaultProvider);

            SmtpServer = new DaSmtpServerInfo()
            {
                ApiKey   = provider.ApiKey,
                UserId   = provider.UserId,
                Host     = provider.HostName,
                Password = provider.Password,
                Port     = provider.Port,
                UseSsl   = provider.UseSsl
            };
        }
        /// <summary>
        /// Creates an instance of a <see cref="IDaMailProvider"/> type.
        /// </summary>
        /// <returns>Returns an instance of <see cref="IDaMailProvider"/>.</returns>
        public static IDaMailProvider GetProvider()
        {
            DaProviderConfigurationElement providerConfig = null;

            var config = DaMailConfigurationManager.GetConfiguration();

            if (config == null)
            {
                return(null);
            }

            providerConfig = config.Providers.GetByName(config.DefaultProvider);

            if (providerConfig == null)
            {
                return(null);
            }

            var type = Type.GetType(providerConfig.Type);

            return(Activator.CreateInstance(type) as IDaMailProvider);
        }
        /// <summary>
        /// Asynchronously sends a mail.
        /// </summary>
        /// <param name="message">The mail message object.</param>
        /// <returns>A task that represents the asynchronous save operation.</returns>
        public override Task SendAsync(MailMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if (message.To == null)
            {
                throw new InvalidOperationException("Must have the email address to whom the email needs to be sent.");
            }

            if (message.From == null)
            {
                var config = DaMailConfigurationManager.GetConfiguration();
                message.From = new MailAddress(config.DefaultSenderEmail);
            }

            var smtpClient = new SmtpClient();

            smtpClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
            return(smtpClient.SendMailAsync(message));
        }