Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing/Returning Normal and Serialized Values between Classes

Status
Not open for further replies.

xcaliber2222

Programmer
Apr 10, 2008
70
0
0
US
Hello, I have this class which has an overloaded SendEmail() function which is going to have its values passed from a global utility class which I pass the values to from different pages throughout my application:

Code:
Imports System.Net.Mail
Imports System.Net.Mail.SmtpClient
Imports system.net
Imports System.IO
Imports System.Reflection
Imports System.Xml
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization

Public Class email_info
    Public Shared Function GetRelativePath(ByVal pgPage As Page) As String
        If pgPage.Request.ApplicationPath = "/" Then
            Return ""
        Else
            Return pgPage.Request.ApplicationPath
        End If
    End Function

    Public Shared Function VerifyEmailAddr(ByVal emailAddress As String) As Boolean
        Dim EmailRegex As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex("^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$")
        Dim M As System.Text.RegularExpressions.Match
        Dim arrEmail As String()
        Dim i As Integer
        Dim bValid As Boolean = True

        arrEmail = emailAddress.Replace(",", ";").Split(";")

        For i = 0 To arrEmail.Length - 1
            If arrEmail(i) <> "" Then
                M = EmailRegex.Match(arrEmail(i))
                If Not M.Success Then
                    bValid = False
                End If
            End If
        Next

        Return bValid
    End Function
    ' Set Priority enumerations. 
    Public Enum Priority
        High
        Normal
    End Enum

    Public Overloads Shared Function SendEmail(ByVal to_emails As String, ByVal from_email As String, ByVal message As String, ByVal subject As String, ByVal cc_emails As String, ByVal bcc_emails As String, ByVal priority As Priority, ByVal is_html As Boolean, ByVal email_host As String) As Boolean
        Return SendEmail(to_emails, from_email, message, subject, cc_emails, bcc_emails, priority, is_html, email_host, Nothing)
    End Function

    Public Overloads Shared Function SendEmail(ByVal to_emails As String, ByVal from_email As String, ByVal message As String, ByVal subject As String, ByVal cc_emails As String, ByVal bcc_emails As String, ByVal priority As Priority, ByVal is_html As Boolean, ByVal email_host As String, ByVal attachment As ArrayList) As Boolean
        Dim strDebug As String
        Dim strArrToAddr As String()
        Dim strArrCCAddr As String()
        Dim strArrBCCAddr As String()

        Dim i As Integer
        Dim j As Integer
        Dim bValid As Boolean = True
        Dim bInValid As Boolean = False
        ' Set Mail Message
        Dim MailMsg As MailMessage = New MailMessage()
        ' Check To Email
        If to_emails = "" Then
            Return bInValid
        End If
        ' Check From Email
        If from_email = "" Then
            Return bInValid
        End If
        ' Check Email Host
        If email_host = "" Then
            Return bInValid
        End If
        MailMsg.From = New MailAddress(from_email.Trim())
        ' Split the Email
        strArrToAddr = to_emails.Replace(",", ";").Split(";")
        ' Set To Email List
        For i = 0 To strArrToAddr.Length - 1
            If strArrToAddr(i) <> "" Then
                MailMsg.To.Add(New MailAddress(strArrToAddr(i).Trim()))
            End If
        Next

        strArrCCAddr = cc_emails.Replace(",", ";").Split(";")
        ' Set CC Email List
        If Not cc_emails Is Nothing Then
            If cc_emails <> "" Then
                For j = 0 To strArrCCAddr.Length - 1
                    If strArrCCAddr(j) <> "" Then
                        MailMsg.CC.Add(New MailAddress(strArrCCAddr(j).Trim()))
                    End If
                Next
            End If
        End If

        MailMsg.Subject = subject
        MailMsg.Body = message
        MailMsg.IsBodyHtml = is_html
        ' Set Email priority to High or Normal
        Select Case priority
            Case priority.High
                MailMsg.Priority = MailPriority.High
            Case priority.Normal
                MailMsg.Priority = MailPriority.Normal
        End Select

        If Not attachment Is Nothing Then
            For Each strFilePath As String In attachment
                If File.Exists(strFilePath) Then
                    Dim att As New Net.Mail.Attachment(strFilePath)
                    MailMsg.Attachments.Add(att)
                End If
            Next
        End If

        Dim SmtpMail As New Net.Mail.SmtpClient()
        ' Set Email Host
        SmtpMail.Host = email_host
        ' Check the Email, it will return error message if it fail 
        Try
            SmtpMail.Send(MailMsg)
            strDebug = "Email Sent"

        Catch ex As Exception
            strDebug = "Email Sending Error. " & ex.Message
            Return bInValid

        End Try

        Return bValid

    End Function
