diff --git a/MimeKit/Multipart.cs b/MimeKit/Multipart.cs index 0d33ed6e78..c3c6549415 100644 --- a/MimeKit/Multipart.cs +++ b/MimeKit/Multipart.cs @@ -436,6 +436,7 @@ 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; @@ -443,6 +444,11 @@ public override void Prepare (EncodingConstraint constraint, int maxLineLength = 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; @@ -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; @@ -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; diff --git a/UnitTests/MimeMessageTests.cs b/UnitTests/MimeMessageTests.cs index 8a9f42caae..6eb7c0fae5 100644 --- a/UnitTests/MimeMessageTests.cs +++ b/UnitTests/MimeMessageTests.cs @@ -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: + +--OtherAccess +Content-Type: message/external-body; + access-type=""local-file""; + name=""/some/directory/loser.gif"" + +Content-Type: image/gif +Content-ID: + +--OtherAccess +Content-Type: message/external-body; + access-type=""afs""; + name=""/afs/directory/loser.gif"" + +Content-Type: image/gif +Content-ID: + +--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 () { diff --git a/UnitTests/MimeParserTests.cs b/UnitTests/MimeParserTests.cs index 67e2fe688f..d7505b1910 100644 --- a/UnitTests/MimeParserTests.cs +++ b/UnitTests/MimeParserTests.cs @@ -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); } }