private void materialRaisedButtonSendLetter_Click(object sender, EventArgs e) { List <string> listOfEmails = new List <string>(); for (int i = 0; i < dataGridView1.Rows.Count; i++) { LetterSubscriber subscirber = new LetterSubscriber(Convert.ToString(dataGridView1.Rows[i].Cells[1].Value)); listOfEmails.Add(subscirber.email); } try { MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); mail.From = new MailAddress("*****@*****.**"); foreach (var email in listOfEmails) { mail.To.Add(email); mail.Subject = materialSingleLineTextFieldSubject.Text; mail.Body = richTextBoxBody.Text; SmtpServer.Port = 587; SmtpServer.Credentials = new System.Net.NetworkCredential("*****@*****.**", "Asd12345qwe"); SmtpServer.EnableSsl = true; SmtpServer.Send(mail); } } catch (Exception ex) { MessageBox.Show("Probléma történt az email küldése során!"); } }
private string ReplaceHtmlTokens(string htmlBody, LetterSubscriber subscriber) { // mail merge string mergedMessage; if (unsubscribeUrl.Length > 0) { if (subscriber.UserGuid == Guid.Empty) { mergedMessage = htmlBody.Replace( Letter.UnsubscribeToken, "<a href='" + unsubscribeUrl + "?s=" + subscriber.SubscribeGuid.ToString() + "'>" + unsubscribeLinkText + "</a>").Replace(Letter.UserNameToken, subscriber.Name.Coalesce(subscriber.EmailAddress)); } else { mergedMessage = htmlBody.Replace( Letter.UnsubscribeToken, "<a href='" + unsubscribeUrl + "?u=" + subscriber.UserGuid.ToString() + "&l=" + subscriber.LetterInfoGuid.ToString() + "'>" + unsubscribeLinkText + "</a>").Replace(Letter.UserNameToken, subscriber.Name.Coalesce(subscriber.EmailAddress)); } return(mergedMessage); } return(htmlBody); }
private void LoadSettings() { subscriptionGuid = WebUtils.ParseGuidFromQueryString("s", subscriptionGuid); if (subscriptionGuid != Guid.Empty) { subscription = subscriptions.Fetch(subscriptionGuid); if ((subscription != null) && (subscription.SiteGuid == siteSettings.SiteGuid)) { subscriptions.Verify(subscription.SubscribeGuid, true, Guid.Empty); if (subscription.UserGuid == Guid.Empty) { SiteUser user = SiteUser.GetByEmail(siteSettings, subscription.EmailAddress); if (user != null) { subscription.UserGuid = user.UserGuid; subscriptions.Save(subscription); } } LetterInfo.UpdateSubscriberCount(subscription.LetterInfoGuid); } else { subscription = null; } } }
private void DoUnsubscribe() { if (subscriptionGuid != Guid.Empty) { LetterSubscriber s = subscriptions.Fetch(subscriptionGuid); if (s == null) { ShowNotFoundMessge(); return; } subscriptions.Delete(s); LetterInfo.UpdateSubscriberCount(s.LetterInfoGuid); lblUnsubscribe.Text = Resource.NewsletterUnsubscribeSuccess; btnUnsubscribeConfirm.Visible = false; lblUnsubscribe.Visible = true; return; } LetterInfo letterInfo = new LetterInfo(letterInfoGuid); if (letterInfo.LetterInfoGuid == Guid.Empty) { ShowNotFoundMessge(); return; } List <LetterSubscriber> userSubscriptions = subscriptions.GetListByUser(siteSettings.SiteGuid, userGuid); bool unsubscribed = false; foreach (LetterSubscriber s in userSubscriptions) { if (s.LetterInfoGuid == letterInfoGuid) { subscriptions.Delete(s); unsubscribed = true; LetterInfo.UpdateSubscriberCount(s.LetterInfoGuid); } } if (unsubscribed) { lblUnsubscribe.Text = string.Format(CultureInfo.InvariantCulture, Resource.NewsletterUnsubscribeSuccessFormatString, letterInfo.Title); btnUnsubscribeConfirm.Visible = false; lblUnsubscribe.Visible = true; } else { ShowNotFoundMessge(); } }
private void RunTask() { // this is where the work gets done // Get a data table of up to 1000 users // who have not opted in but also have not previously // opted out and have valid email accounts and account is not locked DataTable dataTable = GetData(); double completeRatio = .5; if (dataTable.Rows.Count == 0) { completeRatio = 1; //complete ReportStatus(completeRatio); return; } SubscriberRepository repository = new SubscriberRepository(); int count = 0; foreach (DataRow row in dataTable.Rows) { Guid userGuid = new Guid(row["UserGuid"].ToString()); string email = row["email"].ToString(); LetterSubscriber s = new LetterSubscriber(); s.SiteGuid = SiteGuid; s.UserGuid = userGuid; s.EmailAddress = email; s.LetterInfoGuid = LetterInfoGuid; s.UseHtml = true; s.IsVerified = true; repository.Save(s); count += 1; if (count > 20) { ReportStatus(completeRatio); count = 0; } } LetterInfo.UpdateSubscriberCount(LetterInfoGuid); ReportStatus(completeRatio); // repeat until the table comes back with 0 rows RunTask(); }
private void DoSubscribe(LetterInfo letter, SiteUser siteUser) { LetterSubscriber s = new LetterSubscriber(); s.SiteGuid = siteSettings.SiteGuid; s.EmailAddress = siteUser.Email; s.UserGuid = siteUser.UserGuid; s.LetterInfoGuid = letter.LetterInfoGuid; s.UseHtml = rbHtmlFormat.Checked; s.IsVerified = (!siteSettings.UseSecureRegistration); s.IpAddress = SiteUtils.GetIP4Address(); subscriptions.Save(s); LetterInfo.UpdateSubscriberCount(s.LetterInfoGuid); }
private string ReplaceTextTokens(string textBody, LetterSubscriber subscriber) { // mail merge if (unsubscribeUrl.Length > 0) { string mergedMessage = textBody.Replace( Letter.UnsubscribeToken, unsubscribeUrl + "?u=" + subscriber.UserGuid.ToString() + "&l=" + subscriber.LetterInfoGuid.ToString()).Replace(Letter.UserNameToken, subscriber.Name); return(mergedMessage); } return(textBody); }
/// <summary> /// people can subscribe to the newsletters without registering on the site. This method is used to attach those existing subscriptions to the user upon registration /// </summary> /// <param name="siteUser"></param> public static void ClaimExistingSubscriptions(SiteUser siteUser) { SubscriberRepository subscriptions = new SubscriberRepository(); List <LetterSubscriber> userSubscriptions = subscriptions.GetListByEmail(siteUser.SiteGuid, siteUser.Email); foreach (LetterSubscriber s in userSubscriptions) { if (s.UserGuid != siteUser.UserGuid) { s.UserGuid = siteUser.UserGuid; subscriptions.Save(s); } if (!s.IsVerified) { subscriptions.Verify(s.SubscribeGuid, true, Guid.Empty); LetterInfo.UpdateSubscriberCount(s.LetterInfoGuid); } } RemoveDuplicates(userSubscriptions); if (userSubscriptions.Count == 0) { string ipAddress = SiteUtils.GetIP4Address(); //user has no previous subscriptions and just registered // lets give him the site subscriptions that are configured for opting in new users by default List <LetterInfo> allNewsletters = LetterInfo.GetAll(siteUser.SiteGuid); foreach (LetterInfo l in allNewsletters) { if ((l.ProfileOptIn) && (l.AvailableToRoles.Contains("All Users;"))) { LetterSubscriber s = new LetterSubscriber(); s.SiteGuid = siteUser.SiteGuid; s.LetterInfoGuid = l.LetterInfoGuid; s.UserGuid = siteUser.UserGuid; s.EmailAddress = siteUser.Email; s.IsVerified = true; s.UseHtml = true; s.IpAddress = ipAddress; subscriptions.Save(s); } } } }
void grdSubscribers_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e) { string strG = e.CommandArgument.ToString(); switch (e.CommandName) { case "DeleteSubscriber": if (strG.Length == 36) { Guid subscriptionGuid = new Guid(strG); LetterSubscriber s = subscriptions.Fetch(subscriptionGuid); if (s != null) { subscriptions.Delete(s); } LetterInfo.UpdateSubscriberCount(s.LetterInfoGuid); WebUtils.SetupRedirect(this, Request.RawUrl); } break; case "SendVerification": if (strG.Length == 36) { Guid subscriptionGuid = new Guid(strG); LetterSubscriber s = subscriptions.Fetch(subscriptionGuid); LetterInfo letterInfo = new LetterInfo(letterInfoGuid); NewsletterHelper.SendSubscriberVerificationEmail( SiteRoot, s.EmailAddress, s.SubscribeGuid, letterInfo, siteSettings); } WebUtils.SetupRedirect(this, Request.RawUrl); break; } }
void grdSubscribers_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e) { switch (e.CommandName) { case "DeleteSubscriber": string strG = e.CommandArgument.ToString(); if (strG.Length == 36) { Guid subscriptionGuid = new Guid(strG); LetterSubscriber s = subscriptions.Fetch(subscriptionGuid); if (s != null) { subscriptions.Delete(s); } LetterInfo.UpdateSubscriberCount(s.LetterInfoGuid); WebUtils.SetupRedirect(this, Request.RawUrl); } break; } }
private void SendLetter( SmtpSettings smtpSettings, LetterInfo letterInfo, Letter letter, LetterSubscriber subscriber) { // TODO: use multi part email with both html and plain text body // instead of this either or approach? LetterSendLog mailLog = new LetterSendLog(); mailLog.EmailAddress = subscriber.EmailAddress; mailLog.LetterGuid = letter.LetterGuid; mailLog.UserGuid = subscriber.UserGuid; mailLog.SubscribeGuid = subscriber.SubscribeGuid; if (testMode) { Thread.Sleep(testSleepMilliseconds); // sleep 1 seconds per message to simulate } else { try { if (subscriber.UseHtml) { Email.SendEmail( smtpSettings, letterInfo.FromAddress.Coalesce(notificationFromEmail), letterInfo.ReplyToAddress.Coalesce(notificationFromEmail), subscriber.EmailAddress, string.Empty, string.Empty, letter.Subject, ReplaceHtmlTokens(letter.HtmlBody, subscriber), subscriber.UseHtml, "Normal"); } else { Email.SendEmail( smtpSettings, letterInfo.FromAddress.Coalesce(notificationFromEmail), letterInfo.ReplyToAddress.Coalesce(notificationFromEmail), subscriber.EmailAddress, string.Empty, string.Empty, letter.Subject, ReplaceTextTokens(letter.TextBody, subscriber), subscriber.UseHtml, "Normal"); } } catch (Exception ex) { // TODO: catch more specific exception(s) figure out what ones can be thrown here mailLog.ErrorOccurred = true; mailLog.ErrorMessage = ex.ToString(); log.Error(ex); } } mailLog.Save(); }
void btnSavePreferences_Click(object sender, EventArgs e) { foreach (LetterInfo availableSubscription in siteAvailableSubscriptions) { string controlID = "chk" + availableSubscription.LetterInfoGuid.ToString(); if (Request.Params.Get(controlID) != null) { if (!IsSubscribed(availableSubscription.LetterInfoGuid)) { //subscribe LetterSubscriber subscriber = new LetterSubscriber(); subscriber.SiteGuid = siteSettings.SiteGuid; subscriber.LetterInfoGuid = availableSubscription.LetterInfoGuid; subscriber.UserGuid = siteUser.UserGuid; subscriber.EmailAddress = siteUser.Email.ToLower(); subscriber.IsVerified = true; subscriber.UseHtml = rbHtmlFormat.Checked; subscriber.IpAddress = SiteUtils.GetIP4Address(); subscriptions.Save(subscriber); LetterInfo.UpdateSubscriberCount(availableSubscription.LetterInfoGuid); } else { // user is subscribed already foreach (LetterSubscriber s in userSubscriptions) { if (s.LetterInfoGuid == availableSubscription.LetterInfoGuid) { if (s.UseHtml != rbHtmlFormat.Checked) { s.UseHtml = rbHtmlFormat.Checked; subscriptions.Save(s); } if (!s.IsVerified) { subscriptions.Verify(s.SubscribeGuid, true, Guid.Empty); LetterInfo.UpdateSubscriberCount(availableSubscription.LetterInfoGuid); } } } } } else { if (IsSubscribed(availableSubscription.LetterInfoGuid)) { // unsubscribe foreach (LetterSubscriber s in userSubscriptions) { if (s.LetterInfoGuid == availableSubscription.LetterInfoGuid) { subscriptions.Delete(s); } } LetterInfo.UpdateSubscriberCount(availableSubscription.LetterInfoGuid); } } } GetSubscriptions(); BindPreferences(); lblUnverifiedWarning.Visible = false; //btnSavePreferences.Text = Resource.NewsletterSavePreferencesButton; System.Threading.Thread.Sleep(1000); UpdatePanel1.Update(); }
private void DoSubscribe(LetterInfo letter, string email) { if (email == "*****@*****.**") { return; } //I've been seeing a lot of this from a bot LetterSubscriber s = subscriptions.Fetch(siteSettings.SiteGuid, letter.LetterInfoGuid, email); bool needToSendVerification = false; if (s == null) { s = new LetterSubscriber(); s.SiteGuid = siteSettings.SiteGuid; s.EmailAddress = email; s.LetterInfoGuid = letter.LetterInfoGuid; if (showFormatOptions) { s.UseHtml = rbHtmlFormat.Checked; } else { s.UseHtml = htmlIsDefault; } if ((currentUser != null) && (string.Equals(currentUser.Email, email, StringComparison.InvariantCultureIgnoreCase))) { s.UserGuid = currentUser.UserGuid; s.IsVerified = true; } else { // user is not authenticated but may still exist // attach userguid but don't flag as verified // because we don't know that the user who submited the form is the account owner SiteUser siteUser = SiteUser.GetByEmail(siteSettings, email); if (siteUser != null) { s.UserGuid = siteUser.UserGuid; } } s.IpAddress = SiteUtils.GetIP4Address(); subscriptions.Save(s); LetterInfo.UpdateSubscriberCount(s.LetterInfoGuid); if (WebConfigSettings.LogNewsletterSubscriptions) { log.Info(s.EmailAddress + " just subscribed to newsletter " + letter.Title); } if (!s.IsVerified) { needToSendVerification = true; } } else { // we found an existing subscription if (!s.IsVerified) { // if the current authenticated user has the same email mark it as verified if ((currentUser != null) && (string.Equals(currentUser.Email, email, StringComparison.InvariantCultureIgnoreCase))) { s.UserGuid = currentUser.UserGuid; if (showFormatOptions) { s.UseHtml = rbHtmlFormat.Checked; } subscriptions.Save(s); subscriptions.Verify(s.SubscribeGuid, true, Guid.Empty); } else if (s.BeginUtc < DateTime.UtcNow.AddDays(-WebConfigSettings.NewsletterReVerifcationAfterDays)) { // if the user never verifed before and its been at least x days go ahead and send another chance to verify needToSendVerification = true; // TODO: maybe we should log this in case some spam script is using the same email over and over // or maybe we should add a verification sent count on subscription } } } //added 2012-05-16 to support intranet scenarios where verification is not required if (!WebConfigSettings.NewsletterRequireVerification) { if (!s.IsVerified) { s.IsVerified = true; subscriptions.Save(s); } needToSendVerification = false; } if (needToSendVerification) { NewsletterHelper.SendSubscriberVerificationEmail( siteRoot, email, s.SubscribeGuid, letter, siteSettings); } }
private string ReplaceHtmlTokens(string htmlBody, LetterSubscriber subscriber, LetterInfo letterInfo) { // mail merge //string mergedMessage = htmlBody; StringBuilder mergedMessage = new StringBuilder(); mergedMessage.Append(htmlBody); if (unsubscribeUrl.Length > 0) { if (subscriber.UserGuid == Guid.Empty) { mergedMessage.Replace( Letter.UnsubscribeToken, "<a href='" + unsubscribeUrl + "?s=" + subscriber.SubscribeGuid.ToString() + "'>" + unsubscribeLinkText + "</a>"); } else { mergedMessage.Replace( Letter.UnsubscribeToken, "<a href='" + unsubscribeUrl + "?u=" + subscriber.UserGuid.ToString() + "&l=" + subscriber.LetterInfoGuid.ToString() + "'>" + unsubscribeLinkText + "</a>"); } } if (webPageUrl.Length > 0) { mergedMessage.Replace( Letter.WebPageLinkToken, "<a href='" + webPageUrl + "'>" + viewAsWebPageText + "</a>"); } else { mergedMessage.Replace(Letter.WebPageLinkToken, string.Empty); } mergedMessage.Replace(Letter.UserNameToken, subscriber.Name.Coalesce(subscriber.EmailAddress)); mergedMessage.Replace(Letter.UserEmailToken, subscriber.EmailAddress); if (subscriber.UserGuid != Guid.Empty) { // site member if ((subscriber.FirstName.Length > 0) && (subscriber.LastName.Length > 0)) { // if both first and last have values replace the tokens mergedMessage.Replace(Letter.UserFirstNameToken, subscriber.FirstName); mergedMessage.Replace(Letter.UserLastNameToken, subscriber.LastName); } else if (subscriber.FirstName.Length > 0) { // if only first name has a value then replace last name with empty string and first name with first name mergedMessage.Replace(Letter.UserFirstNameToken, subscriber.FirstName); mergedMessage.Replace(Letter.UserLastNameToken, string.Empty); } else { // if neither first name nor last name has a value replace last with empty string and first with display name mergedMessage.Replace(Letter.UserFirstNameToken, subscriber.Name); mergedMessage.Replace(Letter.UserLastNameToken, string.Empty); } } else { //non site member // currently non site members do not have first or last name data fields // but in case we add those this logic will already be here if ((subscriber.FirstName.Length > 0) && (subscriber.LastName.Length > 0)) { // if both first and last have values replace the tokens mergedMessage.Replace(Letter.UserFirstNameToken, subscriber.FirstName); mergedMessage.Replace(Letter.UserLastNameToken, subscriber.LastName); } else if (subscriber.FirstName.Length > 0) { // if only first name has a value then replace last name with empty string and first name with first name mergedMessage.Replace(Letter.UserFirstNameToken, subscriber.FirstName); mergedMessage.Replace(Letter.UserLastNameToken, string.Empty); } else { // this is the expected path for non site members since we don't have data fields for first and last // replace with token defaults from letterInfo (ie Valued Customer could be the first and last token defaults) mergedMessage.Replace(Letter.UserFirstNameToken, letterInfo.FirstNameDefault); mergedMessage.Replace(Letter.UserLastNameToken, letterInfo.LastNameDefault); } } mergedMessage.Replace(Letter.UserDisplayNameToken, subscriber.Name.Coalesce(letterInfo.DisplayNameDefault)); return(mergedMessage.ToString()); // //return htmlBody; }
private void DoSubscribe(LetterInfo letter, string email) { LetterSubscriber s = subscriptions.Fetch(siteSettings.SiteGuid, letter.LetterInfoGuid, email); bool needToSendVerification = false; if (s == null) { s = new LetterSubscriber(); s.SiteGuid = siteSettings.SiteGuid; s.EmailAddress = email; s.LetterInfoGuid = letter.LetterInfoGuid; if (showFormatOptions) { s.UseHtml = rbHtmlFormat.Checked; } else { s.UseHtml = htmlIsDefault; } if ((currentUser != null) && (string.Equals(currentUser.Email, email, StringComparison.InvariantCultureIgnoreCase))) { s.UserGuid = currentUser.UserGuid; s.IsVerified = true; } else { // user is not authenticated but may still exist // attach userguid but don't flag as verified // because we don't know that the user who submited the form is the account owner SiteUser siteUser = SiteUser.GetByEmail(siteSettings, email); if (siteUser != null) { s.UserGuid = siteUser.UserGuid; } } s.IpAddress = SiteUtils.GetIP4Address(); subscriptions.Save(s); LetterInfo.UpdateSubscriberCount(s.LetterInfoGuid); if (!s.IsVerified) { needToSendVerification = true; } } else { // we found an existing subscription if (!s.IsVerified) { // if the current authenticated user has the same email mark it as verified if ((currentUser != null) && (string.Equals(currentUser.Email, email, StringComparison.InvariantCultureIgnoreCase))) { s.UserGuid = currentUser.UserGuid; if (showFormatOptions) { s.UseHtml = rbHtmlFormat.Checked; } subscriptions.Save(s); subscriptions.Verify(s.SubscribeGuid, true, Guid.Empty); } else if (s.BeginUtc < DateTime.UtcNow.AddDays(-WebConfigSettings.NewsletterReVerifcationAfterDays)) { // if the user never verifed before and its been at least x days go ahead and send another chance to verify needToSendVerification = true; // TODO: maybe we should log this in case some spam script is using the same email over and over // or maybe we should add a verification sent count on subscription } } } if (needToSendVerification) { string verificationTemplate = ResourceHelper.GetMessageTemplate(SiteUtils.GetDefaultCulture(), "NewsletterVerificationEmailMessage.config"); string confirmLink = siteRoot + "/eletter/Confirm.aspx?s=" + s.SubscribeGuid.ToString(); string messageBody = verificationTemplate.Replace("{NewsletterName}", letter.Title).Replace("{ConfirmationLink}", confirmLink).Replace("{SiteLink}", siteRoot); string subject = string.Format(CultureInfo.InvariantCulture, Resource.NewsletterVerifySubjectFormat, letter.Title); EmailMessageTask messageTask = new EmailMessageTask(SiteUtils.GetSmtpSettings()); messageTask.SiteGuid = siteSettings.SiteGuid; if (letter.FromAddress.Length > 0) { messageTask.EmailFrom = letter.FromAddress; } else { messageTask.EmailFrom = siteSettings.DefaultEmailFromAddress; } messageTask.EmailTo = email; messageTask.Subject = subject; messageTask.TextBody = messageBody; messageTask.QueueTask(); WebTaskManager.StartOrResumeTasks(); } }