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!

Sending Email Attachment with ASP.NET 2.0 1

Status
Not open for further replies.

xcaliber2222

Programmer
Apr 10, 2008
70
US
Hello,

I have the following code which I can step through in debug mode fine, and I receive the email, but without the attachment. I had a security/permission issue I've resolved, so if someone could take a peek and maybe see what I'm missing I'd sure appreciate it:

Code:
Region " Send Email "
    Private Sub SendEmail()
        Dim strBody As String
        strBody = CreateMessage("Email")

        Dim strMailTo As String
        strMailTo = "test@test.com"
       
        Dim strAttachment As String = String.Empty
        strAttachment = Me.fileAttachment.Value

        Dim sw As StreamWriter
        sw = File.CreateText(strAttachment)

        Try
            Dim strEmailIPAddr As String = CType(ConfigAppSettings.GetValue("EmailIPAddr", GetType(System.String)), String)
            Dim att As New ArrayList
            att.Add(strAttachment)
Me.Message.Text = "Your Request has been submitted."
            Me.Message2.Text = "Your Request has been submitted."
            Me.Message.Visible = True

            email_info.to_emails = strMailTo
            email_info.from_email = email_addr
            email_info.cc_emails = ""
            email_info.bcc_emails = ""
            email_info.priority = global_utl.Priority.Normal
            email_info.is_html = True
            email_info.email_host = strEmailIPAddr
            email_info.attachment = att
            email_info.message = strBody
            email_info.subject = "IT Equipment Request"

            global_utl.AppLogInsert("admin", "is_equip_request", "IT Equipment Request", strBody, email_info)

        Catch ex As Exception
            Trace.Warn(ex.Message)
            DisplayError(ex.Message)
        End Try
    End Sub
#End Region

I am overridding this function in the global_utl class:
Code:
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

        ' Set CC Email List
        If Not cc_emails Is Nothing Then
            If cc_emails <> "" Then
                '1817
                strArrCCAddr = cc_emails.Replace(",", ";").Split(";")
                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

        ' Set BCC Email List
        If Not bcc_emails Is Nothing Then
            If bcc_emails <> "" Then
                '1817
                strArrBCCAddr = bcc_emails.Replace(",", ";").Split(";")
                For j = 0 To strArrBCCAddr.Length - 1
                    If strArrBCCAddr(j) <> "" Then
                        MailMsg.Bcc.Add(New MailAddress(strArrBCCAddr(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

And here is the AppLogInsert function:
Code:
Public Overloads Shared Function AppLogInsert(ByVal application As String, ByVal class_name As String, ByVal description As String, ByVal addtl_info As String, ByVal email_info As email_info) As Boolean

        Dim connObj As New SqlConnection("server=xxx123;database=test_bkup;Integrated Security=True;") 
        Dim comObj As New SqlCommand
        Dim Trans As SqlTransaction
        Dim SQLStr As String

        SQLStr = "INSERT INTO app_test_log (application, class_name, description, addtl_info, email_info) VALUES (@application, @class_name, @description, @addtl_info, @email_info)"


        comObj = New SqlCommand(SQLStr, connObj)
        connObj.Open()

        'note: use appropriate values for SqlDbType
        comObj.Parameters.Add("@application", SqlDbType.Char)
        comObj.Parameters.Add("@class_name", SqlDbType.Char)
        comObj.Parameters.Add("@description", SqlDbType.VarChar)
        comObj.Parameters.Add("@addtl_info", SqlDbType.Text)
        comObj.Parameters.Add("@email_info", SqlDbType.NText)

        comObj.Parameters("@application").Value = application
        comObj.Parameters("@class_name").Value = class_name
        comObj.Parameters("@description").Value = description
        comObj.Parameters("@addtl_info").Value = addtl_info
        comObj.Parameters("@email_info").Value = DBNull.Value


        'Save the data
        Try
            Trans = connObj.BeginTransaction
            comObj.Transaction = Trans
            comObj.ExecuteNonQuery()
            Trans.Commit()
            connObj.Close()
        Catch ex As Exception
            If Not Trans Is Nothing Then
                Trans.Rollback()
            End If
        End Try

        If email_info Is Nothing Then
            Return False
        Else
            SendEmail(email_info)
        End If
        Return True
    End Function

Here is the email_info properties file:

Code:
Public Class email_info
    Private _to_emails As String
    Private _from_email As String
    Private _message As String
    Private _subject As String
    Private _cc_emails As String
    Private _bcc_emails As String
    Private _priority As global_utl.Priority
    Private _is_html As Boolean
    Private _email_host As String
    Private _attachment As ArrayList

    Public Const ApplicationMode As String = "1"

    Public Sub New()
    End Sub
    Public Property to_emails() As String
        Get
            Return _to_emails
        End Get
        Set(ByVal value As String)
            _to_emails = value
        End Set
    End Property
    Public Property from_email() As String
        Get
            Return _from_email
        End Get
        Set(ByVal value As String)
            _from_email = value
        End Set
    End Property
    Public Property message() As String
        Get
            Return _message
        End Get
        Set(ByVal value As String)
            _message = value
        End Set
    End Property
    Public Property subject() As String
        Get
            Return _subject
        End Get
        Set(ByVal value As String)
            _subject = value
        End Set
    End Property
    Public Property cc_emails() As String
        Get
            Return _cc_emails
        End Get
        Set(ByVal value As String)
            _cc_emails = value
        End Set
    End Property
    Public Property bcc_emails() As String
        Get
            Return _bcc_emails
        End Get
        Set(ByVal value As String)
            _bcc_emails = value
        End Set
    End Property
    Public Property priority() As global_utl.Priority
        Get
            Return _priority
        End Get
        Set(ByVal value As global_utl.Priority)
            _priority = value
        End Set
    End Property
    Public Property is_html() As Boolean
        Get
            Return _is_html
        End Get
        Set(ByVal value As Boolean)
            _is_html = value
        End Set
    End Property
    Public Property email_host() As String
        Get
            Return _email_host
        End Get
        Set(ByVal value As String)
            _email_host = value
        End Set
    End Property
    Public Property attachment() As ArrayList
        Get
            Return _attachment
        End Get
        Set(ByVal value As ArrayList)
            _attachment = value
        End Set
    End Property
    
End Class

Getting the output to the email fine, except no attachment.

Any help greatly appreciated.

Thanks,
Alejandro
 
Did you step through the code so you are positive that the attachment condition is met and that the strFilePath exists is true?

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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top