MailBee.NET Objects 4.0

Attachment.GetData Method (Int32, Int32)

Gets the specified portion of the actual content of the attachment as a byte array.

public byte[] GetData(
   int offset,
   int size
);

Parameters

offset
The number of bytes which should be skipped before starting reading the attachment content.
size
The number of bytes which should be read. If -1, the content will be read from the offset position till the end of the data.

Return Value

The specified portion of the actual content of the attachment as a byte array.

Example

This sample loads the message from .EML file and saves the first attachment to disk using BinaryWriter object.

Note   In real-world apps, it's easier to use Save or SaveToFolder methods to save an attachment to disk.
[C#]
// To use the code below, import these namespaces at the top of your code.
using System.IO;
using MailBee;
using MailBee.Mime;

// The actual code (put it into a method of your class)

// Load the message from file.
MailMessage msg = new MailMessage();
msg.LoadMessage(@"C:\Docs\TestMail.eml");

// When there is any attachment...
if (msg.Attachments.Count > 0)
{
    // ...open a file for writing...
    using (BinaryWriter bw = new BinaryWriter(File.OpenWrite(@"C:\Temp\" + msg.Attachments[0].Filename)))
    {
        // ...and write the attachment content to the file.
        bw.Write(msg.Attachments[0].GetData(0, -1));
    }
}
[Visual Basic]
' To use the code below, import these namespaces at the top of your code.
Imports System.IO
Imports MailBee
Imports MailBee.Mime

' The actual code (put it into a method of your class)

' Load the message from file.
Dim msg As New MailMessage
msg.LoadMessage("C:\Docs\TestMail.eml")

' When there is any attachment...
If msg.Attachments.Count > 0 Then
    ' ...open a file for writing...
    Dim bw As BinaryWriter
    Try
        bw = New BinaryWriter(System.IO.File.OpenWrite("C:\Temp\" & msg.Attachments(0).Filename))
        
        ' ...and write the attachment content to the file.
        bw.Write(msg.Attachments(0).GetData(0, -1))
    Finally
        If Not bw Is Nothing Then
            bw.Close()
        End If    
    End Try
End If

See Also

Attachment Class | MailBee.Mime Namespace | Attachment.GetData Overload List | Save | Size