MailBee.NET Objects 4.0

AttachmentCollection.Add Method (Byte[], String, String, String, HeaderCollection, NewAttachmentOptions, MailTransferEncoding)

Adds the attachment from a byte array.

public void Add(
   byte[] data,
   string targetFilename,
   string contentID,
   string contentType,
   HeaderCollection customHeaders,
   NewAttachmentOptions options,
   MailTransferEncoding mailEnc
);

Parameters

data
The binary content of the attachment represented as a byte array.
targetFilename
The filename under which to add the attachment into the collection. If an empty string, the attachment will have no name.
contentID
The content identifier (CID) of the attachment (for inline attachments), or empty string if the attachment is not inline.
contentType
The content type of the attachment, or a null reference (Nothing in Visual Basic) to let MailBee detect the content type automatically.
customHeaders
The collection of the headers which should be included into the header section in additon to the standard attachment headers. If a null reference (Nothing in Visual Basic), no custom headers will be added.
options
The options which affect how the attachment is added.
mailEnc
The mail encoding to use when placing the attachment data into the message.

Exceptions

Exception Type Condition
MailBeeInvalidArgumentException data or targetFilename is a null reference (Nothing in Visual Basic).

Example

This sample reads a file into memory buffer and adds it as attachment to the message.

Note   In real-world apps, it's easier to use Add overload to add an attachment from a file.
[C#]
// To use the code below, import these namespaces at the top of your code.
using System.IO;
using MailBee;
using MailBee.Mime;

// Create new data buffer as binary array.
byte[] fileData = null;

// Open a file for reading.
using (FileStream fs = new FileStream(@"C:\Temp\1.gif", FileMode.Open))
{
    // Set the size of the buffer.
    fileData = new byte[fs.Length];

    // Create new BinaryReader object.
    BinaryReader br = new BinaryReader(fs);

    // Read binary data from file to buffer.
    br.Read(fileData, 0, fileData.Length);
}

// Create a new MailMessage object.
MailMessage msg = new MailMessage();

// Add an attachment to the message.
msg.Attachments.Add(fileData, "my.gif", "<12s4a8a8778c$5664i1b1$ir671781@tlffmdqjobxj>", "image/gif", null, NewAttachmentOptions.None, MailTransferEncoding.Base64);
[Visual Basic]
' To use the code below, import these namespaces at the top of your code.
Imports System.IO
Imports MailBee
Imports MailBee.Mime

' Create new data buffer as binary array.
Dim fileData As Byte() = Nothing

' Open file for read.
Dim fs As FileStream
Try
    fs = New FileStream("C:\Temp\1.gif", System.IO.FileMode.Open)

    ' Set the size of the buffer.
    ReDim fileData(fs.Length)

    ' Create new BinaryReader object.
    Dim br As New BinaryReader(fs)

    ' Read binary data from file to buffer.
    br.Read(fileData, 0, fileData.Length)
Finally
    If Not fs Is Nothing Then fs.Close()
End Try

' Create new MailMessage object.
Dim msg As New MailMessage

' Add an attachment to the message.
msg.Attachments.Add(fileData, "my.gif", "<12s4a8a8778c$5664i1b1$ir671781@tlffmdqjobxj>", "image/gif", Nothing, NewAttachmentOptions.None, MailTransferEncoding.Base64)

See Also

AttachmentCollection Class | MailBee.Mime Namespace | AttachmentCollection.Add Overload List | Attachment