Saturday 17 March 2012

Read Message body & Subject from .txt file And Send Email In windows Application....

 Private Sub btnSendMail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSendMail.Click
        Dim Err As Integer = 0
        EmailFailedCount = 0
        AllEmailIdCount = 0
        AllSuccessEailCount = String.Empty
        Try
            Dim selectedCount As Integer = SelectedDataBaseCount()
            If selectedCount = 0 Then
                ErrorLog.XmlError("btnSendMail_Click", New ApplicationException("Please choose atleast one database to send Notifcation e-mail."))
                txtStatus.Text &= "Please choose atleast one database to send Notifcation e-mail. " & Environment.NewLine
                txtStatus.Refresh()

                lblProgress.Text = "Please choose atleast one database to send Notifcation e-mail."
                lblProgress.Refresh()

                MsgBox("Please choose atleast one database to send Notifcation e-mail.")
                Err = 1
                Return
            End If
            btnSendMail.Enabled = False
            Dim oRead As StreamReader

            Dim subject As String = String.Empty
            Dim SubjectFileName As String = System.Windows.Forms.Application.StartupPath & "\UpdateNotifcationEmailSubject.txt"

            Dim objReader1 As New System.IO.StreamReader(SubjectFileName)
            If System.IO.File.Exists(SubjectFileName) = True Then
                oRead = IO.File.OpenText(SubjectFileName)
                subject = oRead.ReadLine
            End If

            Dim FILE_NAME As String = System.Windows.Forms.Application.StartupPath & "\UpdateNotifcationEmailBody.txt"

            Dim objReader As New System.IO.StreamReader(FILE_NAME)
            If System.IO.File.Exists(FILE_NAME) = True Then
                oRead = IO.File.OpenText(FILE_NAME)
            End If

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

            'Str = Str.Replace("<@UpdateTime@>", DateTime.Now.ToString(" MMMM d, yyyy  h:mm tt 'PST'"))

            Dim PriviousBusinessWebAddress As String = String.Empty
            Dim PriviousUpdateTime As String = String.Empty

            For Each dr As Infragistics.Win.UltraWinGrid.UltraGridRow In dgvDatabases.Rows
                If dr.Cells("select").Value = True Then
                    DatabaseName = dr.Cells("DataBaseName_Text").Value.ToString()

                    Dim BusinessWebAddress As String = DBUtil.GetBusinessWebAddressWhenEmail(DatabaseName)

                    If Str.Contains("<@WebURL@>") Then
                        Str = Str.Replace("<@WebURL@>", BusinessWebAddress)
                    Else
                        If PriviousBusinessWebAddress <> "" Then
                            Str = Str.Replace(PriviousBusinessWebAddress, BusinessWebAddress)
                        End If
                    End If

                    If Str.Contains("<@UpdateTime@>") Then
                        Str = Str.Replace("<@UpdateTime@>", DateTime.Now.ToString(" MMMM d, yyyy  h:mm tt 'PST'"))
                        PriviousUpdateTime = DateTime.Now.ToString(" MMMM d, yyyy  h:mm tt 'PST'")
                    Else
                        If PriviousUpdateTime <> "" Then
                            Str = Str.Replace(PriviousUpdateTime, DateTime.Now.ToString(" MMMM d, yyyy  h:mm tt 'PST'"))
                        End If
                        PriviousUpdateTime = DateTime.Now.ToString(" MMMM d, yyyy  h:mm tt 'PST'")
                    End If

                    Dim AdminsEmails As String = DBUtil.GetAllAdministratorToSendMailTo(DatabaseName)
                    SendMail(DBUtil.GetSettingsWhenEmail("CustomerServiceEmail", DatabaseName), AdminsEmails, "", "", subject, MailFormat.Html, Str, "")

                    PriviousBusinessWebAddress = BusinessWebAddress
                End If
            Next
        Catch ex As Exception
            txtStatus.Text &= ex.Message & vbCrLf
            txtStatus.Refresh()
            ErrorLog.XmlError("btnSendMail_Click", ex)
            MessageBox.Show(ex.Message.ToString(), "Errors")
        Finally
            If Err = 0 Then
                lblProgress.Text = AllSuccessEailCount & " emails have been sent out of " & AllEmailIdCount
                lblProgress.Refresh()
                btnSendMail.Enabled = True
            End If
        End Try
    End Sub

No comments:

Post a Comment