Я пытаюсь прикрепить двоичные данные, полученные с сервера MySQL, к электронной почте в виде вложения с помощью VB.net.Не удается открыть вложение электронной почты из массива байтов
Я могу отправить электронное письмо с прикрепленным файлом, но не смог открыть какой-либо файл вложения, даже текстовый файл также пуст.
Сообщение об ошибке -> Adobe Reader не смог открыть «xxx.pdf», потому что это либо не поддерживаемый тип файла, либо потому, что файл поврежден (например, он был отправлен как вложение электронной почты и wasn 't правильно декодировано).
Обнаженная, но все еще не в состоянии выяснить, что не так с моим кодом.
Любой, кто может помочь, будет очень благодарен!
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
Вы проверили полученное вложение с шестнадцатеричным редактором, чтобы увидеть, что с ним что-то явно не так? –