public SubscriptionResult UnsubscribeUsingBlocklist(string email, int listId) { if (string.IsNullOrEmpty(email)) { return(SubscriptionResult.EmailNotValid); } RecipientList selectedList = RecipientList.Load(listId); if (selectedList.ListType != RecipientListType.BlockList) { throw new ApplicationException("Specified list is not a block list"); } EmailAddress emailAddress = selectedList.CreateEmailAddress(email); emailAddress.Comment = "Unsubscribed using opt-out page."; emailAddress.Source = EmailAddressSource.SelfRegistered; emailAddress.Save(); // if already there, fine, no problem // if not there, we've added the email return(SubscriptionResult.Success); }
private void cmdSubscribe_Click(object sender, System.EventArgs e) { string emailAddress = txtEmail.Text; if (emailAddress == null || emailAddress == string.Empty) { AddErrorMessage(Translate("/bvnetwork/sendmail/subscribe/errornoemail")); return; } emailAddress = emailAddress.Trim(); EPiMailEngine engine = new EPiMailEngine(); ArrayList resultArray = new ArrayList(); EmailAddresses importedItems = new EmailAddresses(); foreach (ListItem itm in this.chkNewsLetterLists.Items) { if (itm.Selected) { // Check that the user does not belong to the groups // already. RecipientList list = RecipientList.Load(Convert.ToInt32(itm.Value)); SubscriptionStatus status = new SubscriptionStatus(); status.RecipientListName = list.Name; //Load and check if the email typed by the user exists. EmailAddress emailItem = EmailAddress.Load(list.Id, emailAddress); if (emailItem == null) { // Create it, and save it. It is automatically // added to the WorkItems collection emailItem = list.CreateEmailAddress(emailAddress); // Save emailItem.Save(); status.SubscriptionResult = true; } else { // Already subscribes status.SubscriptionResult = true; status.Message = Translate("/bvnetwork/sendmail/subscribe/alreadysubscribe"); } resultArray.Add(status); } } // Done adding, now show the result rptResult.DataSource = resultArray; rptResult.DataBind(); }
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); } }
public List <string> ValidateRecipientList(List <EmailAddress> recipientAddresses, RecipientList blocked) { _log.Debug("Validating {0} emails using block list {1} ({2})", recipientAddresses.Count, blocked.Name, blocked.Id); if (recipientAddresses.Count == 0) { return(new List <string>()); } MailgunSettings settings = GetSettings(); RestClient client = new RestClient(); client.BaseUrl = new Uri("https://api.mailgun.net/v2"); client.Authenticator = new HttpBasicAuthenticator("api", settings.PublicKey); if (string.IsNullOrEmpty(settings.ProxyAddress) == false) { client.Proxy = new WebProxy(settings.ProxyAddress, settings.ProxyPort); // Makes it easy to debug as Fiddler will show us the requests } RestRequest request = new RestRequest(); // We're sending a lot of data, which should // be a post, but Mailgun does not allow that // request.Method = Method.POST; request.Resource = "/address/parse"; // Validate strict request.AddParameter("syntax_only", false); string addresses = ""; foreach (EmailAddress emailAddress in recipientAddresses) { addresses += emailAddress.Email + ","; } _log.Debug("Length of address field sent to Mailgun: {0}", addresses.Length); if (addresses.Length > 8000) { throw new ApplicationException("Mailgun only accepts address fields with length of 8000 characters."); } request.AddParameter("addresses", addresses.TrimEnd(',')); var response = client.Execute(request); _log.Debug("Mailgun responded with status: {0} - {1}", (int)response.StatusCode, response.StatusDescription); if (response.StatusCode == HttpStatusCode.OK) { /* * { * "parsed": [ * "Alice <*****@*****.**>", * "*****@*****.**" * ], * "unparseable": [ * "example.com" * ] * } */ Dictionary <string, List <string> > resultParams = null; try { resultParams = new JsonDeserializer().Deserialize <Dictionary <string, List <string> > >(response); } catch (Exception e) { _log.Warning("Unable to parse Mailgun response.", e); } // Update all recipients with information if (resultParams != null) { List <string> invalidAddresses = resultParams["unparseable"]; foreach (string address in invalidAddresses) { EmailAddress emailAddress = recipientAddresses.Find(a => a.Email.Equals(address, StringComparison.InvariantCultureIgnoreCase)); if (emailAddress != null) { emailAddress.Comment = "Mailgun reported address as invalid."; emailAddress.Save(); } EmailAddress blockedAddress = blocked.CreateEmailAddress(address); blockedAddress.Comment = "Mailgun reported address as invalid."; blockedAddress.Save(); } return(invalidAddresses); } } else { // Attempt to log error from Mailgun if (string.IsNullOrEmpty(response.ErrorMessage) == false) { _log.Warning(response.ErrorMessage); } if (string.IsNullOrEmpty(response.Content) == false) { _log.Debug(response.Content); } throw new ApplicationException("Cannot validate email addresses using Mailgun: " + response.ErrorMessage); } return(null); }