locked
Unsuscribe link - I am giving up RRS feed

  • Question

  • User-1352156089 posted

    Hi all,

    it's been weeks now that I am trying to make my application work but the results are really bad.

    I have built a mass mailer basing the code on the following tutorial: http://www.c-sharpcorner.com/UploadFile/47548d/how-to-send-bulk-email-using-Asp-Net/

    What I need to do here is to add an unsubscribe link to the template used for my mass email and have my unsubscribe.aspx page capturing the user ID and email (probably in a query string) to allow user to update their subscription in the unsubscribe.aspx page.

    I have posted several questions on other forums but the feedbacks were not really helpful to me that I am on a learning stage. I was suggested to read other posts but the approaches used in the above posts/forums were different so not really adaptable to my case.

    The problem that I am facing is:

    - how can I modify the code so that I can create a dynamic link "Unsubscribe" that can link to the unsubscribe.aspx which gets the user id and email address of the receiver who has decided to unsubscribe?

    Can you help me please in adjusting my code in order to include this functionality?

    This is my code which retreives the users who accepted to receive the email

    Protected Sub btnBind_Click(sender As Object, e As EventArgs)
            Try
                conn.Open()
                Dim cmd As New SqlCommand("Select Id,FirstName,LastName,Newsletter,Email from AspNetUsers Where Newsletter=1", conn)
                Dim adp As New SqlDataAdapter(cmd)
                Dim ds As New DataSet()
                adp.Fill(ds)
                grvCustomers.DataSource = ds
                grvCustomers.DataBind()
                lbltotalcount.Text = grvCustomers.Rows.Count.ToString()
            Catch ex As SqlException
                ' Display your error messages '
                FailureText.Text = "Something went wrong with your sql server database"
                ErrorMessage.Visible = True
            Finally
    
                conn.Close()
            End Try
            btnBind.Visible = False
        End Sub

    This is my code which sends the email/newsletter:

    Protected Sub btnSend_Click(sender As Object, e As EventArgs)
            Try
                lbltotalcount.Text = String.Empty
                For Each grow As GridViewRow In grvCustomers.Rows
                    Dim Id As String = grow.Cells(0).Text.Trim()
                    Dim FirstName As String = grow.Cells(1).Text.Trim()
                    Dim LastName As String = grow.Cells(2).Text.Trim()
                    Dim Newsletter As String = grow.Cells(3).Text.Trim()
                    Dim Email As String = grow.Cells(4).Text.Trim()
    
                    Dim filename As String = Server.MapPath("mytemplates/email.html")
                    Dim mailbody As String = System.IO.File.ReadAllText(filename)
                    mailbody = mailbody.Replace("##NAME##", FirstName)
                    Dim [to] As String = Email
                    Dim from As String = "xxx@xxxxxx.xxx"
                    Dim message As New MailMessage(from, [to])
                    message.Subject = "Test"
                    message.Body = mailbody
                    message.BodyEncoding = Encoding.UTF8
                    message.IsBodyHtml = True
                    Dim client As New SmtpClient("xxxx.xxxxxx.xxx", 25)
                    Dim basicCredential As New System.Net.NetworkCredential("xxx@xxxxxx.xxx", "xxxxxxx")
                    client.UseDefaultCredentials = True
                    client.Credentials = basicCredential
                    Try
    
                        client.Send(message)
                        emailrecepients.Visible = False
                        emailsent.Visible = True
                    Catch ex As Exception
                        ' Display your error messages '
                        FailureText.Text = "Emails did't go through for some reasons"
                        ErrorMessage.Visible = True
                    End Try
                Next
            Catch ex As SqlException
                ' Display your error messages '
                FailureText.Text = "Something went wrong with your connection to sql server database"
                ErrorMessage.Visible = True
            Finally
                conn.Close()
            End Try
        End Sub

    Thank you so much in advance

    Friday, November 28, 2014 9:36 AM

Answers

  • User-760709272 posted

    What is "newslettercheckbox"?  Is it a checkbox?  If so you need

    UpdateCmd.Parameters("@Newsletter").Value = newslettercheckbox.Checked

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 29, 2014 1:58 PM

