I am having problems in creating a query string in an email body. Could you please help me?
The scenario is that a user receives my email and then I need to add in this email an "unsubscribe" link which redirects the user to the page where he/she can unsubscribe. I am sending these emails to users who have signed up already.
The code I am using to send these bulk emails is the following:
-This display the data fetched from the AspnetUser table using SQLCommand,DataSet and SqlDataAdapter:
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
I then retreive data and put it into a gridview and then I send the bulk email:
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 = "[email protected]"
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("[email protected]", "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
The email is sent correctly to all users who have signed up and decided to receive my emails but how can I add the query string in this email to allow users to unsubscribe without needing to login?
Thank you in advance for sharing some code with me.
I found out how to accomplish that:
In send newsletter:
and in Unsubscribe:
Thanks