Saturday 17 March 2012

Read Message Body from .txt file and send Email......

  Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As Web.UI.ImageClickEventArgs) Handles btnSubmit.Click
        Try
            If cc.UserValidated Then

                Dim subject As String = "Order Return Information"

                Dim retFormate As String = Server.MapPath("Return_Order Success Mail Formate.txt")

                Dim oRead As StreamReader
                oRead = IO.File.OpenText(retFormate)


                Dim intLineCounter As Integer
                Dim Str As String = String.Empty
                intLineCounter = 0
                Do While oRead.Peek() <> -1
                    Str += oRead.ReadLine + Environment.NewLine
                Loop

                Dim CustomerName As String = txtfirstname.Text.Trim() & " " & txtlastname.Text.Trim()

                Dim CustomerEmailID As String = txtemailid.Text.Trim()

                Dim CustomerCaseNumber As String = CaseNumber.ToString()

                Dim CustomerShippingAddress As String = txtAddress.Text.Trim()

                Dim CustomerCellPhoneNumber As String = txtPhone.Text.Trim()

                Dim CustomerWatchModel As String = txtWatchModel.Text.Trim()

                Dim PurchaseDate As String = Request.Form("CAT_Custom_133843")
                If PurchaseDate <> "" Then
                    PurchaseDate = PurchaseDate
                Else
                    PurchaseDate = "N/A"
                End If
                Dim ReasonForReturn As String = String.Empty

                If txtMessage.Text <> "" Then
                    ReasonForReturn = txtMessage.Text.Trim()
                Else
                    ReasonForReturn = "N/A"
                End If


                Str = Str.Replace("<@CustomerName@>", CustomerName)
                Str = Str.Replace("<@CustomerEmailID@>", CustomerEmailID)
                Str = Str.Replace("<@CustomerCaseNumber@>", CustomerCaseNumber)
                Str = Str.Replace("<@CustomerShippingAddress@>", CustomerShippingAddress)
                Str = Str.Replace("<@CustomerCellPhoneNumber@>", CustomerCellPhoneNumber)
                Str = Str.Replace("<@CustomerWatchModel@>", CustomerWatchModel)
                Str = Str.Replace("<@PurchaseDate@>", PurchaseDate)
                Str = Str.Replace("<@ReasonForReturn@>", ReasonForReturn)

                Dim AdminsEmail As String = AdministratorBO.GetAllAdministratorToSendMailTo()

                EmailUtil.SendMail(SystemSettingsBO.GetSettings("CustomerServiceEmail"), AdminsEmail, "", "", Net.Mail.MailPriority.Normal, subject, MailFormat.Html, Str, "")
                lblSuccessmsg.Text = "Thank you for your submission."
                ClearForm()
            Else
                cc.Text = "<b>Enter the code shown above:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b>"
            End If
        Catch ex As Exception
            ErrorLog.XMLError("Error at btnSubmit Event", ex)
        End Try
    End Sub

=======================================================================
Public Shared Function GetAllAdministratorToSendMailTo() As String
    Dim ds As DataSet = AdministratorDB.GetAllAdministratorToSendMailTo()
    Dim BCCAddress As String = ""
    'Dim i As Integer
    Dim count As Integer = ds.Tables(0).Rows.Count
    For Each dr As DataRow In ds.Tables(0).Rows
      BCCAddress &= dr("EmailId") & ";"
    Next
        Return BCCAddress.TrimEnd(";")
        If Not IsNothing(ds) Then   'dataset disposing part added by venkat on 31 Dec 09
            ds.Tables.Clear()
            ds.Clear()
            ds.Dispose()
            ds = Nothing
        End If
  End Function

