Пример #1
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);
        }
Пример #2
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);
            }
        }
Пример #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 field name.
        /// </summary>
        /// <param name="value">Field to check.</param>
        /// <returns>
        /// <strong>true</strong> if field name is valid; otherwise, <strong>false</strong>.
        /// </returns>
        public static bool IsValidFieldName(string value)
        {
            SentinelHelper.ArgumentNull(value);

            var val = new Regex(@"^[a-zA-Z0-9_*%@#-]+");

            return(val.IsMatch(value));
        }
        /// <summary>
        /// Determines whether <paramref name="value" /> is valid path.
        /// </summary>
        /// <param name="value">Path to check.</param>
        /// <returns>
        /// <strong>true</strong> if path is valid; otherwise, <strong>false</strong>.
        /// </returns>
        public static bool IsValidPath(string value)
        {
            SentinelHelper.ArgumentNull(value);

            var val = new Regex(@"^(.*/)?(?:$|(.+?)(?:(\.[^.]*$)|$))");

            return(val.IsMatch(value));
        }
        /// <summary>
        /// Determines whether <paramref name="value" /> is valid mail address.
        /// </summary>
        /// <param name="value">Mail address to check.</param>
        /// <returns>
        /// <strong>true</strong> if mail address is valid; otherwise, <strong>false</strong>.
        /// </returns>
        public static bool IsValidMailAddress(string value)
        {
            SentinelHelper.ArgumentNull(value);

            var val = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");

            return(val.IsMatch(value));
        }
        /// <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));
        }
        /// <summary>
        /// Determines whether <paramref name="value" /> is valid binding resource.
        /// </summary>
        /// <param name="value">String to check.</param>
        /// <returns>
        /// <strong>true</strong> if string is a valid resource; otherwise, <strong>false</strong>.
        /// </returns>
        public static bool IsBindingResource(string value)
        {
            SentinelHelper.ArgumentNull(value);

            var val = new Regex(@"^(\s)*\{(\s)*inding(\s)*:[\s|\w]*[.]*(\w)*}$", RegexOptions.IgnoreCase | RegexOptions.Singleline);

            //var val = new Regex(@"^(\s)*\{(\s)*bind(\s)*:(\s)*\w+(\s)*}$", RegexOptions.IgnoreCase | RegexOptions.Singleline);

            return(val.IsMatch(value));
        }
Пример #9
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);
        }
Пример #10
0
        /// <summary>
        /// Gets a valid full path from a relative path.
        /// </summary>
        /// <param name="targetPath">Relative path.</param>
        /// <param name="baseObject">Base path. If is <b>null</b> then use current assembly.</param>
        /// <returns>
        /// Valid full path.
        /// </returns>
        /// <exception cref="T:System.ComponentModel.InvalidEnumArgumentException">The value specified is outside the range of valid values.</exception>
        public static string GetRelativeFilePathParsed(string targetPath, object baseObject = null)
        {
            SentinelHelper.ArgumentNull(targetPath);

            var relativePathNormalized = targetPath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
            var isRelativePath         = relativePathNormalized.Trim().StartsWith("~", StringComparison.Ordinal);

            if (!isRelativePath)
            {
                return(targetPath);
            }

            if (relativePathNormalized.Length.Equals(1))
            {
                relativePathNormalized = relativePathNormalized.Insert(1, Path.DirectorySeparatorChar.ToString());
            }
            else if (!relativePathNormalized[1].Equals(Path.DirectorySeparatorChar))
            {
                relativePathNormalized = relativePathNormalized.Insert(1, Path.DirectorySeparatorChar.ToString());
            }

            Uri callingUri;

            if (baseObject == null)
            {
                callingUri = AssemblyHelper.GetFullAssemblyUri();
            }
            else
            {
                var callingPath = baseObject.GetType().Assembly.CodeBase;
                callingUri = new Uri(callingPath);
            }

            var candidateUri         = new UriBuilder(callingUri);
            var unscapedCandidateUri = Uri.UnescapeDataString(candidateUri.Path);
            var candidateRootPath    = Path.GetDirectoryName(unscapedCandidateUri);

            var outputPartialPath = string.Empty;
            var rootPattern       = $"~{Path.DirectorySeparatorChar}";

            if (!relativePathNormalized.Equals(rootPattern))
            {
                outputPartialPath = relativePathNormalized.Split(new[] { rootPattern }, StringSplitOptions.RemoveEmptyEntries)[0];
            }

            var rootPath = candidateRootPath.ToUpperInvariant()
                           .Replace("BIN", string.Empty)
                           .Replace($"{Path.DirectorySeparatorChar}DEBUG", string.Empty);

            return(Path.Combine(rootPath, outputPartialPath));
        }
Пример #11
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);
            }
        }
Пример #12
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:iTin.Export.Helper.Mail" /> class.
        /// </summary>
        /// <param name="server">Server model.</param>
        public MailHelper(MailServerModel server)
        {
            SentinelHelper.ArgumentNull(server);

            this.server = server;
        }