Ejemplo n.º 1
0
        /// <summary>
        /// Sends a RFC conform email using default .NET classes.
        /// </summary>
        /// <param name="fromAddress">Mail-address of the sender.</param>
        /// <param name="toAddresses">Mail-addresses of the receivers.</param>
        /// <param name="subject">Subject of the mail.</param>
        /// <param name="body">The string for the body of the mail.</param>
        /// <param name="settings">A structure containing the settings for the server.</param>
        /// <returns><c>True</c> if the mail was sent, otherwise <c>false</c>.</returns>
        public static bool SendMail(string fromAddress, string[] toAddresses, string subject, string body, MailServerSettings settings)
        {
            CheckUtil.ThrowIfNullOrWhitespace(() => subject);
            CheckUtil.ThrowIfNullOrWhitespace(() => body);
            var result = false;

            if (!fromAddress.IsValidEmailAddress())
            {
                throw new ArgumentException("Invalid e-mail-address of sender.", nameof(fromAddress));
            }
            if (!toAddresses.Any())
            {
                throw new ArgumentException("No receipient submitted!", nameof(toAddresses));
            }
            if (toAddresses.Any(a => !a.IsValidEmailAddress()))
            {
                throw new ArgumentException("Invalid e-mail-address of on of the recipient.", nameof(toAddresses));
            }
            // Send the mail and use credentials if given.
            using (var message = new MailMessage(fromAddress, toAddresses.First(), subject, body))
            {
                if (toAddresses.Count() > 1)
                {
                    // add the other receipients
                    for (var i = 1; i < toAddresses.Count(); i++)
                    {
                        message.To.Add(toAddresses[i]);
                    }
                }
                result = SendMail(message, settings);
            }
            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Retrieves the provider name of a connection string identified by the <paramref name="key"/>.
        /// </summary>
        /// <remarks>
        /// This method will throw exceptions on any failure.
        /// </remarks>
        /// <param name="key">The unique key out of the connection string.</param>
        /// <returns>The provider name for the <paramref name="key"/>.</returns>
        public static string GetProviderName(string key)
        {
            CheckUtil.ThrowIfNullOrWhitespace(() => key);
            var connectionString = ConnectionSettings[key];

            if (connectionString == null)
            {
                var error = string.Format(CultureInfo.InvariantCulture, "Cannot find connection-string '{0}' in config-file.", key);
                throw new InvalidOperationException(error);
            }
            return(connectionString.ProviderName);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Searches for an app setting with the provided <paramref name="key"/> from the calling configuration and tries to return
        /// its value converted to <typeparamref name="T"/>.
        /// </summary>
        /// <remarks>
        /// This method will thor exception
        /// </remarks>
        /// <param name="key">The unique key out of the app-settings.</param>
        /// <param name="defaultValue"></param>
        /// <typeparam name="T">Target type which has to be an <see cref="IConvertible"/>.</typeparam>
        /// <returns>The value of type <typeparamref name="T"/> or <paramref name="defaultValue"/> if no value could be obtained.</returns>
        public static T TryGet <T>(string key, T defaultValue) where T : IConvertible
        {
            CheckUtil.ThrowIfNullOrWhitespace(() => key);
            var value = AppSettings[key];

            if (value == null)
            {
                // there was no key inside the settings, so retrieve the default
                return(defaultValue);
            }
            try
            {
                var result = (T)Convert.ChangeType(value, typeof(T), CultureInfo.CurrentCulture);
                return(result);
            }
            catch
            {
                return(defaultValue);
            }
        }