Free Support Forum - aspose.com

ContentStream of an attachment

I’m trying to use the ContentStream property of an attachment object to read into a byte array. See the code below. Msg is an Aspose MailMessage object. After the code executes, the length of my byte array is non-zero, in fact it’s 18003. However all 18003 elements are 0. Any idea what might be wrong? Why does it seem that the read from the ContentStream does not work? I ultimately want a byte array because I am passing it as the value of an SqlParameter object.

Dim data() As Byte
ReDim data(Msg.Attachments.Item(x).ContentStream.Length - 1)
Msg.Attachments.Item(x).ContentStream.Read(data, 0, Msg.Attachments.Item(x).ContentStream.Length)
Msg.Attachments.Item(x).ContentStream.Close()

This same concept works here but I’m opening the same file I used at the attachment, and my byte array has data. And the size is also 18003.

Dim imagestream As FileStream = New FileStream(PDFFile, FileMode.Open)
Dim data() As Byte
ReDim data(imagestream.Length - 1)
imagestream.Read(data, 0, imagestream.Length)
imagestream.Close()

Hi,

Thank you for inquiry.

I am sorry but I could not reproduce the issue at my end with the latest version of Aspose.Network 6.4 using the similar code. I saved the byte array to file instead of passing it to DB, for verification of correct data in the byte array. All attachments were created to disk using the byte array as a source.

Here is my code


Dim lic As Aspose.Network.License = New Aspose.Network.License()
lic.SetLicense(“d:\data\aspose\lic\aspose.total.product.family.lic”)

Dim message As MailMessage = MailMessage.Load(“test email with multiple attachments.msg”, MessageFormat.Msg)
For Each att As Attachment In message.Attachments
Dim data As Byte() = New Byte(att.ContentStream.Length - 1) {}
Console.WriteLine(att.ContentStream.Length & " = " & att.ContentStream.Position)
att.ContentStream.Read(data, 0, data.Length)

WriteByteArrayToFile(data, att.Name)
Next att

Public Function WriteByteArrayToFile(ByVal buff As Byte(), ByVal fileName As String) As Boolean
Dim response As Boolean = False

Try
Dim fs As FileStream = New FileStream(fileName, FileMode.Create, FileAccess.ReadWrite)
Dim bw As BinaryWriter = New BinaryWriter(fs)
bw.Write(buff)
bw.Close()
response = True
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Return response
End Function