Listing J
'##############################################################################
' Author: Kevin Koch
' Description:  This class provides asynchronous access to methods which
'              send email to customers. The data required by the methods
'              are provided in the constructor, and used in the method body.
'##############################################################################
 
 
 
 
Imports System.Web.Mail
Imports System.Threading
Imports System.Configuration.ConfigurationSettings
Imports System.Web.UI.WebControls
Imports System.IO
Imports System.Web.UI
 
 
Public Class EmailUtil
 
 
    Private Const CLASS_NAME As String = "EmailUtil"
 
 
    Private FROM_ADDRESS As String = AppSettings("AdminEmail")
    Private SMTP_SERVER As String = AppSettings("SMTPServer")
 
 
    Private m_OrderDs As DataSet
    Private m_Recipient As String
    Private m_RecipientName As String
 
 
    '=====================================================================================================================
    '   CONSTRUCTORS
    '=====================================================================================================================
    Public Sub New(ByVal OrderDs As DataSet, ByVal Recipient As String, ByVal RecipientName As String)
        m_OrderDs = OrderDs
        m_Recipient = Recipient
        m_RecipientName = RecipientName
    End Sub
 
 
    '=====================================================================================================================
    '   This method uses information setup in the constructor to send an email confirmation to a customer when
   '  a pending order is created. The method signature allows for asynchronous invocation
    '=====================================================================================================================
    Public Sub SendPendingOrderConfirmation(ByVal state As Object)
        Const METHOD_NAME As String = "SendPendingOrderConfirmation"
        Const MAIL_SUBJECT = "Your order at Tom's Books has been received"
        Try
            'Prepare a new Mail Message object
            Dim MailMsg As New MailMessage()
 
 
            Dim Msg As New Text.StringBuilder()
           Msg.Append("Dear: ").Append(Me.m_RecipientName).Append(Environment.NewLine).Append(Environment.NewLine)
           Msg.Append("Thank you for your order with Tom's Books. You will be notified when your order is shipped")
 
 
            MailMsg.Body = Msg.ToString()
            MailMsg.From = FROM_ADDRESS
            MailMsg.To = Me.m_Recipient
            MailMsg.Subject = MAIL_SUBJECT
 
 
            SmtpMail.SmtpServer = SMTP_SERVER
            SmtpMail.Send(MailMsg)
 
 
        Catch ex As Exception
           Log.WriteLogEntry(ex, CLASS_NAME, METHOD_NAME)
        Finally
            CType(state, AutoResetEvent).Set()
        End Try
    End Sub
 
 
 
 
    '=====================================================================================================================
    '   This method uses information setup in the constructor to send an email confirmation to a customer when
   '  an order that has been approved by John's application is created. The method signature allows for asynchronous invocation
    '=====================================================================================================================
    Public Sub SendCompletedOrderConfirmation(ByVal state As Object)
        Const METHOD_NAME As String = "SendCompletedOrderConfirmation"
       Const MAIL_SUBJECT = "Your order at Tom's Books has been shipped"
        Const HTML_BREAK As String = "<BR>"
        Try
            'Prepare a new Mail Message object
            Dim MailMsg As New MailMessage()
 
 
            Dim Msg As New Text.StringBuilder()
            Msg.Append("Dear: ").Append(Me.m_RecipientName).Append(Environment.NewLine).Append(HTML_BREAK)
            Msg.Append("Your order with Tom's Books placed on ").Append(Me.m_OrderDs.Tables(0).Rows(0).Item(JohnDb.OrderDb.FIELD_CREATION_DATE))
           Msg.Append(" has been confirmed and shipped. You will receive your books within a few business days.")
            Msg.Append(HTML_BREAK).Append(HTML_BREAK)
            Msg.Append("The following books have been shipped:").Append(HTML_BREAK).Append(HTML_BREAK)
 
 
            Dim TempDataGrid As New DataGrid()
            Dim NewColumn As BoundColumn
            TempDataGrid.AutoGenerateColumns = False
            TempDataGrid.HeaderStyle.Font.Bold = True
 
 
            ' Add the book name column
            NewColumn = New BoundColumn()
            NewColumn.DataField = JohnDb.BookDb.FIELD_BOOK_NAME
            NewColumn.HeaderText = "Book Ordered"
            TempDataGrid.Columns.Add(NewColumn)
 
 
            ' Add the author column
            NewColumn = New BoundColumn()
            NewColumn.DataField = JohnDb.BookDb.FIELD_AUTHOR
            NewColumn.HeaderText = "Author"
            TempDataGrid.Columns.Add(NewColumn)
 
 
            TempDataGrid.DataSource = Me.m_OrderDs.Tables(0)
            TempDataGrid.DataBind()
 
 
            Dim Sb As New System.Text.StringBuilder()
            Dim TextWriter As New StringWriter(Sb)
            Dim writer As New HtmlTextWriter(TextWriter)
 
 
            TempDataGrid.RenderControl(writer)
           Msg.Append(Sb.ToString())
 
 
            ' Set up the mail message
            MailMsg.BodyFormat = MailFormat.Html
            MailMsg.Body = Msg.ToString()
            MailMsg.From = FROM_ADDRESS
            MailMsg.To = Me.m_Recipient
            MailMsg.Subject = MAIL_SUBJECT
 
 
            SmtpMail.SmtpServer = SMTP_SERVER
            SmtpMail.Send(MailMsg)
 
 
        Catch ex As Exception
            Log.WriteLogEntry(ex, CLASS_NAME, METHOD_NAME)
        Finally
            CType(state, AutoResetEvent).Set()
        End Try
    End Sub
 
 
End Class