All replies

  • User-760709272 posted

    Have something like this in your template

    To unsubscribe <a href="##UNSUBSCRIBE##">click here</a>

    then update your code

    mailbody = mailbody.Replace("##NAME##", FirstName)
    string url = string.Format("http://{0}/unsubscribe.aspx?id={1}", Request.Url.Authority, Id);
    mailbody = mailbody.Replace("##UNSUBSCRIBE##", url)
    
    

    if you want to pass the email too then something like

    string url = string.Format("http://{0}/unsubscribe.aspx?id={1}&email={2}", Request.Url.Authority, Id, Server.UrlEncode(Email));

    Friday, November 28, 2014 9:49 AM
  • User-1352156089 posted

    Hi AidyF,

    you are a super  star!! You made my day. It does work!!

    I have implemented the second option with both Id and Email.

    Is it too much if I ask you guidelines/tips to build  the unsubscribe.aspx code behind?

    - I need to redirect the unsubscribers to an error page if they land in the unsubscribe.aspx without clicking on the unsubscribe link holding the 2 above strings:

    I tried with this but it doesn't seem to be working:

      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Request.QueryString("[Id]") <> "" AndAlso Request.QueryString("[Email]") Is Nothing Then
                Response.Redirect("http://localhost:53972/Deafult")
    
            End If
        End Sub

    - I need  the unsubscribers to update the field newsletter of my database from 1 to 0

    Do you think is it possible?

    Thank you

    Friday, November 28, 2014 12:50 PM
  • User-760709272 posted

    It'll be

    If Request.QueryString("Id") <> "" AndAlso Request.QueryString("Email") Is Nothing Then

    Just read the id and email and execute

    update table set newsletter = 0 where id = @id and email = @email

    Saturday, November 29, 2014 11:18 AM
  • User-1352156089 posted

    Hi AidyF,

    thank you again. I don't know why but it works only if I use the IsNot Nothing clause: 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Request.QueryString("Id") <> "" AndAlso Request.QueryString("Email") IsNot Nothing Then
                emaillbl.Text = Request.QueryString("Email").ToString()
            Else
                Response.Redirect("http://localhost:53972/Default")
            End If
        End Sub

    I also tried this code but I receive the following error:  

    updateCmd.Parameters.Add("@Newsletter", SqlDbType.Bit, "Newsletter"): string format input string incorrect format 

    What I am trying to do is to have the unsubscriber to check the checkbox and then press the Unsubbtn button to update the database column named Newsletter from True to False

        Protected Sub Unsubbtn_Click(sender As Object, e As EventArgs) Handles Unsubbtn.Click
            Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("DefaultConnection").ConnectionString)
            'Create Command object
            Dim nonqueryCommand As SqlCommand = conn.CreateCommand()
            Try
                conn.Open()
                Dim updateSql As String = "UPDATE AspNetUsers SET Newsletter=0 WHERE Id=@Id AND Email=@EMail"
                Dim UpdateCmd As New SqlCommand(updateSql, conn)
                UpdateCmd.Parameters.Add("@Newsletter", SqlDbType.Bit, "Newsletter")
                UpdateCmd.Parameters("@Newsletter").Value = newslettercheckbox
                UpdateCmd.ExecuteNonQuery()
            Catch ex As System.Data.SqlClient.SqlException
                ' Display your error messages '
                FailureText.Text = "Something went wrong with your sql server database"
                ErrorMessage.Visible = True
            Finally
                conn.Close()
                conn.Dispose()
            End Try
        End Sub
    End Class


    Thank you

    Saturday, November 29, 2014 1:03 PM
  • User-760709272 posted

    What is "newslettercheckbox"?  Is it a checkbox?  If so you need

    UpdateCmd.Parameters("@Newsletter").Value = newslettercheckbox.Checked

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 29, 2014 1:58 PM
  • User-1352156089 posted

    Yes, newslettercheckbox is a checkbox. The full code is below:

    <asp:CheckBox ID="newslettercheckbox" runat="server"  Text="Unsubscribe me"  CssClass="checkboxes" TextAlign="Right" />
    <asp:Button ID="Unsubbtn" runat="server" Text="Update" />

    I corrected my code as you suggested and the table is not updated with the new value.

    The error I get is: "Cannot convert type 'String' to 'Int32'"

    Any idea about what happens here?

    Protected Sub Unsubbtn_Click(sender As Object, e As EventArgs) Handles Unsubbtn.Click
     Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("DefaultConnection").ConnectionString)
    
            Dim nonqueryCommand As SqlCommand = conn.CreateCommand()
            Try
                conn.Open()
                Dim updateSql As String = "UPDATE AspNetUsers SET Newsletter=0 WHERE Id=@Id"
                Dim UpdateCmd As New SqlCommand(updateSql, conn)
                UpdateCmd.Parameters.Add("@Newsletter", SqlDbType.Bit)
                UpdateCmd.Parameters("@Newsletter").Value = newslettercheckbox.Checked
              UpdateCmd.ExecuteNonQuery()
            Catch ex As System.Data.SqlClient.SqlException
                ' Display your error messages '
                FailureText.Text = "Something went wrong with your sql server database"
                ErrorMessage.Visible = True
            Finally
                conn.Close()
                conn.Dispose()
            End Try
        End Sub

    Thanks

    Saturday, November 29, 2014 2:18 PM
  • User-1352156089 posted

    The was actually a mistake in my update command:

    the final and working code is:

       Protected Sub Unsubbtn_Click(sender As Object, e As EventArgs) Handles Unsubbtn.Click
            Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("DefaultConnection").ConnectionString)
            'Create Command object
            Dim nonqueryCommand As SqlCommand = conn.CreateCommand()
            Try
                Dim updateSql As String = "UPDATE AspNetUsers SET Newsletter=0 WHERE Id=@Id"
                Dim UpdateCmd As New SqlCommand(updateSql, conn)
                UpdateCmd.Parameters.Add("@Newsletter", SqlDbType.Bit).Value = newslettercheckbox.Checked
                UpdateCmd.Parameters.Add("@Id", SqlDbType.NVarChar, 128).Value = Request.QueryString("Id")
                conn.Open()
                UpdateCmd.ExecuteNonQuery()
            Catch ex As System.Data.SqlClient.SqlException
                ' Display your error messages '
                Response.Write(ex.ToString())
                'FailureText.Text = "Something went wrong with your sql server database"
                ErrorMessage.Visible = True
            Finally
                conn.Close()
                conn.Dispose()
            End Try
        End Sub

    Thanks AidyF for your help

    Sunday, November 30, 2014 7:15 AM