Example #1
0
        public void SendMail(string credential, MailMessage message, bool asAsync)
        {
            SentinelHelper.ArgumentNull(message);
            SentinelHelper.IsTrue(string.IsNullOrEmpty(credential));

            var model = server.Credentials[credential];

            if (asAsync)
            {
                var client = new SmtpClient(model.Host, model.Port)
                {
                    EnableSsl      = model.SSL == YesNo.Yes,
                    Credentials    = new NetworkCredential(model.UserName, model.Password, model.Domain),
                    DeliveryMethod = SmtpDeliveryMethod.Network
                };

                client.SendCompleted += SendCompletedCallback;
                client.SendAsync(message, message);
            }
            else
            {
                var client = new SmtpClient(model.Host, model.Port)
                {
                    EnableSsl      = true,
                    Credentials    = new NetworkCredential(model.UserName, model.Password, model.Domain),
                    DeliveryMethod = SmtpDeliveryMethod.Network
                };

                client.Send(message);
            }
        }
Example #2
0
        /// <summary>
        /// Sends mail with specified credential synchronously.
        /// </summary>
        /// <param name="credential">Credential name.</param>
        /// <param name="message">Message to send.</param>
        public void SendMail(string credential, MailMessage message)
        {
            SentinelHelper.ArgumentNull(message);
            SentinelHelper.IsTrue(string.IsNullOrEmpty(credential));

            SendMail(credential, message, false);
        }
Example #3
0
        public static MemoryStream AsMemoryStreamFromFile(string fileName)
        {
            SentinelHelper.IsTrue(string.IsNullOrEmpty(fileName));

            MemoryStream ms;
            FileStream   fs     = null;
            MemoryStream mstemp = null;

            try
            {
                fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                try
                {
                    mstemp = new MemoryStream(fs.AsByteArray());
                    ms     = mstemp;
                    mstemp = null;
                }
                finally
                {
                    if (mstemp != null)
                    {
                        mstemp.Dispose();
                    }
                }
            }
            finally
            {
                if (fs != null)
                {
                    fs.Dispose();
                }
            }

            return(ms);
        }
        /// <summary>
        /// Determines whether <paramref name="value" /> is valid ip address.
        /// </summary>
        /// <param name="value">Ip address to check.</param>
        /// <returns>
        /// <strong>true</strong> if ip address is valid; otherwise, <strong>false</strong>.
        /// </returns>
        public static bool IsValidIpAddress(string value)
        {
            SentinelHelper.ArgumentNull(value);
            SentinelHelper.IsTrue(value.Length > 15);

            var val = new Regex(@"^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$");

            return(val.IsMatch(value));
        }
Example #5
0
        /// <summary>
        /// Returns the specified file as a byte array.
        /// </summary>
        /// <param name="fileName">File to convert.</param>
        /// <returns>
        /// Array of byte than represent the file.
        /// </returns>
        public static byte[] AsByteArrayFromFile(string fileName)
        {
            SentinelHelper.IsTrue(string.IsNullOrEmpty(fileName));

            byte[] buffer;

            using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                buffer = stream.AsByteArray();
            }

            return(buffer);
        }
Example #6
0
        /// <summary>
        /// Copies the files.
        /// </summary>
        /// <param name="sourceDirectory">Source directory.</param>
        /// <param name="targetDirectory">Target directory.</param>
        /// <param name="criterial">File criteria.</param>
        /// <param name="overrides">if is <strong>true</strong> overrides destination file.</param>
        public static void CopyFiles(string sourceDirectory, string targetDirectory, string criterial, bool overrides)
        {
            SentinelHelper.IsTrue(string.IsNullOrEmpty(sourceDirectory));
            SentinelHelper.IsTrue(string.IsNullOrEmpty(targetDirectory));

            var items = Directory.GetFiles(sourceDirectory, criterial, SearchOption.TopDirectoryOnly);

            foreach (var item in items)
            {
                var filename = Path.GetFileName(item);
                var target   = Path.Combine(targetDirectory, filename);

                File.Copy(item, target, overrides);
            }
        }