Click here to Skip to main content
15,898,373 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hye all,

I have been trying like million methods to make my gridview visible inside email body..
One of the method is like this:

VB
Private Function RenderGridView() As String
       Dim writer As New StringWriter
       Dim htmlWriter As New HtmlTextWriter(writer)
       Try
           gdEmail.RenderControl(htmlWriter)
       Catch ex As HttpException

       End Try

       Return writer.ToString()
   End Function

   Public Overrides Sub VerifyRenderingInServerForm(ByVal control As System.Web.UI.Control)


   End Sub
   Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
       If Page IsNot Nothing Then
           Page.VerifyRenderingInServerForm(Me)
       End If
       MyBase.Render(writer)
   End Sub


 Dim StrEmailBody As String = ""

StrEmailBody = StrEmailBody & RenderGridView()



This is the result from the email I received:

<div>
	<table cellspacing="1" cellpadding="3" id="MainContent_gdEmail" style="background-color:White;border-color:White;border-width:2px;border-style:Ridge;">
		<tr style="color:#E7E7FF;background-color:#4A3C8C;font-weight:bold;">
			<th scope="col">Reg. No</th><th scope="col">Requestor</th><th scope="col">Control No</th><th scope="col">Cert. Type</th>
		</tr><tr style="color:Black;background-color:#DEDFDE;">
			<td>WC-DG/20120813/001363</td><td>EPJ6520-HAMDAN BIN JELANI</td><td>WC-DG0216</td><td>CALIBRATE</td>
		</tr><tr style="color:Black;background-color:#DEDFDE;">
			<td>WC-DG/20120817/001379</td><td>EPJ6520-HAMDAN BIN JELANI</td><td>WC-DG0044</td><td>CALIBRATE</td>
		</tr><tr style="color:Black;background-color:#DEDFDE;">
			<td>WC-DG/20120817/001380</td><td>EPJ6520-HAMDAN BIN JELANI</td><td>WC-DG0045</td><td>CALIBRATE</td>
		</tr><tr style="color:Black;background-color:#DEDFDE;">
			<td>WC-DG/20120823/001381</td><td>EPJ6520-HAMDAN BIN JELANI</td><td>WC-DG0053</td><td>CALIBRATE</td>
		</tr><tr style="color:Black;background-color:#DEDFDE;">
			<td>WC-DG/20120824/001384</td><td>EPJ6520-HAMDAN BIN JELANI</td><td>WC-DG0110</td><td>CALIBRATE</td>
		</tr><tr style="color:Black;background-color:#DEDFDE;">
			<td>WC-DG/20120828/001395</td><td>EPJ6520-HAMDAN BIN JELANI</td><td>WC-DG0271</td><td>CALIBRATE</td>
		</tr><tr style="color:Black;background-color:#DEDFDE;">
			<td>WC-DG/20120830/001406</td><td>EPJ6520-HAMDAN BIN JELANI</td><td>WC-DG0284</td><td>CALIBRATE</td>
		</tr><tr style="color:Black;background-color:#DEDFDE;">
			<td>WC-DG/20120903/001413</td><td>EPJ6520-HAMDAN BIN JELANI</td><td>WC-DG0371</td><td>CALIBRATE</td>
		</tr><tr style="color:Black;background-color:#DEDFDE;">
			<td>WC-DG/20120903/001414</td><td>EPJ6520-HAMDAN BIN JELANI</td><td>WC-DG0067</td><td>CALIBRATE</td>
		</tr><tr style="color:Black;background-color:#DEDFDE;">
			<td>WC-DG/20120905/001415</td><td>EPJ6520-HAMDAN BIN JELANI</td><td>WC-DG0137</td><td>CALIBRATE</td>
		</tr>
	</table>
</div>




What step am I missing? Thanks guys.




---UPDATED-----

Full Code:

 Protected Sub BtnApprove_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnApprove.Click
    SendNotificationEmail(regArr, Nothing, Nothing, Nothing, Nothing, Nothing, "APPROVED", nxt)
  End Sub

  Public Sub SendNotificationEmail(ByVal RegNo As String, ByVal RequestorName As String, ByVal RequestorID As String, ByVal CtrlNo As String, ByVal CertType As String, ByVal Remarks As String, ByVal ReceiverType As String, ByVal NextAppID As String)
