コード例 #1
0
        static bool SendViaSmtp(IEmailQueueItem mailItem, MailMessage mail)
        {
            // Developer note: Web.config setting for SSL is designed to take priority over the specific setting of the email.
            // If in your application you want the email item's setting to take priority, do this:
            //      1. Remove the 'Email.Enable.Ssl' setting from web.config totally.
            //      2. If you need a default value, use  your application's Global Settings object and use that value everywhere you create an EmailQueueItem.
            using (var smtpClient = new SmtpClient {
                EnableSsl = Config.Get <bool>("Email.Enable.Ssl", mailItem.EnableSsl)
            })
            {
                smtpClient.Configure();

                if (mailItem.SmtpHost.HasValue())
                {
                    smtpClient.Host = mailItem.SmtpHost;
                }

                if (mailItem.SmtpPort.HasValue)
                {
                    smtpClient.Port = mailItem.SmtpPort.Value;
                }

                if (mailItem.Username.HasValue())
                {
                    smtpClient.Credentials = new NetworkCredential(mailItem.Username, mailItem.Password.Or((smtpClient.Credentials as NetworkCredential).Get(c => c.Password)));
                }

                if (Config.IsDefined("Email.Random.Usernames"))
                {
                    var userName = Config.Get("Email.Random.Usernames").Split(',').Trim().PickRandom();
                    smtpClient.Credentials = new NetworkCredential(userName, Config.Get("Email.Password"));
                }

                OnSending(mailItem, mail);

                smtpClient.Send(mail);

                OnSent(mailItem, mail);
            }

            return(true);
        }
コード例 #2
0
        /// <summary>
        ///     Applies connection settings from a connection string using a <see cref="SmtpConnectionStringBuilder"></see>
        /// </summary>
        public static void Configure(this SmtpClient smtpClient, string connectionString)
        {
            var connectionStringBuilder = new SmtpConnectionStringBuilder(connectionString);

            smtpClient.Configure(connectionStringBuilder);
        }