Example #1
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);
            }
        }
Example #2
0
        public void TestEncodeHeader()
        {
            test(
                RTSmtpClient.EncodeHeader("Subject", "Test subject"),
                @"Subject: Test subject");
            test(
                RTSmtpClient.EncodeHeader("Subject", "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor."),
                @"Subject: Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean
 commodo ligula eget dolor. Aenean massa. Lorem ipsum dolor sit amet,
 consectetuer adipiscing elit. Aenean commodo ligula eget dolor.");
            test(
                RTSmtpClient.EncodeHeader("Subject", "Далеко-далеко за словесными горами в стране гласных и согласных живут рыбные тексты. Вдали от всех живут они в буквенных домах на берегу Семантика большого языкового океана."),
                @"Subject: =?UTF-8?B?0JTQsNC70LXQutC+LdC00LDQu9C10LrQviDQt9CwINGB0LvQvtCy?=
 =?UTF-8?B?0LXRgdC90YvQvNC4INCz0L7RgNCw0LzQuCDQsiDRgdGC0YDQsNC90LUg0LM=?=
 =?UTF-8?B?0LvQsNGB0L3Ri9GFINC4INGB0L7Qs9C70LDRgdC90YvRhSDQttC40LLRg9GC?=
 =?UTF-8?B?INGA0YvQsdC90YvQtSDRgtC10LrRgdGC0YsuINCS0LTQsNC70Lgg0L7RgiA=?=
 =?UTF-8?B?0LLRgdC10YUg0LbQuNCy0YPRgiDQvtC90Lgg0LIg0LHRg9C60LLQtdC90L0=?=
 =?UTF-8?B?0YvRhSDQtNC+0LzQsNGFINC90LAg0LHQtdGA0LXQs9GDINCh0LXQvNCw0L0=?=
 =?UTF-8?B?0YLQuNC60LAg0LHQvtC70YzRiNC+0LPQviDRj9C30YvQutC+0LLQvtCz0L4g?=
 =?UTF-8?B?0L7QutC10LDQvdCwLg==?=");
            test(
                RTSmtpClient.EncodeHeader("Subject", "sjfwoijfdalwfdwiufdhawldkjhkawuydgakwjdfygakwjdfybawkdufyghawkjdfgawkfuygawkjdfygawkjdybfawkuydgawkudyg awdfgkawuydgawdfawdfygliuhawdlufhawlydfglawyeghdliuhewgdliuhadgifaliugawdfliyguawdlfygawdflygawdlfguy sd"),
                @"Subject:
 sjfwoijfdalwfdwiufdhawldkjhkawuydgakwjdfygakwjdfybawkdufyghawkjdfgawkfuygawkjdfygawkjdybfawkuydgawkudyg
 awdfgkawuydgawdfawdfygliuhawdlufhawlydfglawyeghdliuhewgdliuhadgifaliugawdfliyguawdlfygawdflygawdlfguy
 sd");

            test(
                RTSmtpClient.EncodeHeader("From", new MailAddress("*****@*****.**")),
                "From: [email protected]");
            test(
                RTSmtpClient.EncodeHeader("From", new MailAddress("*****@*****.**"), new MailAddress("*****@*****.**"), new MailAddress("*****@*****.**"), new MailAddress("*****@*****.**"), new MailAddress("*****@*****.**"), new MailAddress("*****@*****.**")),
                @"From: [email protected], [email protected], [email protected],
 [email protected], [email protected], [email protected]");
            test(
                RTSmtpClient.EncodeHeader("From", new MailAddress("Test <*****@*****.**>"), new MailAddress("\"Τεστ Σμιθ\" <*****@*****.**>"), new MailAddress("*****@*****.**"), new MailAddress("*****@*****.**"), new MailAddress("\"Грегор Замза обнаружил что он у себя в постели\" <*****@*****.**>"), new MailAddress("*****@*****.**")),
                @"From: ""Test"" <*****@*****.**>, ""=?UTF-8?B?zqTOtc+Dz4QgzqPOvM65zrg=?=""
 <*****@*****.**>, [email protected], [email protected], ""=?UTF-8?B?0JM=?=
 =?UTF-8?B?0YDQtdCz0L7RgCDQl9Cw0LzQt9CwINC+0LHQvdCw0YDRg9C20LjQuyDRh9GC?=
 =?UTF-8?B?0L4g0L7QvSDRgyDRgdC10LHRjyDQsiDQv9C+0YHRgtC10LvQuA==?=""
 <*****@*****.**>, [email protected]");
        }
Example #3
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);
            }
        }