Wrox Programmer Forums
|
BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6
This is the forum to discuss the Wrox book ASP.NET 2.0 Instant Results by Imar Spaanjaars, Paul Wilton, Shawn Livermore; ISBN: 9780471749516
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 1st, 2007, 03:18 AM
Authorized User
 
Join Date: Oct 2007
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default

:p Bedankt

But i've found the problem why that didnt worked that was the error email.

This is the code for the good mail
 
Quote:
quote: Public Shared Sub SendConfirmationMessage( _
Quote:
    ByVal theShoppingCart As ShoppingCart, ByVal orderId As Integer, _
    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 = "Totaal:"
                myGridView.FooterRow.Cells(3).Text = _
                         String.Format("{0:c}", theShoppingCart.Total)

                Dim theSubject As String = "Jou bestelling bij Itasc:"
                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("[email protected]", emailAddress, theSubject, theMessage)
            End If
        Catch ex As Exception
        End Try
    End Sub


Should i do something like this?

 
Quote:
quote:mySmtpClient.IsBodyHtml = true
Quote:

Or theMessage.IsBodyHtml = true
But where do i add this?

 
Old November 1st, 2007, 10:47 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

theMessage is a string here, not a MailMessage so you can't set the IsBodyHtml property. Did you check out the link I posted? It deals with the same issue and I recommend a different approach.

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old November 1st, 2007, 12:48 PM
Authorized User
 
Join Date: Oct 2007
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default

So you're saying i should replace:

this:
 
Quote:
quote:Dim mySmtpClient As New System.Net.Mail.SmtpClient
Quote:
                theMessage = theMessage.Replace("##ShoppingCart##", _
                        GetHtmlFromControl(myGridView))
                theMessage = theMessage.Replace("##OrderNumber##", orderId.ToString())
                mySmtpClient.Send("[email protected]", emailAddress, theSubject, theMessage)


For this:
 
Quote:
quote:Dim mySmtpClient As SmtpClient = New SmtpClient()
Quote:
Dim myMessage As MailMessage = New MailMessage( _
  AppConfiguration.EmailFrom, AppConfiguration.EmailTo, subject, _
        errorMessage.ToString())
  myMessage.IsBodyHtml = True
  mySmtpClient.Send(myMessage)


Because ive tried that but it didn't work :(
(i think becouse of the names, so ive changed them into this:)
 
Quote:
quote: Dim mySmtpClient As SmtpClient = New SmtpClient()
Quote:
                Dim theMessage As MailMessage = New MailMessage( _
                  AppConfiguration.EmailFrom, AppConfiguration.EmailTo, subject, _
                        errorMessage.ToString())
                theMessage.IsBodyHtml = True
                mySmtpClient.Send(theMessage)


but still nothing.

 
Old November 1st, 2007, 03:39 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Are you sure you are sending HTML? Have you tried setting a breakpoint and then looked at errorMessage.ToString() ??

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old November 1st, 2007, 04:45 PM
Authorized User
 
Join Date: Oct 2007
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i mean that code is wrong i get the following errors:

Error 1 Type 'SmtpClient' is not defined. App_Code\Helpers.vb 178 37
Error 2 Local variable 'theMessage' is already declared in the current block. App_Code\Helpers.vb 179 21
Error 3 Type 'MailMessage' is not defined. App_Code\Helpers.vb 179 35


But i dont know wich names i should use in stead?

 
Old November 2nd, 2007, 01:38 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Right. It would really help if you'd provide the errors or problem in more detail. I thought the mail arrived but not as HTML and we were trying to solve that.

For the SmtpClient, you need to import the System.Net namespace.

For 'theMessage' variable, make sure you don't Dim the same variable twice. E.g. use MyMailMessage for the MailMessage and MessageBody for the message body for example. Names are arbitrarily but help in understanding what is what.

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old November 2nd, 2007, 02:41 AM
Authorized User
 
Join Date: Oct 2007
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Jeah it didn't format the mail correct

but you directed me to the other page so i jused that code
but i can't get that to work i keep getting errors

Warning 1 Namespace or type specified in the Imports 'System.Net.Namespace' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases. C:\Documents and Settings\Christian\Mijn documenten\Visual Studio 2005\WebSites\Nieuwe map\WebShop\App_Code\Helpers.vb 9 9 C:\...\WebShop
Thats error 1


Error 2 Type 'SmtpClient' is not defined. C:\Documents and Settings\Christian\Mijn documenten\Visual Studio 2005\WebSites\Nieuwe map\WebShop\App_Code\Helpers.vb 179 37 C:\...\WebShop
Error number 2


Error 3 Type 'MessageBody' is not defined. C:\Documents and Settings\Christian\Mijn documenten\Visual Studio 2005\WebSites\Nieuwe map\WebShop\App_Code\Helpers.vb 180 38 C:\...\WebShop

Error number 3


And this is my total code now on the page.

 
Quote:
quote:' This code is from the book "ASP.NET 2.0 Instant Results" by
Quote:
' Imar Spaanjaars, Paul Wilton and Shawn Livermore, published by Wrox.
' Copyright 2006 by Wiley Publishing Inc.
' Information about this book is available at www.wrox.com.
' Visit p2p.wrox.com to discuss this code in the Wrox forums.

Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Net.Namespace

''' <summary>
''' The Helpers class provides a few utility methods that are used in the site.
''' </summary>
Public Class Helpers

#Region "Constructor(s)"

  ''' <summary>
  ''' Hide the constructor to avoid instances of the Helpers class.
  ''' </summary>
  Private Sub New()
  End Sub

#End Region

#Region "Public Methods"

  ''' <summary>
  ''' Resizes an image to the specified maximum width
  ''' </summary>
  ''' <param name="fileNameIn">The full path to the image that must be resized.</param>
  ''' <param name="fileNameOut">The full path of the thumbnail that must be created.</param>
  ''' <param name="maxWidthOrHeight">The maximum width of the image.</param>
  ''' <remarks>When <paramref name="fileNameIn" /> equals <paramref name="fileNameOut" /> the original is overwritten.</remarks>
  Public Shared Sub ResizeImage(ByVal fileNameIn As String, ByVal fileNameOut As String, ByVal maxWidthOrHeight As Integer)
    Dim portrait As Boolean = False
    Dim bmpSource As Bitmap = Nothing

    ' Open the image
    bmpSource = New Bitmap(fileNameIn)

    Dim originalSize As Size = bmpSource.Size
    Dim newSize As Size = New Size(0, 0)

    bmpSource.Dispose()

    Dim maxHeightDecimal As Decimal = Convert.ToDecimal(maxWidthOrHeight)
    Dim maxWidthAsDecimal As Decimal = Convert.ToDecimal(maxWidthOrHeight)

    Dim resizeFactor As Decimal

    If (originalSize.Height > originalSize.Width) Then
      ' Portrait
      resizeFactor = Convert.ToDecimal(Decimal.Divide(originalSize.Heig ht, maxHeightDecimal))
      newSize.Height = maxWidthOrHeight
      newSize.Width = Convert.ToInt32(originalSize.Width / resizeFactor)
    Else
      ' Landscape or square
      resizeFactor = Convert.ToDecimal(Decimal.Divide(originalSize.Widt h, maxWidthAsDecimal))
      newSize.Width = maxWidthOrHeight
      newSize.Height = Convert.ToInt32(originalSize.Height / resizeFactor)
    End If

    Dim mySourceBitmap As Bitmap = Nothing
    Dim myTargetBitmap As Bitmap = Nothing
    Dim myGraphics As Graphics = Nothing

    Try
      mySourceBitmap = New Bitmap(fileNameIn)

      Dim newWidth As Integer = newSize.Width
      Dim newHeight As Integer = newSize.Height

      myTargetBitmap = New Bitmap(newWidth, newHeight)

      myGraphics = Graphics.FromImage(myTargetBitmap)

      myGraphics.InterpolationMode = _
                System.Drawing.Drawing2D.InterpolationMode.HighQua lityBicubic

      myGraphics.DrawImage(mySourceBitmap, New Rectangle(0, 0, newWidth, newHeight))
      mySourceBitmap.Dispose()

      ' Save the new image
      myTargetBitmap.Save(fileNameOut, ImageFormat.Jpeg)
    Catch
      Throw
    Finally
      If (myGraphics IsNot Nothing) Then
        myGraphics.Dispose()
      End If
      If mySourceBitmap IsNot Nothing Then
        mySourceBitmap.Dispose()
      End If
      If myTargetBitmap IsNot Nothing Then
        myTargetBitmap.Dispose()
      End If
    End Try

  End Sub

#End Region

    Public Shared Function CreateGridView() As GridView
        Dim myGridView As GridView = New GridView
        myGridView.AutoGenerateColumns = False
        myGridView.ShowFooter = True
        myGridView.Width = Unit.Percentage(100)
        myGridView.BorderWidth = Unit.Pixel(1)

        myGridView.HeaderStyle.BackColor = System.Drawing.Color.FromName("#ffb49f")
        myGridView.HeaderStyle.ForeColor = System.Drawing.Color.White
        myGridView.HeaderStyle.HorizontalAlign = HorizontalAlign.Left
        myGridView.HeaderStyle.Font.Name = "Arial"

        myGridView.RowStyle.BackColor = System.Drawing.Color.White
        myGridView.RowStyle.ForeColor = System.Drawing.Color.Black
        myGridView.RowStyle.Font.Name = "Arial"

        myGridView.FooterStyle.Font.Bold = True
        myGridView.FooterStyle.Font.Name = "Arial"

        Dim myGridViewColumn As New BoundField
        myGridViewColumn.HeaderText = "Title"
        myGridViewColumn.DataField = "Title"
        myGridView.Columns.Add(myGridViewColumn)

        myGridViewColumn = New BoundField
        myGridViewColumn.HeaderText = "Quantity"
        myGridViewColumn.DataField = "Quantity"
        myGridViewColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Right
        myGridView.Columns.Add(myGridViewColumn)

        myGridViewColumn = New BoundField()
        myGridViewColumn.HeaderText = "Price"
        myGridViewColumn.DataField = "Price"
        myGridViewColumn.DataFormatString = "{0:c}"
        myGridViewColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Right
        myGridView.Columns.Add(myGridViewColumn)

        myGridViewColumn = New BoundField()
        myGridViewColumn.HeaderText = "Total"
        myGridViewColumn.DataField = "SubTotal"
        myGridViewColumn.DataFormatString = "{0:c}"
        myGridViewColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Right
        myGridViewColumn.FooterStyle.HorizontalAlign = HorizontalAlign.Right
        myGridView.Columns.Add(myGridViewColumn)

        Return myGridView
    End Function

    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 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 = "Totaal:"
                myGridView.FooterRow.Cells(3).Text = _
                         String.Format("{0:c}", theShoppingCart.Total)

                Dim theSubject As String = "Jou bestelling bij Itasc:"
                Dim theMessage As String = My.Computer.FileSystem.ReadAllText _
                  (AppConfiguration.ConfirmationMessageLocation)

                Dim mySmtpClient As SmtpClient = New SmtpClient()
                Dim MyMailMessage As MessageBody = New MessageBody( _
                  AppConfiguration.EmailFrom, AppConfiguration.EmailTo, subject, _
                        errorMessage.ToString())
                MyMailMessage.IsBodyHtml = True
                mySmtpClient.Send(theMessage)
            End If
        Catch ex As Exception
        End Try
    End Sub


End Class
I've tried diffrent names but he visual studio says that there not defined i dont know where?

 
Old November 2nd, 2007, 05:03 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Here's a very simple example that sends out an HTML formatted message:

Dim myMessage As New MailMessage(fromAddress, toAddress, "Subject", "<h1>Body</h1>")
myMessage.IsBodyHtml = True
Dim mySmtpClient As New SmtpClient()
mySmtpClient.Send(myMessage)

This should be easy to implement in your code.

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old November 2nd, 2007, 05:10 AM
Authorized User
 
Join Date: Oct 2007
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here he says:

 Dim myMessage As New MailMessage

MailMessage is not declared where do i declare it?

And here he says:
Dim mySmtpClient As New SmtpClient()
SmtpClient is not declared

 
Old November 7th, 2007, 06:41 AM
Authorized User
 
Join Date: Oct 2007
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I've changed my code to this

(because like this i dont get any error and he sends the mail but still nothing)

 
Quote:
quote: Public Shared Sub SendConfirmationMessage( _
Quote:
        ByVal theShoppingCart As ShoppingCart, ByVal orderId As Integer, _
        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 Wrox WebShop"
                Dim theMessage As String = My.Computer.FileSystem.ReadAllText _
                  (AppConfiguration.ConfirmationMessageLocation)

                Dim mySmtpClient As New System.Net.Mail.MailMessage
                mySmtpClient.IsBodyHtml = True
                theMessage = theMessage.Replace("##ShoppingCart##", _
                        GetHtmlFromControl(myGridView))
                theMessage = theMessage.Replace("##OrderNumber##", orderId.ToString())
                Dim mClient As New System.Net.Mail.SmtpClient
                mClient.Host = "itascsrv01"
                mClient.Send("[email protected]", emailAddress, theSubject, theMessage)
                'Mail.SmtpMail.Send("[email protected]", emailAddress, theSubject, theMessage)
            End If
        Catch ex As Exception
        End Try
    End Sub
End Class
Als i used this:
 
Quote:
quote:Imports System.Net.Mail.MailMessage
Quote:
Imports System.Net.Mail.SmtpClient
I am really wondering what i did wrong :S






Similar Threads
Thread Thread Starter Forum Replies Last Post
Send mail and attachments with PHP mail function Lofa Beginning PHP 1 June 2nd, 2008 03:24 PM
mail function remo Beginning PHP 0 July 16th, 2007 05:45 AM
mail function sunsetbay Beginning PHP 5 July 3rd, 2005 08:59 PM
can't seem to get the mail() function to work Graham76 BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 0 October 5th, 2004 01:59 PM
mail () function Mark200408 BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 1 September 30th, 2004 07:59 AM





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