MailBee.NET Objects 4.0

AttachmentCollection.Add Method (MailMessage, String, String, String, HeaderCollection, NewAttachmentOptions, MailTransferEncoding)

Adds the specified the MailMessage as attachment (i.e. forwards that message as attachment).

public void Add(
   MailMessage message,
   string targetFilename,
   string contentID,
   string contentType,
   HeaderCollection customHeaders,
   NewAttachmentOptions options,
   MailTransferEncoding mailEnc
);

Parameters

message
The mail message to be added as attachment.
targetFilename
The filename under which to add the attachment into the collection. If a null reference (Nothing in Visual Basic), MailBee will take it from the Subject of the message being attached. 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. If a null reference (Nothing in Visual Basic), message/rfc822 value is used.
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.

Remarks

This method can be used to forward mail messages with another mail message. To forward a single message, it's easier to use ForwardAsAttachment method.

Exceptions

Exception Type Condition
MailBeeInvalidArgumentException message is a null reference (Nothing in Visual Basic).

Example

The following example demonstrates how a mail message can be attached to another mail message.

[C#]
// To use the code below, import MailBee namespaces at the top of your code.
using MailBee;
using MailBee.Mime;

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

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

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

// Set plain text body of the message.
msg.BodyPlainText = "Hello, World!";

// Add the attachment to the message.
msg.Attachments.Add(messageToAttach, "1.eml", "", "message/rfc822", null, NewAttachmentOptions.None, MailTransferEncoding.None);

// Save message to disk.
msg.SaveMessage(@"C:\Temp\TestEmail.eml");
[Visual Basic]
' To use the code below, import MailBee namespaces at the top of your code.
Imports MailBee
Imports MailBee.Mime

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

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

' Create new MailMessage object.
Dim msg As New MailMessage

' Set plain text body of the message.
msg.BodyPlainText = "Hello, World!"

' Add the attachment to the message.
msg.Attachments.Add(messageToAttach, "1.eml", "", "message/rfc822", Nothing, NewAttachmentOptions.None, MailTransferEncoding.None)

' Save message to disk.
msg.SaveMessage("C:\Temp\TestEmail.eml")

See Also

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