Example #1
0
        /// <summary>Deletes the settings file.</summary>
        public virtual void Delete(string filename = null, SettingsOnFailure onFailure = SettingsOnFailure.Throw)
        {
            // Save and delete must not be interrupted or superseded by a SaveThreaded
            lock (_lock)
            {
                if (_saveThread != null) // this can only ever occur in the Sleep/lock wait phase of the quick save thread
                {
                    _saveThread.Abort();
                    _saveThread = null;
                }
                var attr = SettingsUtil.GetAttribute(GetType());
                if (filename == null)
                {
                    filename = attr.GetFileName();
                }

                if (onFailure == SettingsOnFailure.Throw)
                {
                    File.Delete(filename);
                }
                else if (onFailure == SettingsOnFailure.DoNothing)
                {
                    try { File.Delete(filename); }
                    catch { }
                }
                else
                {
                    while (true)
                    {
                        try
                        {
                            File.Delete(filename);
                            break;
                        }
                        catch (Exception e)
                        {
                            var choices = new List <string>()
                            {
                                "Try &again", "&Don't delete settings"
                            };
                            if (onFailure == SettingsOnFailure.ShowRetryWithCancel)
                            {
                                choices.Add("&Cancel");
                            }
                            int choice = DlgMessage.ShowWarning("Program settings could not be deleted.\n({0})\n\nWould you like to try again?".Fmt(e.Message), choices.ToArray());
                            if (choice == 1)
                            {
                                return;
                            }
                            if (choice == 2)
                            {
                                throw new SettingsCancelException();
                            }
                        }
                    }
                    ;
                }
            }
        }
Example #2
0
        /// <summary>
        ///     Sends an email using one of the pre-configured RT.Emailer SMTP accounts. If none are configured on this
        ///     computer, an exception will be thrown, describing what the user needs to do - though this requires a pretty
        ///     technical user.</summary>
        /// <param name="to">
        ///     The recipients of the email.</param>
        /// <param name="subject">
        ///     Subject line.</param>
        /// <param name="bodyPlain">
        ///     Body of the message in plaintext format, or null to omit this MIME type.</param>
        /// <param name="bodyHtml">
        ///     Body of the message in HTML format, or null to omit this MIME type.</param>
        /// <param name="account">
        ///     The name of one of the RT.Emailer accounts to use (case-sensitive). If null or not defined, will fall back to
        ///     exe name, then the Default Account setting, and then any defined account, in this order.</param>
        /// <param name="fromName">
        ///     The text to use as the "from" name. If null, will use the executable name. This setting has no effect if the
        ///     specified RT.Emailer account specifies a FromName of its own.</param>
        public static void SendEmail(IEnumerable <MailAddress> to, string subject, string bodyPlain = null, string bodyHtml = null, string account = null, string fromName = null)
        {
            // Load the settings file
            var      settingsFile = SettingsUtil.GetAttribute <settings>().GetFileName();
            settings settings;

            SettingsUtil.LoadSettings(out settings);

            // Add an empty exe name account
            string exeName = Assembly.GetEntryAssembly() == null ? null : Path.GetFileName(Assembly.GetEntryAssembly().Location);

            if (exeName != null && !settings.Accounts.ContainsKey(exeName))
            {
                settings.Accounts.Add(exeName, null);
            }

            // Save any changes we've made (to a separate file!) and report error if we have no accounts at all
            if (!settings.Accounts.Any(a => a.Value != null))
            {
                settings.Accounts.Add("example", new account());
                settings.SaveQuiet(PathUtil.AppendBeforeExtension(settingsFile, ".example"));
                throw new InvalidOperationException("There are no RT.Emailer accounts defined on this computer. Please configure them in the file \"{0}\".".Fmt(settingsFile));
            }
            else
            {
                settings.SaveQuiet(PathUtil.AppendBeforeExtension(settingsFile, ".rewrite"));
            }

            // Pick the actual account we'll use
            if (account == null || !settings.Accounts.ContainsKey(account) || settings.Accounts[account] == null)
            {
                account = exeName;
            }
            if (account == null || !settings.Accounts.ContainsKey(account) || settings.Accounts[account] == null)
            {
                account = settings.DefaultAccount;
            }
            if (account == null || !settings.Accounts.ContainsKey(account) || settings.Accounts[account] == null)
            {
                account = settings.Accounts.First(a => a.Value != null).Key;
            }
            var acc = settings.Accounts[account];

            // Send the email
            using (var smtp = new RTSmtpClient(acc, Log))
            {
                var from = new MailAddress(acc.FromAddress, acc.FromName ?? fromName ?? (exeName == null ? null : Path.GetFileNameWithoutExtension(exeName)));
                smtp.SendEmail(from, to, subject, bodyPlain, bodyHtml);
            }
        }