If ReceiverType = "APPROVED" And NextAppID <> "LAST" Then
           
            Dim StrEmailBody As String = ""
            StrEmailBody = StrEmailBody & "Greetings," & Chr(10) & Chr(10)
            StrEmailBody = StrEmailBody & "You have new certificate awaiting for your approval: " & Chr(10) & Chr(10) & Environment.NewLine & Environment.NewLine
            StrEmailBody = StrEmailBody & "" & Environment.NewLine & Environment.NewLine
            StrEmailBody = StrEmailBody & RegNo
            StrEmailBody = StrEmailBody & "" & Environment.NewLine & Environment.NewLine
            StrEmailBody = StrEmailBody & "Please click below URL for reference: " & Chr(10) & "<" & "http://" & HttpContext.Current.Request.Url.Host & HttpContext.Current.Request.ApplicationPath & "/Account/Login.aspx" & ">" & Chr(10)
            StrEmailBody = StrEmailBody & RenderGridView()
         
            Dim StrEmailSubject = "TQM INSTRUMENT E-CERTIFICATE REQUISITION CONFIRMATION "
            SendEmailNotification(GetEmailObject(StrEmailBody, StrEmailSubject, RequestorID, NextAppID))
  End If
    End Sub

 Private Function RenderGridView() As String
        Dim writer As New StringWriter
        Dim htmlWriter As New HtmlTextWriter(writer)
        Try
            gdEmail.RenderControl(htmlWriter)
        Catch ex As HttpException

        End Try

        Return writer.ToString()
    End Function

    Public Overrides Sub VerifyRenderingInServerForm(ByVal control As System.Web.UI.Control)


    End Sub
    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        If Page IsNot Nothing Then
            Page.VerifyRenderingInServerForm(Me)
        End If
        MyBase.Render(writer)
    End Sub


    Private Function SendEmailNotification(ByVal MailSend As MailMessage) As String
        Dim webS As New EmailService.emailServices
        Dim CountTo As Integer = 1
        Dim CountCC As Integer = 1
        Dim ToStrS As String = ""
        Dim CCStrS As String = ""
        For Each ma As MailAddress In MailSend.To
            If CountTo = 1 Then
                ToStrS = ToStrS & ma.Address
            Else
                ToStrS = ToStrS & ";" & ma.Address
            End If
            CountTo = CountTo + 1
        Next

        For Each ma As MailAddress In MailSend.CC
            If CountCC = 1 Then
                CCStrS = CCStrS & ma.Address
            Else
                CCStrS = CCStrS & ";" & ma.Address
            End If
            CountCC = CountCC + 1
        Next

        MailSend.IsBodyHtml = True
    Dim s As String = webS.sendmailSmtp(MailSend.Subject, MailSend.Body, "sitinoraina.matyazaz@sep.epson.com.sg", Nothing, "", MailSend.From.Address, MailSend.From.Address)
      
        SendEmailNotification = s
    End Function

    Private Function GetEmailObject(ByVal EmailBody As String, ByVal EmailSubject As String, ByVal RequestorID As String, ByVal NextAppID As String) As MailMessage
        Dim TempMail As New MailMessage()
        TempMail.From = New MailAddress("eCalibration_Admin@sep.epson.com.sg")
        Dim ToColl As MailAddressCollection = TempMail.To
        Dim CCColl As MailAddressCollection = TempMail.CC

        ToColl.Add(CheckEmailAdd(NextAppID))

        TempMail.Subject = EmailSubject
        TempMail.Body = EmailBody & Environment.NewLine & "TO : " & Environment.NewLine & CheckEmailAdd(NextAppID)
        Return TempMail
    End Function

    Private Function CheckEmailAdd(ByVal touserid As String) As String
        Dim Result As String = ""
        Dim dt As DataTable = DBLayer.Util.Data.MsSql.DBFunction.BuildDataTable("SELECT * FROM EPJ_EMAIL WHERE USERID_T = '" & touserid & "' ", DBLayer.ClsConnection.getConnString("Maintenance"))

        If dt.Rows.Count > 0 Then
            Result = dt.Rows(0).Item("EMAILADD_T")
        End If

        CheckEmailAdd = Result
    End Function
Posted
Updated 15-Mar-13 16:07pm
v2
Comments
[no name] 15-Mar-13 21:05pm    
Did you tell your email message that is was to contain HTML?
Sergey Alexandrovich Kryukov 15-Mar-13 21:42pm    
No, OP did not, it's quite apparent from the result, and did not show relevant code.
I answered, please see.
—SA
snamyna 15-Mar-13 21:12pm    
Before sending the email, I've declared MailSend.IsBodyHtml = True
Sergey Alexandrovich Kryukov 15-Mar-13 21:43pm    
It's hard to see what have you really done from your question. :-)
I answered, please see.
—SA
snamyna 15-Mar-13 22:09pm    
Hye.. I've updated my question to full code that I've written in my project.

1 solution

All correct. This is because you provide no indication that your e-mail or a part in multipart e-mail has HTML content type, in this syntax:
Content-Type: text/plain


Please see:
http://en.wikipedia.org/wiki/MIME[^].

In this article, you will see other relevant mail message header, explanation of multipart message format and reference to relevant RFC memoranda standardizing a mail message structure.

By some reason, your code sample is totally irrelevant to you problem, as you never demonstrated how you formed the mail message text itself, but usually it is done using the class System.Net.Mail.MailMessage:
http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.aspx[^].

From this MSDN article you can find out how to set all the detail of the message.

For example, this page explains forming of simple HTML message:
http://www.systemnetmail.com/faq/3.1.2.aspx[^].

And this page — а multipart message with content types "plain/text" and "plain/html":
http://www.systemnetmail.com/faq/3.1.3.aspx[^].

To check the the full list of MIME media type, you need to address to IANA:
http://www.iana.org/assignments/media-types[^],
http://www.iana.org/assignments/media-types/text[^].

See also:
http://en.wikipedia.org/wiki/Internet_Assigned_Numbers_Authority[^],
http://www.iana.org/[^].

—SA
 
Share this answer
 
Comments
snamyna 18-Mar-13 23:28pm    
I have tried some examples found in google search. One thing I encounter when running the code is when I view the string as HTML visualizer, the HTML control appears!
But at sendmail() function, email.body passed is in string type.
Is there any way to convert string to HTML?
Sergey Alexandrovich Kryukov 18-Mar-13 23:56pm    
I don't understand: HTML is always a string, what's the problem? The question does not seem to make sense.
Do you need to escape entities, or what? You need to generate valid well-formed HTML, with tags, not escaped tag entities.
Anyway, I think I answered your question in full, will you accept it formally (green button)?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900