Skip to content
This repository has been archived by the owner on Dec 31, 2024. It is now read-only.

added check to see if body part has a name and disposition is unknown, b... #106

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions MessageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -352,21 +352,25 @@ internal static void AddBodypart(this MailMessage message, Bodypart part, string
// Attachments collection.
bool hasName = part.Parameters.ContainsKey("name");

// Check for alternative view.
string ContentType = ParseMIMEField(message.Headers["Content-Type"])["value"];
bool preferAlternative = string.Compare(ContentType, "multipart/alternative", true) == 0;

// If the MailMessage's Body fields haven't been initialized yet, put it there. Some weird
// (i.e. spam) mails like to omit content-types so we don't check for that here and just
// assume it's text.
if (String.IsNullOrEmpty(message.Body) &&
part.Disposition.Type != ContentDispositionType.Attachment) {
!(part.Disposition.Type == ContentDispositionType.Attachment ||
(part.Disposition.Type == ContentDispositionType.Unknown &&
preferAlternative == false && hasName))) {
//if (String.IsNullOrEmpty(message.Body) &&
// !(part.Disposition.Type == ContentDispositionType.Attachment)) {
message.Body = encoding.GetString(bytes);
message.BodyEncoding = encoding;
message.IsBodyHtml = part.Subtype.ToLower() == "html";
return;
}

// Check for alternative view.
string ContentType = ParseMIMEField(message.Headers["Content-Type"])["value"];
bool preferAlternative = string.Compare(ContentType, "multipart/alternative", true) == 0;

// Many attachments are missing the disposition-type. If it's not defined as alternative
// and it has a name attribute, assume it is Attachment rather than an AlternateView.
if (part.Disposition.Type == ContentDispositionType.Attachment ||
Expand Down
37 changes: 36 additions & 1 deletion Tests/MessageBuilderTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using System.Linq;
using System.Net.Mail;
Expand Down Expand Up @@ -128,6 +129,40 @@ public void MessageWithMultipartMixedAttachment() {
"Attachment name missmatch.");
}

/// <summary>
/// Creates a MailMessage instance from an RFC822/MIME string containing an attachment without
/// a disposition and an empty body.
/// </summary>
[TestMethod]
[TestCategory("BuildMessageFromMIME822")]
public void MessageWithMultipartMixedAttachmentAndEmptyBody()
{
// The multiparts is manipulated base64 data and not the real files.
MailMessage m = MessageBuilder.FromMIME822(Properties.Resources.MailWithoutDispositionEmptyBody);

// Check the From header.
Assert.AreEqual("", m.From.DisplayName, "Expected no From displayname.");
Assert.AreEqual("[email protected]", m.From.Address, "Unexpected From address.");
// Check the To header.
Assert.AreEqual(1, m.To.Count, "Unexpected To address count.");
Assert.AreEqual("", m.To[0].DisplayName, "Expected no To displayname.");
Assert.AreEqual("[email protected]", m.To[0].Address, "Unexpected To address.");
Assert.AreEqual("Fakt__xxxx___xxxx___07_02_2013", m.Subject, "Unexpected Subject.");
Assert.AreEqual(
"multipart/mixed;\tboundary=\"--boundary_0_6cb33448-390c-4f02-b75a-2738f1d6dd45\"",
m.Headers["Content-Type"], "Unexpected Content-Type");
Assert.IsFalse(m.IsBodyHtml, "Expected non HTML body");
// Ensure that the body encoding is null, since there is no body.
Assert.AreEqual(null, m.BodyEncoding, "Expected null Body Encoding.");
// Ensure that the body is empty.
Assert.AreEqual(String.Empty, m.Body);
Assert.AreEqual(0, m.AlternateViews.Count, "AlternateViews count missmatch.");
// Ensure that the attachment is treated correctly as an attachment.
Assert.AreEqual(1, m.Attachments.Count, "Attachment count missmatch.");
Assert.AreEqual("Fakt_fil_xxxx_20130207.pdf", m.Attachments[0].Name,
"Attachment name missmatch.");
}

/// <summary>
/// Ensures a malformed, invalid or missing From header is handled properly. See issue #50.
/// </summary>
Expand Down
27 changes: 24 additions & 3 deletions Tests/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Tests/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ X-MimeOLE: Produced By Microsoft MimeOLE V6.0.6002.18463</value>
<data name="MailWithoutDisposition" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\MailWithoutDisposition.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
<data name="MailWithoutDispositionEmptyBody" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\MailWithoutDispositionEmptyBody.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
<data name="MailWithQuotedPrintables" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\MailWithQuotedPrintables.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
Expand Down
26 changes: 26 additions & 0 deletions Tests/Resources/MailWithoutDispositionEmptyBody.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Received: from xxx.xxxxx.com (xx.xxxx.com [123.45.67.89])
by xxxx.asxxx.se (Postfix) with ESMTP id E357F2CCC6A
for <[email protected]>; Thu, 7 Feb 2013 17:12:05 +0100 (CET)
MIME-Version: 1.0
From: <[email protected]>
To: <[email protected]>
Date: Thu, 7 Feb 2013 17:12:02 +0100
Subject: Fakt__xxxx___xxxx___07_02_2013
Content-Type: multipart/mixed;
boundary="--boundary_0_6cb33448-390c-4f02-b75a-2738f1d6dd45"
Message-ID: <[email protected]>

----boundary_0_6cb33448-390c-4f02-b75a-2738f1d6dd45
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64

----boundary_0_6cb33448-390c-4f02-b75a-2738f1d6dd45
Content-Type: application/octet-stream; name="Fakt_fil_xxxx_20130207.pdf"
Content-Transfer-Encoding: base64

JVBERi0xLjMNCiX15Pb8DQoNCjcgMCBvYmogPDwgL1R5cGUgL1hPYmplY3QNCi9TdWJ0eXBl
IC9JbWFnZQ0KL05hbWUgL0kxDQovV2lkdGggNDYxDQovSGVpZ2h0IDExMA0KL0JpdHNQZXJD
b21wb25lbnQgOA0KL0NvbG9yU3BhY2UgL0RldmljZUdyYXkNCi9MZW5ndGggOTUyOCAvRmls
dGVyIFsgL0FTQ0lJODVEZWNvZGUgL0ZsYXRlRGVjb2RlIF0gPj4NCnN0cmVhbQ0KR2IiLyxN
NDU0MFtKcD8/V14uckZCMXQuLUBjRVxBclFMVkgzJW1iWWR0bEpcRkVdO21faT==
----boundary_0_6cb33448-390c-4f02-b75a-2738f1d6dd45--
4 changes: 4 additions & 0 deletions Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
Expand All @@ -111,6 +112,9 @@
<ItemGroup>
<None Include="Resources\MailWithMIMEComments.txt" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\MailWithoutDispositionEmptyBody.txt" />
</ItemGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
<ItemGroup>
Expand Down