End Class

Here is the global utility class, which I've added an AppLogInsert() function to in order to grab the subject, message and rest of the info, which I am trying to do as a Serialiazble Object from the email_info class:

Code:
Imports System.Net.Mail
Imports System.Net.Mail.SmtpClient
Imports system.net
Imports System.IO
Imports System.Reflection
Imports System.Xml
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization

Public Class global_util

    Public Overloads Shared Function AppLogInsert(ByVal subject As String, ByVal message As String, ByVal email_info As Object) As Object

        Dim Conn As SqlConnection
        Conn = New SqlConnection("server=NVS05;database=netlink2_bkup;Trusted_Connection=yes")
        Dim cmd As SqlCommand
        Dim SQLStr As String

        Dim apppath As String
        apppath = HttpContext.Current.Request.PhysicalApplicationPath

        Dim ThisAssembly As [Assembly] = [Assembly].GetExecutingAssembly()

        SQLStr = "INSERT INTO app_alert_log (application, class, subject, message, email_info) VALUES (@application, @class, @subject, @message, @email_info)"

        cmd = New SqlCommand(SQLStr, Conn)

        'note: use appropriate values for SqlDbType
        cmd.Parameters.Add("@application", SqlDbType.VarChar)
        cmd.Parameters.Add("@class", SqlDbType.VarChar)
        cmd.Parameters.Add("@subject", SqlDbType.VarChar)
        cmd.Parameters.Add("@message", SqlDbType.VarChar)
        cmd.Parameters.Add("@email_info", SqlDbType.Binary)

        'note: use appropriate values
        cmd.Parameters("@application").Value = apppath
        cmd.Parameters("@class").Value = ThisAssembly
        cmd.Parameters("@subject").Value = "Subject"
        cmd.Parameters("@message").Value = "Message"
        cmd.Parameters("@email_info").Value = SERIALIZED OBJECT

        '!!NOTE: to save your email_info class you will need to serialize the object, and use that for SerializedObject
        'checked this link for an example of object serialization: [URL unfurl="true"]http://www.devcity.net/Articles/113/1/dotnet_serialization.aspx[/URL]

        'Finally, save the data
        Dim Trans As SqlTransaction
        Try
            Trans = Conn.BeginTransaction
            cmd.Transaction = Trans
            cmd.ExecuteNonQuery()
            Trans.Commit()
        Catch ex As Exception
            If Not Trans Is Nothing Then
                Trans.Rollback()
            End If
        End Try
    End Function
End Class

Here are my questions:

1. Can someone please show me an example of how I could pass the values from the global_util class to the SendEmail() functions in the email_info class? The reason for this is that I've taken the functions from the global_util class and separated them out in the email_info class.

2. Could you please show me an example of how I would return the values back from the email_info class to the global_util class?

3. In returning the values back from the email_info class into the global_util class, could you please show me an example of how I would pass the value from the Serialized object into the cmd.Parameters("@email_info").Value = SerializedObject? Do I need to actually Serialize the email_info class then Deserialize in the global_util class or does the Serialize/Deserialize take place completely in the global_util class? Also, I'm assuming this would need to be xml serialization which is going to provide the values to be inserted into the database table. I believe I have the correct Imports declarations.

4. Am I correct to assume that the return type for the AppLogInsert() function in the global_util class should be As Object?

I hope this wasn't too many questions and what I'm trying to do makes sense.

Thanks,
Alejandro
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top