Attatch Gridview发送电子邮件正文VB.NET

时间:2015-06-04 07:31:33

标签: asp.net vb.net email gridview

我目前正在我的网页上以编程方式发送电子邮件,电子邮件工作正常,但我还想将gridview附加到电子邮件正文中。

这是我迄今为止发送电子邮件的VB代码......

Dim Uname As String = Page.User.Identity.Name.Substring(Page.User.Identity.Name.IndexOf("\") + 1)
            strFm = Uname + "@Inscapepeople.co.uk"
            If strTo <> "" Then
                Dim insMail As New System.Net.Mail.MailMessage(New MailAddress(strFm), New MailAddress(strTo))
                With insMail
                    For Each y As String In strToREST.Split(";")
                        If y = "" Then
                        Else
                            .CC.Add(y)
                        End If
                    Next
                    .Subject = subject
                    .Body = Greet + "<br />" + "<br />" + bodyTxt.Text + "<br />" + PName + "- " + PNum + "<br />" + "<br />" + "The Required Date was: " + ReqDateNow.Text + " ,The Required Date is now: " + ReqDateAfter.Text + "<br />" + "Regards," + "<br />" + "<br />"
                    .IsBodyHtml = True
                    Sig = Uname
                    If Sig.ToLower = "djones" Then Sig = "GBennett"
                    Dim source As String = Path + Sig + ".jpg"
                    If System.IO.File.Exists(source) = False Then
                        Msgbox.ShowAlertMessage("Signature not found, but the email was still sent.")
                    Else
                        Dim lr As New LinkedResource(source)
                        lr.ContentId = "Signature"
                        Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString(insMail.Body + "<img width=""500"" height=""400"" src=cid:Signature>", Nothing, "text/html")
                        htmlView.LinkedResources.Add(lr)
                        .AlternateViews.Add(htmlView)
                    End If
                End With
                Dim smtp As New System.Net.Mail.SmtpClient
                smtp.EnableSsl = False
                smtp.Host = "192.168.50.2"
                smtp.Port = 25
                smtp.UseDefaultCredentials = True
                smtp.Send(insMail)

我在网上搜索过,找不到任何可以帮助我的东西,我想知道我是否可以得到一些帮助,谢谢你。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

我建议使用以下函数将gridview转换为Html:

Private Function GridViewToHtml(ByVal gv As GridView) As String
    Dim sb As New StringBuilder
    Dim sw As New StringWriter
    Dim hw As New HtmlTextWriter(sw)
    gv.RenderControl(hw)
    Return sb.ToString()
End Function

然后您可以调用此功能并将其添加到电子邮件正文中。