Search code examples
asp.netvb.netemailgridview

How to insert a gridview with a row image button into the email body


I wrote some code that sends emails and inserts a table cell from Gridview.

Protected Sub SendRequestMail()
    Dim tryCount As Integer = 5
    Dim failed As Boolean = False
    Do
        Try
            failed = False

            Dim ToMailIDs As String
            ToMailIDs = LblRecipients.Text

            Using sw As New StringWriter()
                Using hw As New HtmlTextWriter(sw)

                    GridView1.RenderControl(hw)
                    Dim sr As New StringReader(sw.ToString())

                    Dim smtpSection As SmtpSection = CType(ConfigurationManager.GetSection("system.net/mailSettings/smtp"), SmtpSection)
                    Dim mm As MailMessage = New MailMessage()

                    mm.From = New MailAddress(smtpSection.From, "Notification")
                    For i As Integer = 0 To ToMailIDs.Split(","c).Length - 1
                        mm.To.Add(New MailAddress(ToMailIDs.Split(","c)(i)))
                    Next

                    mm.ReplyTo = New MailAddress("[email protected]")

                    mm.Subject = LblMrNumFull.Text & " | " & LblMrDate1x.Text & " | " & LblStorsName1.Text & " - Approval"

                    mm.Body = LblMrNumFull.Text & " | " & LblMrDate1x.Text & " | " & LblStorsName1.Text
                  
                    mm.Body += sw.ToString()

                    mm.Body += "<br />"
                    mm.Body += "Thanking you" 
                    
                    mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
                    mm.DeliveryNotificationOptions = DeliveryNotificationOptions.Delay
                    mm.Headers.Add("Disposition-Notification-To", "[email protected]")

                    ' ----- webconfig mail start
                    mm.IsBodyHtml = True
                    Dim smtp As SmtpClient = New SmtpClient
                    smtp.Host = smtpSection.Network.Host
                    smtp.EnableSsl = smtpSection.Network.EnableSsl
                    Dim networkCred As NetworkCredential = New NetworkCredential(smtpSection.Network.UserName, smtpSection.Network.Password)
                    smtp.UseDefaultCredentials = smtpSection.Network.DefaultCredentials
                    smtp.Credentials = networkCred
                    smtp.Port = smtpSection.Network.Port
                    smtp.Send(mm)
                    ' ----- webconfig mail end

                End Using
            End Using

        Catch ex As Exception
            failed = True
            tryCount = tryCount - 1
        Finally
            Response.Redirect("~/abc/xyx.aspx?NewID=" & Request.QueryString("NewID"))
        End Try

    Loop While failed AndAlso tryCount > 0
End Sub

That code worked, and the email was successfully sent. After that, I have added an image button to the last column in the Gridview:

enter image description here

Then emails stop being sent. Please suggest the above code for sending emails with or without the last column of image buttons.

tried nothing works

'To Export all pages.

GridView1.AllowPaging = False
Me.BindData_GridView1()

GridView1.HeaderRow.BackColor = Color.White
For Each cell As TableCell In GridView1.HeaderRow.Cells
    cell.BackColor = GridView1.HeaderStyle.BackColor
Next
For Each row As GridViewRow In GridView1.Rows
    row.BackColor = Color.White
    For Each cell As TableCell In row.Cells
        If row.RowIndex Mod 2 = 0 Then
            cell.BackColor = GridView1.AlternatingRowStyle.BackColor
        Else
            cell.BackColor = GridView1.RowStyle.BackColor
        End If
        cell.CssClass = "textmode"
    Next
Next

Solution

  • I got the solution from another forum, so I posted it below.

    HTML

    <%@ Page Title="" Language="vb" AutoEventWireup="false"  EnableEventValidation="false" MasterPageFile="~/abc/xyz.Master" CodeBehind="aaaa1.aspx.vb" Inherits="aaaa.sss" %>
    

    VB.NET

    Protected Sub SendRequestMail()
    Dim tryCount As Integer = 5
    Dim failed As Boolean = False
    Do
        Try
            failed = False
            Dim ToMailIDs As String
            ToMailIDs = LblRecipients.Text
            GridView1.Columns(GridView1.Columns.Count - 1).Visible = False
            Using sw As New StringWriter()
                Using hw As New HtmlTextWriter(sw)
                    GridView1.RenderControl(hw)
                    Dim sr As New StringReader(sw.ToString())
                    Dim smtpSection As Net.Configuration.SmtpSection = CType(ConfigurationManager.GetSection("system.net/mailSettings/smtp"), SmtpSection)
                    Dim mm As System.Web.Mail.MailMessage = New System.Web.Mail.MailMessage()
                    mm.From = New MailAddress(smtpSection.From, "Notification")
                    For i As Integer = 0 To ToMailIDs.Split(","c).Length - 1
                        mm.To.Add(New MailAddress(ToMailIDs.Split(","c)(i)))
                    Next
                    mm.ReplyTo = New MailAddress("[email protected]")
                    mm.Subject = LblMrNumFull.Text & " | " & LblMrDate1x.Text & " | " & LblStorsName1.Text & " - Approval"
                    mm.Body = LblMrNumFull.Text & " | " & LblMrDate1x.Text & " | " & LblStorsName1.Text
                    mm.Body += sw.ToString()
                    mm.Body += "<br />"
                    mm.Body += "Thanking you"
                    mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
                    mm.DeliveryNotificationOptions = DeliveryNotificationOptions.Delay
                    mm.Headers.Add("Disposition-Notification-To", "[email protected]")
                    ' ----- webconfig mail start
                    mm.IsBodyHtml = True
                    Dim smtp As SmtpClient = New SmtpClient
                    smtp.Host = smtpSection.Network.Host
                    smtp.EnableSsl = smtpSection.Network.EnableSsl
                    Dim networkCred As NetworkCredential = New NetworkCredential(smtpSection.Network.UserName, smtpSection.Network.Password)
                    smtp.UseDefaultCredentials = smtpSection.Network.DefaultCredentials
                    smtp.Credentials = networkCred
                    smtp.Port = smtpSection.Network.Port
                    smtp.Send(mm)
                    ' ----- webconfig mail end
                End Using
            End Using
            GridView1.Columns(GridView1.Columns.Count - 1).Visible = True
        Catch ex As Exception
            failed = True
            tryCount = tryCount - 1
        Finally
            Response.Redirect("~/abc/xyx.aspx?NewID=" & Request.QueryString("NewID"))
        End Try
    Loop While failed AndAlso tryCount > 0
    

    End Sub

    Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control) End Sub
    

    Thank you