p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 (http://p2p.wrox.com/forumdisplay.php?f=252)
-   -   How to show plain format from html (http://p2p.wrox.com/showthread.php?t=71526)

hdpark November 19th, 2008 10:21 AM

How to show plain format from html
 
Hi Imar,

From your Webshop's SendConfirmationMessage is working great in html format. I need to send email in plain format, not html. How can I do it?

Your kind advice would be appreciated.
HD


Public Shared Function GetHtmlFromControl(ByVal theControl As WebControl) As String
        Dim myStringBuilder As StringBuilder = New StringBuilder()
        Dim myStringWriter As System.IO.StringWriter = _
              New IO.StringWriter(myStringBuilder)
        Dim myHtmlTextWriter As HtmlTextWriter = New HtmlTextWriter(myStringWriter)
        theControl.RenderControl(myHtmlTextWriter)
        Return myStringBuilder.ToString()
    End Function
    Public Shared Sub SendConfirmationMessage( _
        ByVal theShoppingCart As ShoppingCart, ByVal orderId As Integer, _
        ByVal userName As String, ByVal emailAddress As String)

        Try
            Dim myGridView As GridView = CreateGridView()
            myGridView.DataSource = theShoppingCart.Items
            myGridView.DataBind()

            If myGridView.Rows.Count > 0 Then
                myGridView.FooterRow.Cells(0).Text = "Totals:"
                myGridView.FooterRow.Cells(3).Text = String.Format("{0:c}", theShoppingCart.Total)

                Dim theSubject As String = "Your order at Sports Camps"
                Dim theMessage As String = My.Computer.FileSystem.ReadAllText _
                  (AppConfiguration.ConfirmationMessageLocation)

                Dim mySmtpClient As New System.Net.Mail.SmtpClient
                theMessage = theMessage.Replace("##ShoppingCart##", GetHtmlFromControl(myGridView))
                theMessage = theMessage.Replace("##OrderNumber##", orderId.ToString())
                mySmtpClient.Send("hdpark@myemail.com", emailAddress, theSubject, theMessage)
            End If
        Catch ex As Exception
        End Try
    End Sub

--------
ConfirmationMessage.txt:
<html>
<head>
  <title>Your Order at Sports Camps WebShop</title>
</head>
<body>
  Dear customer,<br /><br />
  Thank you for your order at Sports Camps WebShop. Your order number is
          <b>##OrderNumber##</b><br /><br />
  Below you find a list of the products you have ordered. The goods will ship as
  soon as we receive your payment.<br /><br />
  ##ShoppingCart##
  <br /><br />
  Thanks<br /><br />
  The Sports Camps WebShop Team
</body>
</html>


Imar November 19th, 2008 04:06 PM

Hi there,

You can construct a MailMessage instance, set the Body to your plain text and IsBodyHtml to False and then send the message using the Send method of the mail client.

http://msdn.microsoft.com/en-us/libr...ilmessage.aspx

Cheers,

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.

hdpark November 20th, 2008 12:16 AM

Hi Imar,

I cannot understand your advice.
Would you please explain more?

Thanks,
HD


Imar November 20th, 2008 04:25 AM

Hi there,

Did you look at the links I posted? You'll find example code in them:

Dim message As MailMessage = new MailMessage(
               "jane@contoso.com",
               "ben@contoso.com",
               "Quarterly data report.",
               "Plain text here.")
' optionally, set properties on message here:

' message.Body = "Plain Text Boxy" ' not necessary as it has been set in the constructor
' message.To.Add(other recipients go here)
' message.IsBodyHtml = False

Dim mySmtpClient As New System.Net.Mail.SmtpClient
mySmtpClient.Send(message)

This creates a new message, sets To, Fro, Subject and Body in the constructor and uses the SmtpClient to send it.

Hope this helps,

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.

hdpark November 20th, 2008 04:12 PM

Hi Imar,

I think it's off from my intention. My intention is at following red line.
Thanks,
HD

Dim mySmtpClient As New System.Net.Mail.SmtpClient
theMessage = theMessage.Replace("##ShoppingCart##", GetHtmlFromControl(myGridView))
theMessage = theMessage.Replace("##OrderNumber##", orderId.ToString())

===> Is there any way to convert html formatted message(theMessage) to plain text? <====

mySmtpClient.Send("hdpark@myemail.com", emailAddress, theSubject, theMessage)



Imar November 20th, 2008 07:03 PM

I am not sure I understand what you're asking. The mail template is in HTML, so if you need plain text, why don't you change the HTML in the mail template to plain text?

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.

hdpark November 24th, 2008 04:29 PM

Hi Imar,

When I email it in html format, it displays like following #A. How can I make recipient see it in email like in internet html format?

Thanks,
HD

#A:
<html>
<head>
<title>Your Order at Sports Camps WebShop</title>
</head>
<body>
Dear customer,<br /><br />
Thank you for your order at Sports Camps WebShop. Your order number is
         <b>1023</b><br /><br />
Below you find a list of the products you have ordered. The goods will ship as
soon as we receive your payment.<br /><br />
<div>
    <table cellspacing="0" rules="all" border="1" style="border-width:1px;border-style:solid;width:100%;border-collapse:collapse;">
        <tr align="left" style="color:White;background-color:#ffb49f;font-family:Arial;">
            <th scope="col">Title</th><th scope="col">Quantity</th><th scope="col">Price</th><th scope="col">Total</th>
        </tr><tr style="color:Black;background-color:White;font-family:Arial;">
            <td>TURP000_A</td><td align="right">1</td><td align="right">$0.17</td><td align="right">$0.17</td>
        </tr><tr style="font-family:Arial;font-weight:bold;">
            <td>Totals:</td><td>&nbsp;</td><td>&nbsp;</td><td align="right">$0.17</td>
        </tr>
    </table>
</div>
<br /><br />
Thanks<br /><br />
The Sports Camps WebShop Team
</body>
</html>

Imar November 24th, 2008 05:04 PM

Now you want it in HTML format? I thought you wanted it as plain text? Sorry, but I lost you....

Can you explain - in full detail - what it is that you need, and what you already have? Otherwise, I can't help much.

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.

hdpark November 25th, 2008 09:58 AM

Thank you Imar for your kind understanding even if I made you confuse.
1. My problem is : When I email it, it displays in html code format(following #A).
2. My goal is : How can I make it display in web browser format(following #B), not code style format(following #A).
Your kind advice would be kindly appreciated.

#A code style format :
<html>
<head>
  <title>Your Order at Sports Camps WebShop</title>
</head>
<body>
  Dear customer,<br /><br />
  Thank you for your order at Sports Camps WebShop. Your order number is
          <b>1023</b><br /><br />
  Below you find a list of the products you have ordered. The goods will ship as
  soon as we receive your payment.<br /><br />
  <div>
    <table cellspacing="0" rules="all" border="1" style="border-width:1px;border-style:solid;width:100%;border-collapse:collapse;">
        <tr align="left" style="color:White;background-color:#ffb49f;font-family:Arial;">
            <th scope="col">Title</th><th scope="col">Quantity</th><th scope="col">Price</th><th scope="col">Total</th>
        </tr><tr style="color:Black;background-color:White;font-family:Arial;">
            <td>TURP000_A</td><td align="right">1</td><td align="right">$0.17</td><td align="right">$0.17</td>
        </tr><tr style="font-family:Arial;font-weight:bold;">
            <td>Totals:</td><td>?lt;/td><td>?lt;/td><td align="right">$0.17</td>
        </tr>
    </table>
</div>
  <br /><br />
  Thanks<br /><br />
  The Sports Camps WebShop Team
</body>
</html

#B web browser format:
Dear customer,

Thank you for your order at Sports Camps WebShop. Your order number is 1023

Below you find a list of the products you have ordered. The goods will ship as soon as we receive your payment.

table data display with border(I cannot put it here).

Thanks

The Sports Camps WebShop Team




Imar November 25th, 2008 12:13 PM

In that case, you should be able to do it the way you;re doing it now.

Here's how it works:

1. Create a Body that contains HTML

2. Set IsBodyHtml to true

If the text ends up looking like HTML, rather than the visual representation of it, you have a mail client that does not support HTML.

What client are you using?

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.


All times are GMT -4. The time now is 05:47 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.