/// <summary> /// Parses the email addresses and creates in-memory work items. /// </summary> /// <remarks> /// During parsing, duplicates and invalid addresses are discarded. /// The work items has not been saved. /// </remarks> /// <param name="emailAddresses">The email addresses as a separated string to parse. Separators are ";, \n"</param> /// <returns>The email addresses as JobWorkItem objects</returns> public static JobWorkItems ParseEmailAddressesToWorkItems(string emailAddresses, JobWorkStatus defaultStatus) { JobWorkItems items = new JobWorkItems(); string[] emailAddressArray = emailAddresses.Split(new char[] { ';', ',', ' ', '\n' }, StringSplitOptions.RemoveEmptyEntries); foreach (string emailAddress in emailAddressArray) { // Clean address string emailAddressCleaned = NewsLetterUtil.CleanEmailAddress(emailAddress); // Validate email address if (EmailSyntaxValidator.Validate(emailAddressCleaned) == true) { // Check if already added. JobWorkItem workItem = items.FirstOrDefault(j => j.EmailAddress.Equals(emailAddressCleaned)); if (workItem == null) { // Handle duplicates - try to load it first workItem = new JobWorkItem(emailAddressCleaned, defaultStatus); // Add to collection items.Add(workItem); } } } return(items); }
/// <summary> /// Imports email addresses as work items for this job /// </summary> /// <param name="emailArray">The email addresses to import.</param> /// <param name="invalidEmailAddresses">A list of all available email addresses that could not be parsed as a valid address</param> /// <param name="duplicateAddresses">A list of duplicate email addresses added</param> /// <returns>The number of email addresses imported as new work items. Duplicates are not part of this number.</returns> public int ImportEmailAddresses(string[] emailArray, out List <string> invalidEmailAddresses, out List <string> duplicateAddresses) { JobWorkItems importedItems = new JobWorkItems(); invalidEmailAddresses = new List <string>(); duplicateAddresses = new List <string>(); int numberOfNewItems = 0; foreach (string emailAddress in emailArray) { // TODO: This can be optimized by checking the // existence of these email addresses in batches // Clean address (this is done on save, so we need to make sure it's correct) string emailAddressCleaned = NewsLetterUtil.CleanEmailAddress(emailAddress); // Validate email address if (EmailSyntaxValidator.Validate(emailAddressCleaned) == false) { // Invalid email address, skip it. invalidEmailAddresses.Add(emailAddressCleaned); } else { // Check if already imported. This is the quickest duplicate check JobWorkItem workItem = importedItems.Items.FirstOrDefault(j => j.EmailAddress.Equals(emailAddressCleaned)); if (workItem == null) { // Handle duplicates - try to load it first workItem = JobWorkItem.Load(Id, emailAddressCleaned); if (workItem == null) { // Create it, and save it. It is automatically // added to the WorkItems collection workItem = this.CreateWorkItem(emailAddressCleaned); workItem.Save(); numberOfNewItems++; } else { // Duplicate duplicateAddresses.Add(emailAddressCleaned); } // Add to imported collection, for quick // in memory duplicate check importedItems.Items.Add(workItem); } } } // Counters are no longer valid ResetStatusCounters(); return(numberOfNewItems); }
/// <summary> /// Determines if an email has valid syntax /// </summary> /// <param name="email">the email to test</param> /// <param name="TLDrequired">indicates whether or not the /// email must end with a known TLD to be considered valid</param> /// <returns>boolean indicating if the email has valid syntax</returns> /// <remarks> /// Validates an email address specifying whether or not /// the email is required to have a TLD that is valid. /// </remarks> public static bool Validate(string email, bool TLDrequired) { EmailSyntaxValidator v; bool valid; //call syntax validator v = new EmailSyntaxValidator(email, TLDrequired); //determine validity valid = v.IsValid; //cleanup v = null; //return indication of validity return(valid); }
/// <summary> /// Removes emailaddresses from the recipient list. /// Validates the input email address to prevent sql statements in the input. /// </summary> /// <param name="emailAddressArray">Array of emailadrresses</param> /// <returns></returns> private int RemoveEmailAddresses(string[] emailAddressArray) { int numberOfDeletedItems = 0; RecipientData recipientUtil = GetWorker(); foreach (string emailAddress in emailAddressArray) { string emailAddressCleaned = NewsLetterUtil.CleanEmailAddress(emailAddress); if (EmailSyntaxValidator.Validate(emailAddressCleaned)) { recipientUtil.RecipientListRemoveItem(_id, emailAddressCleaned); numberOfDeletedItems++; } } ClearEmailAddressCount(); return(numberOfDeletedItems); }
protected SubscriptionResult AddSubscriptionToList(string email, RecipientList selectedList) { EmailSyntaxValidator validator = new EmailSyntaxValidator(email, false); if (validator.IsValid) { _log.Debug("Attemt to add email subscription for {0}", email); EmailAddress emailAddress = selectedList.CreateEmailAddress(email); emailAddress.Source = EmailAddressSource.SelfRegistered; emailAddress.Added = DateTime.Now; emailAddress.Save(); // Will add it to the list, or update it if it exists return SubscriptionResult.Success; } else { _log.Warning("Failed to add email subscription for '{0}' (not valid)", email); return SubscriptionResult.EmailNotValid; } }
/// <summary> /// Determines if an email has valid syntax /// </summary> /// <param name="email">the email to test</param> /// <param name="TLDrequired">indicates whether or not the /// email must end with a known TLD to be considered valid</param> /// <returns>boolean indicating if the email has valid syntax</returns> /// <remarks> /// Validates an email address specifying whether or not /// the email is required to have a TLD that is valid. /// </remarks> public static bool Validate(string email, bool TLDrequired) { EmailSyntaxValidator v; bool valid; //call syntax validator v = new EmailSyntaxValidator(email, TLDrequired); //determine validity valid = v.IsValid; //cleanup v = null; //return indication of validity return valid; }