Question

I am using DevExpress tools, specifically the FileManager which has a 'SelectedFiles' property which returns all the data needed to (add,insert,delete,retrieve, modify the record). However I can not figure out how to use the selectedfiles as a MailMessage.Attachment. The code below works to send the email, I've changed the credentials and host values for security. I just need some direction or thought on how to use the FileManager collection that is generated via 'SelectedFiles' and add them as an attachment to the email. I would really like to Zip the files if possible, but at this point simply attaching them is fine. Any thoughts?

   Dim fileManager As ASPxFileManager = TryCast(sender, ASPxFileManager)
    If ASPxFileManager1.SelectedFiles IsNot Nothing AndAlso ASPxFileManager1.SelectedFiles.Length > 0 Then
        For i As Integer = 0 To ASPxFileManager1.SelectedFiles.Length - 1
            Dim file = ASPxFileManager1.SelectedFiles.ToString
            Dim attachments As New Attachment(fileManager.SelectedFiles.ToString)???

        Next
    End If
    Try
        Dim mail As New MailMessage("noreply", DropDownEdit.Text)
        Dim smtp_Server As New SmtpClient("host") With {.Credentials = New Net.NetworkCredential("username", "password")}

        mail.Subject = "SUBJECT"
        mail.IsBodyHtml = False
        mail.Body = "Testing"
        smtp_Server.Send(mail)
        successLabel.Text = "Your email was sent successfully."

    Catch ex As Exception

    End Try

End Sub
Was it helpful?

Solution

                Dim attachments As New Attachment(ReadFile(ASPxFileManager1.SelectedFiles(i)), file)
                mail.Attachments.Add(attachments)

The function below was needed to Read the bytes and then attach the items to the MailMessage.

Public Function ReadFile(file__1 As FileManagerFile) As System.IO.Stream
    'This function allows us to pull the bytes from the DB value to render the file.
    Dim filePath As String = (file__1.RelativeName)
    Dim fileData As Byte()
    Using con As New SqlConnection([Global].conn)
        Dim sqlCmd As New SqlCommand()
        sqlCmd.Connection = con
        sqlCmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = file__1.Name
        sqlCmd.Parameters.Add("@APIKey", SqlDbType.Int).Value = Session("_UserAPIKey")
        sqlCmd.CommandText = "SELECT STATEMENT"
        con.Open()
        Dim sqlReader As SqlDataReader = sqlCmd.ExecuteReader()

        If sqlReader.HasRows Then
            While sqlReader.Read()
                fileData = CType(sqlReader(0), Byte())
            End While
        End If

    End Using

    Return New MemoryStream(fileData)

End Function
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top