Skip to content

Commit

Permalink
Fixed serialization for embedded message/* parts
Browse files Browse the repository at this point in the history
Fixes issue #228
  • Loading branch information
jstedfast committed Feb 5, 2016
1 parent eaba4d7 commit eb63e26
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 1 deletion.
12 changes: 12 additions & 0 deletions MimeKit/Multipart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,13 +436,19 @@ public override void Prepare (EncodingConstraint constraint, int maxLineLength =

if (cancellable != null) {
for (int i = 0; i < children.Count; i++) {
var msg = children[i] as MessagePart;
var multi = children[i] as Multipart;
var part = children[i] as MimePart;

cancellable.Write (boundary, 0, boundary.Length - 2, cancellationToken);
cancellable.Write (options.NewLineBytes, 0, options.NewLineBytes.Length, cancellationToken);
children[i].WriteTo (options, stream, false, cancellationToken);

if (msg != null && msg.Message != null && msg.Message.Body != null) {
multi = msg.Message.Body as Multipart;
part = msg.Message.Body as MimePart;
}

if ((part != null && part.ContentObject == null) ||
(multi != null && !multi.WriteEndBoundary))
continue;
Expand All @@ -459,6 +465,7 @@ public override void Prepare (EncodingConstraint constraint, int maxLineLength =
cancellable.Write (options.NewLineBytes, 0, options.NewLineBytes.Length, cancellationToken);
} else {
for (int i = 0; i < children.Count; i++) {
var msg = children[i] as MessagePart;
var multi = children[i] as Multipart;
var part = children[i] as MimePart;

Expand All @@ -467,6 +474,11 @@ public override void Prepare (EncodingConstraint constraint, int maxLineLength =
stream.Write (options.NewLineBytes, 0, options.NewLineBytes.Length);
children[i].WriteTo (options, stream, false, cancellationToken);

if (msg != null && msg.Message != null && msg.Message.Body != null) {
multi = msg.Message.Body as Multipart;
part = msg.Message.Body as MimePart;
}

if ((part != null && part.ContentObject == null) ||
(multi != null && !multi.WriteEndBoundary))
continue;
Expand Down
95 changes: 95 additions & 0 deletions UnitTests/MimeMessageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,101 @@ This is the body.
Assert.AreEqual (rawMessageText, result, "Reserialized message is not identical to the original.");
}

[Test]
public void TestReserializationMessageParts ()
{
string rawMessageText = @"Path: flop.mcom.com!news.Stanford.EDU!agate!tcsi.tcs.com!uunet!vixen.cso.uiuc.edu!gateway
From: Internet-Drafts@CNRI.Reston.VA.US
Subject: I-D ACTION:draft-smith-ipatm-bcast-00.txt
Date: 25 Apr 95 15:09:13 GMT
Organization: University of Illinois at Urbana
Lines: 96
Approved: Usenet@ux1.cso.uiuc.edu
Message-ID: <9504251109.aa04587@IETF.CNRI.Reston.VA.US>
Reply-To: Internet-Drafts@CNRI.Reston.VA.US
NNTP-Posting-Host: ux1.cso.uiuc.edu
Mime-Version: 1.0
Content-Type: Multipart/Mixed; Boundary=""NextPart""
Originator: daemon@ux1.cso.uiuc.edu
--NextPart
here are a couple of external bodies:
--NextPart
Content-Type: Multipart/MIXED; Boundary=""OtherAccess""
--OtherAccess
Content-Type: Message/External-body;
access-type=""mail-server"";
server=""mailserv@ds.internic.net""
Content-Type: text/plain
Content-ID: <19950424144009.I-D@CNRI.Reston.VA.US>
ENCODING mime
FILE /internet-drafts/draft-smith-ipatm-bcast-00.txt
--OtherAccess
Content-Type: Message/External-body;
name=""draft-smith-ipatm-bcast-00.txt"";
site=""ds.internic.net"";
access-type=""anon-ftp"";
directory=""internet-drafts""
Content-Type: text/plain
Content-ID: <19950424144009.I-D@CNRI.Reston.VA.US>
--OtherAccess
Content-Type: message/external-body;
access-type=""URL"";
url=""http://home.netscape.com/
people/
jwz/
index.html""
Content-Type: TEXT/HTML
Content-ID: <spankulate@hubba.hubba.hubba>
--OtherAccess
Content-Type: message/external-body;
access-type=""local-file"";
name=""/some/directory/loser.gif""
Content-Type: image/gif
Content-ID: <spankulate3@hubba.hubba.hubba>
--OtherAccess
Content-Type: message/external-body;
access-type=""afs"";
name=""/afs/directory/loser.gif""
Content-Type: image/gif
Content-ID: <spankulate4@hubba.hubba.hubba>
--OtherAccess--
--NextPart--
".Replace ("\r\n", "\n");
string result;

using (var source = new MemoryStream (Encoding.UTF8.GetBytes (rawMessageText))) {
var parser = new MimeParser (source, MimeFormat.Default);
var message = parser.ParseMessage ();

using (var serialized = new MemoryStream ()) {
var options = FormatOptions.Default.Clone ();
options.NewLineFormat = NewLineFormat.Unix;

message.WriteTo (options, serialized);

result = Encoding.UTF8.GetString (serialized.ToArray ());
}
}

Assert.AreEqual (rawMessageText, result, "Reserialized message is not identical to the original.");
}

[Test]
public void TestMailMessageToMimeMessage ()
{
Expand Down
2 changes: 1 addition & 1 deletion UnitTests/MimeParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public void TestJwzPersistentMbox ()

// Force the various MimePart objects to write their content streams.
// The idea is that by forcing the MimeParts to seek in their content,
// we will test to make sure that parser correctly deals with it.
// we will test to make sure that the parser correctly deals with it.
message.WriteTo (Stream.Null);
}
}
Expand Down

0 comments on commit eb63e26

Please sign in to comment.