将PDF作为电子邮件发送

时间:2013-01-03 04:09:18

标签: email pdf gridview itextsharp itext

我有以下代码,可以将gridview和detailview附加为PDF文件 但我希望实现的不是下载它,而是将其作为电子邮件附加到指定用户。有什么方法可以修改代码,以便我可以将其作为电子邮件发送而不是下载。

   Dim strQuery As String = "SELECT * FROM [PointOrder] WHERE PointOrderId =" & Session("PointOrder")
    Dim cmd As New SqlCommand(strQuery)
    Dim dt As DataTable = GetData(cmd)

    Dim DetailsView1 As New DetailsView()
    DetailsView1.AllowPaging = False
    DetailsView1.DataSource = dt
    DetailsView1.DataBind()

    Dim strQuery2 As String = "SELECT PointsOrderDetail.RedeemId, Redeem.RedeemName, Redeem.RedeemPoints, PointsOrderDetail.RedeemOrderQuantity, Redeem.RedeemPoints * PointsOrderDetail.RedeemOrderQuantity AS Total_Points FROM PointsOrderDetail INNER JOIN Redeem ON PointsOrderDetail.RedeemId = Redeem.RedeemId WHERE PointsOrderDetail.PointOrderId = " & Session("PointOrder")
    Dim cmd2 As New SqlCommand(strQuery2)
    Dim dt2 As DataTable = GetData(cmd2)

    Dim GridView1 As New GridView()
    GridView1.AllowPaging = False
    GridView1.DataSource = dt2
    GridView1.DataBind()


    Response.ContentType = "application/pdf"
    Response.AddHeader("content-disposition", "attachment;filename=" & Session("ID") & "-" & Session("PointOrder") & "-RedeemConfirm.pdf")
    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    Dim sw As New StringWriter()
    Dim hw As New HtmlTextWriter(sw)
    DetailsView1.RenderControl(hw)
    GridView1.RenderControl(hw)
    Dim sr As New StringReader(sw.ToString())
    Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F)
    Dim htmlparser As New HTMLWorker(pdfDoc)
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
    pdfDoc.Open()
    htmlparser.Parse(sr)
    pdfDoc.Close()
    Response.Write(pdfDoc)
    Response.End()

Private Function GetData(ByVal cmd As SqlCommand) As DataTable
    Dim dt As New DataTable()
    Dim strConnString As [String] = System.Configuration.ConfigurationManager.ConnectionStrings("LegacySGConnectionString").ConnectionString()
    Dim con As New SqlConnection(strConnString)
    Dim sda As New SqlDataAdapter()
    cmd.CommandType = CommandType.Text
    cmd.Connection = con
    Try
        con.Open()
        sda.SelectCommand = cmd
        sda.Fill(dt)
        Return dt
    Catch ex As Exception
        Throw ex
    Finally
        con.Close()
        sda.Dispose()
        con.Dispose()
    End Try
End Function

0 个答案:

没有答案