0

我正在使用 DevExpress 工具,特别是具有“SelectedFiles”属性的 FileManager,该属性返回所需的所有数据(添加、插入、删除、检索、修改记录)。但是我不知道如何将选定的文件用作 MailMessage.Attachment。下面的代码用于发送电子邮件,为了安全起见,我更改了凭据和主机值。我只需要一些指导或思考如何使用通过“SelectedFiles”生成的 FileManager 集合并将它们作为附件添加到电子邮件中。如果可能的话,我真的很想压缩文件,但此时只需附加它们就可以了。有什么想法吗?

   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
4

1 回答 1

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

下面的函数需要读取字节,然后将项目附加到 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
于 2013-08-28T16:37:05.490 回答