========================================================================
Public Shared Sub SendMail(ByVal strFrom As String, ByVal strTo As String, ByVal strCC As String, ByVal strBcc As String, ByVal strPriority As Rebex.Mail.MailPriority, ByVal strSubject As String, ByVal strBodyFormat As MailFormat, ByVal strBody As String, ByVal strAttachment As String, Optional ByVal SenderName As String = "")
        Dim _smtp As New Smtp
        'Dim SystemSettingsBO.GetSettings("As") SettingsInfo = (New SettingsBO).GetAllSettings()
        Dim MailMsg As New Rebex.Mail.MailMessage
        Try
            If strFrom <> "" Then
                If SenderName <> "" Then
                    MailMsg.From.Add(New Rebex.Mime.Headers.MailAddress(strFrom, SenderName)) '= New MailAddress(strFrom)
                Else
                    MailMsg.From.Add(New Rebex.Mime.Headers.MailAddress(strFrom)) '= New MailAddress(strFrom)
                End If
            Else
                MailMsg.From.Add(SystemSettingsBO.GetSettings("CustomerServiceEmail")) '= New MailAddress(SystemSettingsBO.GetSettings("CustomerServiceEmail"))
            End If

            'MailMsg.From.Add(strFrom)
            If strTo <> "" Then
                Dim strToArray As String()
                strToArray = strTo.Split(";")

                Dim ToInt As Integer = strToArray.Length
                Dim Tocount As Integer = 0

                While Tocount < ToInt
                    MailMsg.To.Add(strToArray(Tocount))
                    Tocount = Tocount + 1
                End While
            Else
                Throw New Exception("No TO Address Specified")
            End If

            If strCC <> "" Then
                Dim strccArray As String()
                strccArray = strCC.Split(";")

                Dim ccInt As Integer = strccArray.Length
                Dim ccCount As Integer = 0

                While ccCount < ccInt
                    MailMsg.CC.Add(strccArray(ccCount))
                    ccCount = ccCount + 1
                End While
            End If

            If strBcc <> "" Then
                Dim strBccArray As String()
                strBccArray = strBcc.Split(";")

                Dim BccInt As Integer = strBccArray.Length
                Dim BccCount As Integer = 0

                While BccCount < BccInt
                    MailMsg.Bcc.Add(strBccArray(BccCount))
                    BccCount = BccCount + 1
                End While
            End If

            MailMsg.Priority = strPriority
            MailMsg.Subject = strSubject

            If Not HttpContext.Current Is Nothing Then
                If HttpContext.Current.Request.Url.AbsoluteUri.ToString.Contains("SendEmail.aspx") OrElse HttpContext.Current.Request.Url.AbsoluteUri.ToString.Contains("return_form.aspx") Then

                Else
                    strBody = strBody.Replace(vbCrLf, "<br>")
                    strBody = strBody.Replace(vbCr, "<br>")
                    strBody = strBody.Replace(vbLf, "<br>")
                End If
            End If

            strBody = strBody.Replace("&lt;", "<")
            strBody = strBody.Replace("&gt;", ">")
            strBody = strBody.Replace("&quot;", """")
            strBody = strBody.Replace("&amp;", "&")
            strBody = strBody.Replace("../", "" & SystemSettingsBO.GetSettings("BusinessWebAddress") & "") ''Added by team7k for isssue Id# 1534
            MailMsg.BodyHtml = strBody



            'If strBodyFormat = MailFormat.Html Then
            '  MailMsg.BodyHtml = strBody
            'Else
            '  MailMsg.BodyText = strBody
            'End If

            If strAttachment <> "" Then

                Dim strAttachmentArray As String()
                strAttachmentArray = strAttachment.Split(";")

                Dim AttachmentInt As Integer = strAttachmentArray.Length
                Dim AttachmentCount As Integer = 0

                While AttachmentCount < AttachmentInt
                    MailMsg.Attachments.Add(New Rebex.Mail.Attachment(strAttachmentArray(AttachmentCount)))
                    AttachmentCount = AttachmentCount + 1
                End While
            End If
            _smtp.Connect(SystemSettingsBO.GetSettings("SMTPServer"), SystemSettingsBO.GetSettings("SMTPPort"))
            _smtp.Timeout = 300000 'Included by geetha on 21/10/2008
            ' login if needed
            If SystemSettingsBO.GetSettings("SMTPServerUserName") <> "" And SystemSettingsBO.GetSettings("SMTPServerPassword") <> "" Then
                _smtp.Login(SystemSettingsBO.GetSettings("SMTPServerUserName"), SystemSettingsBO.GetSettings("SMTPServerPassword"))
                'Else 'commednted by geetha on 11/11/2008
                '  ErrorLog.Log("Receipt has not been sent.", MessageType.Log)
            End If

            _smtp.Send(MailMsg)
        Catch ex As Exception
            ErrorLog.XMLError("SendMail", ex)
            'ErrorLog.Log("Receipt has not been sent.", MessageType.Log)'commented by geetha on 11/11/2008 as it being handled in helper class.
            Throw ex
        Finally
            MailMsg = Nothing
            _smtp.Disconnect()
            _smtp.Dispose()
        End Try
    End Sub
=====================================================================

Text of .txt file
---------------------
   <div class="webform-results">
            <table class="tabledefault">
                <tbody>
                    <tr>
                        <td id="title">
                            <strong>Return form submission summary:</strong><br>
                        </td>
                    </tr>
                    <tr>
                        <td id="name">
                            <strong>Your Name</strong><br>
                            <@CustomerName@>
                        </td>
                    </tr>
                    <tr>
                        <td id="email">
                            <strong>Email Address</strong><br>
                            <@CustomerEmailID@>
                        </td>
                    </tr>
                    <tr>
                        <td id="casenumber">
                            <strong>Case Number</strong><br>
                            <@CustomerCaseNumber@>
                        </td>
                    </tr>
                    <tr>
                        <td id="shippingaddress">
                            <strong>Shipping Address</strong><br>
                            <@CustomerShippingAddress@>
                        </td>
                    </tr>
                    <tr>
                        <td id="cellphone">
                            <strong>Cell Phone Number</strong><br>
                            <@CustomerCellPhoneNumber@>
                        </td>
                    </tr>
                    <tr>
                        <td id="customfields">
                            <table class="tabledefault">
                                <tbody>
                                    <tr>
                                        <td id="CAT_Custom_133845">
                                            <strong>WATCH MODEL</strong><br>
                                            <@CustomerWatchModel@>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td id="CAT_Custom_133843">
                                            <strong>PURCHASE DATE</strong><br>
                                           <@PurchaseDate@>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td id="CAT_Custom_133844">
                                            <strong>REASON FOR RETURN</strong><br>
                                            <@ReasonForReturn@>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>

No comments:

Post a Comment