Hello all,
Our association runs a fairly standard .Net membership management platform. We have an extra feature where an automatic monthly email is sent to members whose fees are due within the next month. This all works - accept - the courtesy "Dear ABC" at the top of the email appears as "Dear XYZ" ie. the person's first name is substituted for someone else's name. Kinda weird. As I said, other than this oddity, the entire process works perfectly.
Disclosure: the code was written by a third party as we have no knowledge of C# whatsoever.
Code attached if someone can shed any light into why this first name is wrong on emails.
Many thanks,
Mart
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="EmailReminder.aspx.cs" Inherits="EmailReminder" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><title></title></head><body><form id="form1" runat="server"><div></div></form></body></html>
using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.IO; using System.Linq; using System.Text; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using Microsoft.SqlServer.Server; public partial class EmailReminder : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { DoReminderJob(); DeActivateJob(); } private void DeActivateJob() { try { string connectionString = ConfigurationManager.ConnectionStrings["UpdateDetailsConnectionString"].ConnectionString; //This query will fetch users that should be reminded for membership renewal //Remind each month (as long as the status is active) start from 1 month before due date string com ="SELECT UserProfiles.UserId,DateLastPaid,FirstName,SurName,IsApproved FROM UserProfiles INNER JOIN dbo.aspnet_Membership ON dbo.UserProfiles.UserId = dbo.aspnet_Membership.UserId WHERE DATEDIFF(MONTH, DateLastPaid,GETDATE()) = 15 AND aspnet_Membership.IsApproved=1"; using (var myConnection = new SqlConnection(connectionString)) { myConnection.Open(); var ta = new SqlDataAdapter(); var myCommand = new SqlCommand(com, myConnection); ta.SelectCommand = myCommand; ta.SelectCommand.CommandType = CommandType.Text; var ds = new DataSet(); ta.Fill(ds); foreach (DataRow reader in ds.Tables[0].Rows) { Guid userId = Guid.Parse(reader[0].ToString()); DateTime lastDatePaid = Convert.ToDateTime(reader[1]); string firstName = reader[2].ToString(); string surName = reader[3].ToString(); var member = Membership.GetUser(userId); //Deactivate the user member.IsApproved = false; Membership.UpdateUser(member); //Log deactivated user LogMemeberDeactivation(firstName, surName, member.Email, lastDatePaid); } myConnection.Close(); } } catch (Exception ex) { Logger.Log(ex, "Memeber deactivation"); } } private void DoReminderJob() { try { string connectionString = ConfigurationManager.ConnectionStrings["UpdateDetailsConnectionString"].ConnectionString; //This query will fetch users that should be reminded for membership renewal //Remind each month (as long as the status is active) start from 1 month before due date string com = "SELECT UserProfiles.UserId,DateLastPaid,FirstName,SurName,IsApproved FROM UserProfiles INNER JOIN dbo.aspnet_Membership ON dbo.UserProfiles.UserId = dbo.aspnet_Membership.UserId WHERE DATEDIFF(MONTH, DateLastPaid,GETDATE()) = 11 AND (DateLastReminder is null OR DATEDIFF(DAY, DateLastReminder,GETDATE()) > 31) AND aspnet_Membership.IsApproved=1"; using (var myConnection = new SqlConnection(connectionString)) { myConnection.Open(); var ta = new SqlDataAdapter(); var myCommand = new SqlCommand(com, myConnection); ta.SelectCommand = myCommand; ta.SelectCommand.CommandType=CommandType.Text; var ds = new DataSet(); ta.Fill(ds); //Prepare the template mail string template = null; if (ds.Tables[0].Rows.Count>0) { using (var sr = new StreamReader(Server.MapPath("~/MailTemplates/EmailReminder.html"), Encoding.UTF8)) { template = sr.ReadToEnd(); } } foreach (DataRow reader in ds.Tables[0].Rows) { Guid userId = Guid.Parse(reader[0].ToString()); DateTime lastDatePaid = Convert.ToDateTime(reader[1]); string firstName = reader[2].ToString(); string surName = reader[3].ToString(); string email = Membership.GetUser(userId).Email; // Define name/value pairs to be replaced. var replacements = new Dictionary<string, string>(); replacements.Add("##FirstName##", firstName); replacements.Add("##DueDate##", lastDatePaid.AddYears(1).ToShortDateString()); // Replace template with parameters foreach (var replacement in replacements) { template = template.Replace(replacement.Key, replacement.Value); } //Send the email var newMail = new Mail(); newMail.FromDisplayName = "CYCANL Admin"; newMail.From = "XXXXX@cycanl.ca"; newMail.SendMail(new[] { email }, "Membership Renewal Reminder", template); //Update last reminder date string com2 = "UPDATE UserProfiles SET DateLastReminder=getdate() WHERE UserId=@UserId"; var myCommand2 = new SqlCommand(com2, myConnection); myCommand2.Parameters.Clear(); myCommand2.Parameters.AddWithValue("@UserId", userId); myCommand2.ExecuteNonQuery(); //Log reminder LogReminders(firstName, surName, email, lastDatePaid); } myConnection.Close(); } } catch (Exception ex) { Logger.Log(ex, "Email Reminder"); } } private void LogReminders(string firstName, string surName, string email, DateTime lastDatePaid) { try { var sb = new StringBuilder(); sb.AppendLine("Date: " + DateTime.Now.ToString()); sb.AppendLine("Member Name: " + firstName + " " + surName); sb.AppendLine("Member Email: " + email); sb.AppendLine("Last Paid Date: " + lastDatePaid.ToShortDateString()); sb.AppendLine(" "); File.AppendAllText(Server.MapPath("~/LogFiles/RemindersLog.txt"), sb.ToString()); Response.Write(sb.Replace(Environment.NewLine, "<br />").ToString()); } catch (Exception ex) { Logger.Log(ex, "Email Reminder RemindersLog"); } } private void LogMemeberDeactivation(string firstName, string surName, string email, DateTime lastDatePaid) { try { var sb = new StringBuilder(); sb.AppendLine("Date: " + DateTime.Now.ToString()); sb.AppendLine("Member Name: " + firstName + " " + surName); sb.AppendLine("Member Email: " + email); sb.AppendLine("Last Paid Date: " + lastDatePaid.ToShortDateString()); sb.AppendLine(" "); File.AppendAllText(Server.MapPath("~/LogFiles/DeactivationLog.txt"), sb.ToString()); Response.Write(sb.Replace(Environment.NewLine, "<br />").ToString()); } catch (Exception ex) { Logger.Log(ex, "Reminder DeactivationLog"); } } }
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><title></title></head><body> Dear ##FirstName##<br /><br /> This is a reminder that your membership of CYCANL will expire on ##DueDate##<br/><br/><a href="http://www.XXXXX.xxx/XXXX.aspx">Click here to renew immediately.</a></body></html>