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); } }
/// <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); }
/// <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)); }
/// <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)); }
/// <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; }