Tnef AttachmentLoad Embedded Message Method
Load the embedded message.
Definition
Namespace: MimeKit.Tnef
Assembly: MimeKit.Core (in MimeKit.Core.dll) Version: 5.0.0
The embedded message.
Assembly: MimeKit.Core (in MimeKit.Core.dll) Version: 5.0.0
C#
public TnefMessage LoadEmbeddedMessage(
CancellationToken cancellationToken = default
)Parameters
- cancellationToken CancellationToken (Optional)
- The cancellation token.
Return Value
TnefMessageThe embedded message.
Remarks
Loads the embedded TNEF message contained within the attachment.
The embedded message is loaded using the same TnefOptions and compliance logger as the message that contains it, and has its own MaxTotalDataBytes budget. If it is nested more deeply than MaxNestingDepth allows, a NestingTooDeep issue is reported and an empty message is returned.
The returned message is independent of the attachment and must be disposed by the caller.
Example
C#
public static void ExtractAttachments (MimeMessage message, string outputDirectory)
{
foreach (var part in message.BodyParts.OfType<TnefPart> ()) {
using (var tnef = part.LoadTnefMessage ())
SaveAttachments (tnef, outputDirectory);
}
}
static void SaveAttachments (TnefMessage tnef, string outputDirectory)
{
foreach (var attachment in tnef.Attachments) {
if (attachment.IsEmbeddedMessage) {
// An embedded message is a TNEF message in its own right, which may have its own attachments.
using (var embedded = attachment.LoadEmbeddedMessage ())
SaveAttachments (embedded, outputDirectory);
continue;
}
if (!attachment.HasContent)
continue;
// Note: The file name comes from an untrusted source, so sanitize it before using it as a path.
var fileName = Path.GetFileName (attachment.FileName);
if (string.IsNullOrEmpty (fileName))
fileName = "attachment.dat";
using (var content = attachment.OpenRead ())
using (var output = File.Create (Path.Combine (outputDirectory, fileName)))
content.CopyTo (output);
}
}
Exceptions
| ObjectDisposedException | The attachment has been disposed. |
| InvalidOperationException | The attachment is not an embedded message. |
| TnefException | The embedded message does not begin with the TNEF signature. |
| OperationCanceledException | The operation was canceled via the cancellation token. |