Quantcast
Viewing latest article 6
Browse Latest Browse All 9

Re: Emails being sent to strange recipients

It depends on how you pasted it in. Hopefully you correctly located the section of code that I intended to replace, but just for clarity, I have indicated it below:

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

The best way to find out if it works is to test it yourself. If you get any errors, report them back here.


Viewing latest article 6
Browse Latest Browse All 9

Trending Articles