Skip to content

Commit

Permalink
Modified AttachmentCollection.Add()
Browse files Browse the repository at this point in the history
When adding a message/rfc822 stream, if the content fails to parse
as a MimeMessage, then fall back to attaching the content as
application/octet-stream.

This is only possible if the stream is seekable and if the mime-type
was auto-detected. If the user has specifically requested that we
attach as a message/rfc822, allow the FormatException to bubble back up
to our caller.

Fixes issue #1001
  • Loading branch information
jstedfast committed Feb 7, 2024
1 parent 93e7986 commit 9f63257
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions MimeKit/AttachmentCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,19 +238,33 @@ static string GetFileName (string path)
return index > 0 ? path.Substring (index + 1) : path;
}

MimeEntity CreateAttachment (ContentType contentType, string path, Stream stream, bool copyStream, CancellationToken cancellationToken)
MimeEntity CreateAttachment (ContentType contentType, bool autoDetected, string path, Stream stream, bool copyStream, CancellationToken cancellationToken)
{
var fileName = GetFileName (path);
MimeEntity attachment;
MimeEntity attachment = null;

if (contentType.IsMimeType ("message", "rfc822")) {
var message = MimeMessage.Load (stream, cancellationToken);

if (!copyStream)
stream.Dispose ();
long position = stream.CanSeek ? stream.Position : 0;

try {
var message = MimeMessage.Load (stream, cancellationToken);

if (!copyStream)
stream.Dispose ();

attachment = new MessagePart { Message = message };
} catch (FormatException) {
if (autoDetected && stream.CanSeek) {
// If the contentType was auto-detected and the stream is seekable, fall back to attaching this content as a generic stream
contentType = new ContentType ("application", "octet-stream");
stream.Position = position;
} else {
throw;
}
}
}

attachment = new MessagePart { Message = message };
} else {
if (attachment is null) {
MimePart part;

if (contentType.IsMimeType ("text", "*")) {
Expand Down Expand Up @@ -350,7 +364,7 @@ public MimeEntity Add (string fileName, byte[] data, ContentType contentType)
throw new ArgumentNullException (nameof (contentType));

var stream = new MemoryStream (data, false);
var attachment = CreateAttachment (contentType, fileName, stream, false, CancellationToken.None);
var attachment = CreateAttachment (contentType, false, fileName, stream, false, CancellationToken.None);

attachments.Add (attachment);

Expand Down Expand Up @@ -401,7 +415,7 @@ public MimeEntity Add (string fileName, Stream stream, ContentType contentType,
if (contentType is null)
throw new ArgumentNullException (nameof (contentType));

var attachment = CreateAttachment (contentType, fileName, stream, true, cancellationToken);
var attachment = CreateAttachment (contentType, false, fileName, stream, true, cancellationToken);

attachments.Add (attachment);

Expand Down Expand Up @@ -489,7 +503,7 @@ public MimeEntity Add (string fileName, byte[] data)
throw new ArgumentNullException (nameof (data));

var stream = new MemoryStream (data, false);
var attachment = CreateAttachment (GetMimeType (fileName), fileName, stream, false, CancellationToken.None);
var attachment = CreateAttachment (GetMimeType (fileName), true, fileName, stream, false, CancellationToken.None);

attachments.Add (attachment);

Expand Down Expand Up @@ -532,7 +546,7 @@ public MimeEntity Add (string fileName, Stream stream, CancellationToken cancell
if (stream is null)
throw new ArgumentNullException (nameof (stream));

var attachment = CreateAttachment (GetMimeType (fileName), fileName, stream, true, cancellationToken);
var attachment = CreateAttachment (GetMimeType (fileName), true, fileName, stream, true, cancellationToken);

attachments.Add (attachment);

Expand Down Expand Up @@ -626,7 +640,7 @@ public MimeEntity Add (string fileName, ContentType contentType, CancellationTok
throw new ArgumentNullException (nameof (contentType));

using (var stream = File.OpenRead (fileName)) {
var attachment = CreateAttachment (contentType, fileName, stream, true, cancellationToken);
var attachment = CreateAttachment (contentType, false, fileName, stream, true, cancellationToken);

attachments.Add (attachment);

Expand Down Expand Up @@ -729,7 +743,7 @@ public MimeEntity Add (string fileName, CancellationToken cancellationToken = de
throw new ArgumentException ("The specified file path is empty.", nameof (fileName));

using (var stream = File.OpenRead (fileName)) {
var attachment = CreateAttachment (GetMimeType (fileName), fileName, stream, true, cancellationToken);
var attachment = CreateAttachment (GetMimeType (fileName), true, fileName, stream, true, cancellationToken);

attachments.Add (attachment);

Expand Down

0 comments on commit 9f63257

Please sign in to comment.