【问题标题】:Query string in email body in asp.net在 asp.net 中的电子邮件正文中的查询字符串
【发布时间】:2014-11-14 17:37:52
【问题描述】:

我在创建电子邮件正文中的查询字符串时遇到问题。你能帮帮我吗?

场景是用户收到我的电子邮件,然后我需要在这封电子邮件中添加一个“取消订阅”链接,该链接将用户重定向到他/她可以取消订阅的页面。我将这些电子邮件发送给已经注册的用户。

我用来发送这些批量电子邮件的代码如下:

-这显示使用 SQLCommand、DataSet 和 SqlDataAdapter 从 AspnetUser 表中获取的数据:

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

然后我检索数据并将其放入网格视图中,然后发送批量电子邮件:

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

电子邮件已正确发送给所有已注册并决定接收我的电子邮件的用户,但我如何在此电子邮件中添加查询字符串以允许用户在无需登录的情况下取消订阅?

提前感谢您与我分享一些代码。

【问题讨论】:

    标签: query-string email request.querystring


    【解决方案1】:

    根据您的问题,我认为您希望用户无论何时想取消订阅该消息,都应该在电子邮件正文中获得该链接。你可以这样尝试:-

      msg.Body = "<a href='http://www.yoursite.com/SignUp.aspx?nyckel=" + uniqueid + "'></a>";
    

    或者从这里获取参考并根据需要进行自定义,

    Unsubscribe link

    【讨论】:

    • 感谢您的反馈。这个解决方案仍然不起作用,因为 message.Body 已经按照我上面的代码用于使用 html 模板。
    【解决方案2】:

    我发现了如何做到这一点:

    在发送时事通讯中:

         Dim url As String = String.Format("http://localhost:53972/Newsletter/unsubscribe?Id={1}&Email={2}", Request.Url.Authority, Id, Server.UrlEncode(Email))
                mailbody = mailbody.Replace("##UNSUBSCRIBE##", url)
    

    在退订中:

    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 '
            FailureText.Text = "Something went wrong with your sql server database"
            ErrorMessage.Visible = True
        Finally
            conn.Close()
            conn.Dispose()
        End Try
    End Sub
    

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-28
      • 2017-08-22
      • 2013-03-20
      • 2020-01-12
      相关资源
      最近更新 更多