Mysql 无法从字节数组打开电子邮件附件

Mysql 无法从字节数组打开电子邮件附件,mysql,vb.net,Mysql,Vb.net,我正在尝试使用VB.net将从MySQL服务器检索到的二进制数据作为附件附加到电子邮件 我可以发送带有附件的电子邮件,但无法打开任何附件文件,即使word文件也是空的 错误消息是-->Adobe Reader无法打开“xxx.pdf”,因为该文件不是受支持的文件类型,或者该文件已损坏(例如,它作为电子邮件附件发送,未正确解码) 被谷歌搜索过,但仍然无法找出我的代码有什么问题 任何能帮忙的人都将不胜感激!谢谢 Dim m_ImageBinary As [Byte]() = New [Byt

我正在尝试使用VB.net将从MySQL服务器检索到的二进制数据作为附件附加到电子邮件

我可以发送带有附件的电子邮件,但无法打开任何附件文件,即使word文件也是空的

错误消息是-->Adobe Reader无法打开“xxx.pdf”,因为该文件不是受支持的文件类型,或者该文件已损坏(例如,它作为电子邮件附件发送,未正确解码)

被谷歌搜索过,但仍然无法找出我的代码有什么问题

任何能帮忙的人都将不胜感激!谢谢

    Dim m_ImageBinary As [Byte]() = New [Byte](-1) {}
    Dim m_AttachmentType As String = ""
    Dim m_AttachmentName As String = ""
    Dim m_Attachment As Boolean = False
    Dim cmd3 As New MySqlCommand("SELECT Attachment, AttachmentType, AttachmentName FROM tbcommunication WHERE CommunicationID = " & cid, MySql)
    Dim rdr2 As MySqlDataReader = cmd3.ExecuteReader()
    While rdr2.Read()
        If rdr2("Attachment").ToString() <> "" Then
            Dim m_Length As Integer = DirectCast(rdr2("Attachment"), [Byte]()).Length

            m_ImageBinary = New [Byte](m_Length - 1) {}
            m_ImageBinary = DirectCast(rdr2("Attachment"), [Byte]())
            m_AttachmentType = rdr2("AttachmentType").ToString()
            m_AttachmentName = rdr2("AttachmentName").ToString()
        End If
    End While

    If m_ImageBinary.Length <> 0 Then
        If m_Attachment = False Then
            If m_AttachmentType.Contains("jpeg") OrElse m_AttachmentType.Contains("bmp") OrElse m_AttachmentType.Contains("gif") Then
                m_Attachment = False
            Else
                m_Attachment = True
            End If
        End If

        If m_Attachment = True Then
            ' If not image file
            Response.AppendHeader("content-disposition", "attachment; filename=" & m_Atta`enter code here`chmentName)
        End If

        'Write(binary)
        'Response.ContentType = m_AttachmentType
        'Response.BinaryWrite(m_ImageBinary)
        'Response.[End]()
    End If



    Dim mailmssg As New MailMessage()
    Dim smtp_client As New SmtpClient

    Using memoryStream As New MemoryStream()
        Dim bytes As Byte() = memoryStream.ToArray()

        Dim att As New Attachment(New MemoryStream(bytes), m_AttachmentName)

        mailmssg.Attachments.Add(att)
        MemoryStream.Dispose()

        smtp_client.DeliveryMethod = SmtpDeliveryMethod.Network
        smtp_client.Send(mailmssg)
   End Using
Dim m_ImageBinary As[Byte]()=新的[Byte](-1){
Dim m_AttachmentType As String=“”
Dim m_AttachmentName As String=“”
Dim m_附件为布尔值=False
Dim cmd3作为新的MySqlCommand(“从tbcommunication WHERE CommunicationID=“&cid,MySql中选择附件、附件类型、附件名称”)
作为MySqlDataReader=cmd3.ExecuteReader()的Dim rdr2
而rdr2.Read()
如果rdr2(“附件”).ToString()“”则
Dim m_长度为整数=DirectCast(rdr2(“附件”),[Byte]())。长度
m_ImageBinary=新的[字节](m_长度-1){}
m_ImageBinary=DirectCast(rdr2(“附件”),[Byte]())
m_AttachmentType=rdr2(“AttachmentType”).ToString()
m_AttachmentName=rdr2(“AttachmentName”).ToString()
如果结束
结束时
如果m_ImageBinary.Length为0,则
如果m_Attachment=False,则
如果m_AttachmentType.Contains(“jpeg”)或LSE m_AttachmentType.Contains(“bmp”)或LSE m_AttachmentType.Contains(“gif”),则
m_附件=错误
其他的
m_附件=真
如果结束
如果结束
如果m_Attachment=True,则
'如果不是图像文件
Response.AppendHeader(“内容处置”、“附件;文件名=“&m_Atta`enter code here`chmentName”)
如果结束
'写入(二进制)
'Response.ContentType=m_AttachmentType
'Response.BinaryWrite(m_ImageBinary)
'响应。[结束]()
如果结束
Dim mailmssg作为新邮件消息()
Dim smtp_客户端作为新的SmtpClient
将memoryStream用作新的memoryStream()
Dim bytes As Byte()=memoryStream.ToArray()
Dim att作为新附件(新内存流(字节),m_AttachmentName)
mailmssg.Attachments.Add(附件)
MemoryStream.Dispose()
smtp_client.DeliveryMethod=SmtpDeliveryMethod.Network
smtp_客户端发送(mailmssg)
终端使用

使用您当前的代码,您只发送一个名为且始终只有一个名的空文件。

您是否使用十六进制编辑器检查了生成的附件,以查看是否有任何明显的错误?噢..大卫,您能给我一些提示吗..真的不知道memorystream的情况。。
'...
' memoryStream = new blank MemoryStream
Using memoryStream As New MemoryStream()
        ' get emtpy array from memoryStream
        Dim bytes As Byte() = memoryStream.ToArray()
        ' Create empty MemoryStream from empty bytes array
        Dim att As New Attachment(New MemoryStream(bytes), m_AttachmentName)
'...